Accuracy pendant training

This commit is contained in:
eynard 2022-01-06 12:13:22 +01:00
parent 151343b7bd
commit 58d2d70f2b
4 changed files with 34 additions and 10 deletions

View File

@ -60,14 +60,14 @@ class network:
def train(self, inputs, desiredOutputs, learningRate, batchSize, epochs=1, visualize=False): def train(self, inputs, desiredOutputs, learningRate, batchSize, epochs=1, accuracyInputs=False, accuracyDesiredOutputs=False, visualize=False):
if (type(inputs) != list or type(desiredOutputs) != list): if (type(inputs) != list or type(desiredOutputs) != list):
raise TypeError("The inputs and desired outputs must be lists of numpy arrays !") raise TypeError("The inputs and desired outputs must be lists of numpy arrays !")
if (len(inputs) != len(desiredOutputs)): if (len(inputs) != len(desiredOutputs)):
raise ValueError("The inputs and desired outputs lists must have the same amount of data ! " + str(len(inputs)) + " != " + str(len(desiredOutputs))) raise ValueError("The inputs and desired outputs lists must have the same amount of data ! " + str(len(inputs)) + " != " + str(len(desiredOutputs)))
if (len(inputs) == 0): if (len(inputs) == 0):
raise ValueError("The list is empty !") raise ValueError("The list is empty !")
if (visualize == False): if (visualize != False):
if (self.__inputLayerSize != 2): if (self.__inputLayerSize != 2):
raise ValueError("Visualization is only possible for 2 inputs networks") raise ValueError("Visualization is only possible for 2 inputs networks")
if (len(self.weights[-1]) != 1): if (len(self.weights[-1]) != 1):
@ -126,6 +126,14 @@ class network:
errorSumsBiases[layerNumber] = np.multiply(errorSumsBiases[layerNumber], -(learningRate/len(inputBatch))) errorSumsBiases[layerNumber] = np.multiply(errorSumsBiases[layerNumber], -(learningRate/len(inputBatch)))
self.biases[layerNumber] = np.add(self.biases[layerNumber], errorSumsBiases[layerNumber]) self.biases[layerNumber] = np.add(self.biases[layerNumber], errorSumsBiases[layerNumber])
'''errorSum = 0
for layer in errorSumsBiases:
errorSum += np.sum(np.abs(layer))
print(errorSum)'''
if (accuracyInputs and accuracyDesiredOutputs):
print(self.accuracy(accuracyInputs, accuracyDesiredOutputs))
if (visualize): if (visualize):
ani = animation.ArtistAnimation(fig, vizualisationData, interval=100) ani = animation.ArtistAnimation(fig, vizualisationData, interval=100)
plt.show() plt.show()

View File

@ -14,16 +14,19 @@ class Sketchpad(tkinter.Canvas):
self.bind("<B1-Motion>", self.add_line) self.bind("<B1-Motion>", self.add_line)
self.PILImage = Image.new("F", (560, 560), 100) self.PILImage = Image.new("F", (560, 560), 100)
self.draw = ImageDraw.Draw(self.PILImage) self.draw = ImageDraw.Draw(self.PILImage)
self.MNISTNN = network.networkFromFile("MNIST30epoch") self.MNISTNN = network.networkFromFile("MNISTtest2")
self.predictionLabel = predictionLabel self.predictionLabel = predictionLabel
def add_line(self, event): def add_line(self, event):
self.create_oval((event.x+32, event.y+32, event.x-32, event.y-32), fill="black") self.create_oval((event.x+24, event.y+24, event.x-24, event.y-24), fill="black")
self.draw.ellipse([event.x-32, event.y-32, event.x+32, event.y+32], fill="black") self.draw.ellipse([event.x-24, event.y-24, event.x+24, event.y+24], fill="black")
smallerImage = self.PILImage.reduce(20) smallerImage = self.PILImage.reduce(20)
imageAsArray = np.array(smallerImage.getdata()) imageAsArray = np.array(smallerImage.getdata())
imageAsArray = (100 - imageAsArray)/100 imageAsArray = (100 - imageAsArray)/100
self.predictionLabel['text'] = ( "Predicted number : " + str(np.argmax(self.MNISTNN.process(imageAsArray)))) predictionArray = self.MNISTNN.process(imageAsArray)
print(predictionArray)
prediction = np.argmax(predictionArray)
self.predictionLabel['text'] = ( "Predicted number : " + str(prediction) + " with confidence : " + str(predictionArray[prediction]))
def test(self, event): def test(self, event):
self.PILImage = Image.new("F", (560, 560), 100) self.PILImage = Image.new("F", (560, 560), 100)

View File

@ -27,7 +27,20 @@ for label in tempTrainLabels:
trainLabels.append(np.zeros(10)) trainLabels.append(np.zeros(10))
trainLabels[-1][label] = 1.0 trainLabels[-1][label] = 1.0
myNetwork = network(784, 30, 10) tempAccuracyImages = getData("./MNIST/t10k-images-idx3-ubyte.gz")[0x10:].reshape((-1, 784)).tolist()
accuracyImages = []
for image in tempAccuracyImages:
for pixel in range(784):
if image[pixel] !=0:
image[pixel] = image[pixel]/256
accuracyImages.append(np.array(image, dtype=np.float64))
tempAccuracyLabels = getData("./MNIST/t10k-labels-idx1-ubyte.gz")[8:]
accuracyLabels = []
for label in tempAccuracyLabels:
accuracyLabels.append(np.zeros(10))
accuracyLabels[-1][label] = 1.0
myNetwork = network(784, 32, 10)
learningRate = 3.0 learningRate = 3.0
@ -48,7 +61,7 @@ for i in range(1):
print(batchEnd) print(batchEnd)
""" """
myNetwork.train(trainImages, trainLabels, learningRate, 10, 30) myNetwork.train(trainImages, trainLabels, learningRate, batchSize=10, epochs=30, accuracyInputs=accuracyImages, accuracyDesiredOutputs=accuracyLabels)
endTime = time.perf_counter() endTime = time.perf_counter()
@ -57,4 +70,4 @@ print("Learning time : " + str(endTime - startTime))
print(trainLabels[121]) print(trainLabels[121])
print(myNetwork.process(trainImages[121])) print(myNetwork.process(trainImages[121]))
myNetwork.saveToFile("MNIST30epoch") myNetwork.saveToFile("MNISTtest3")

View File

@ -35,7 +35,7 @@ for i in range(nbRep):
startTime = time.perf_counter() startTime = time.perf_counter()
myNetwork.train(test, result, learningRate, len(test), 10000, visualize=False) myNetwork.train(test, result, learningRate, len(test), 10000, visualize=True)
endTime = time.perf_counter() endTime = time.perf_counter()
learningTime += endTime - startTime learningTime += endTime - startTime