From 58d2d70f2b9c1eb6f2c63f71c48a59636a45f774 Mon Sep 17 00:00:00 2001 From: eynard Date: Thu, 6 Jan 2022 12:13:22 +0100 Subject: [PATCH] Accuracy pendant training --- sobek/network.py | 12 ++++++++++-- tests/MNISTDrawingPrediction.py | 11 +++++++---- tests/MNISTLearning.py | 19 ++++++++++++++++--- tests/testLearningNAND.py | 2 +- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/sobek/network.py b/sobek/network.py index dc6091c..8d02041 100755 --- a/sobek/network.py +++ b/sobek/network.py @@ -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): raise TypeError("The inputs and desired outputs must be lists of numpy arrays !") 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))) if (len(inputs) == 0): raise ValueError("The list is empty !") - if (visualize == False): + if (visualize != False): if (self.__inputLayerSize != 2): raise ValueError("Visualization is only possible for 2 inputs networks") if (len(self.weights[-1]) != 1): @@ -126,6 +126,14 @@ class network: errorSumsBiases[layerNumber] = np.multiply(errorSumsBiases[layerNumber], -(learningRate/len(inputBatch))) 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): ani = animation.ArtistAnimation(fig, vizualisationData, interval=100) plt.show() diff --git a/tests/MNISTDrawingPrediction.py b/tests/MNISTDrawingPrediction.py index 1d9e8b6..2a65a7c 100644 --- a/tests/MNISTDrawingPrediction.py +++ b/tests/MNISTDrawingPrediction.py @@ -14,16 +14,19 @@ class Sketchpad(tkinter.Canvas): self.bind("", self.add_line) self.PILImage = Image.new("F", (560, 560), 100) self.draw = ImageDraw.Draw(self.PILImage) - self.MNISTNN = network.networkFromFile("MNIST30epoch") + self.MNISTNN = network.networkFromFile("MNISTtest2") self.predictionLabel = predictionLabel def add_line(self, event): - self.create_oval((event.x+32, event.y+32, event.x-32, event.y-32), fill="black") - self.draw.ellipse([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-24, event.y-24, event.x+24, event.y+24], fill="black") smallerImage = self.PILImage.reduce(20) imageAsArray = np.array(smallerImage.getdata()) 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): self.PILImage = Image.new("F", (560, 560), 100) diff --git a/tests/MNISTLearning.py b/tests/MNISTLearning.py index f18bc7c..bac16f2 100644 --- a/tests/MNISTLearning.py +++ b/tests/MNISTLearning.py @@ -27,7 +27,20 @@ for label in tempTrainLabels: trainLabels.append(np.zeros(10)) 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 @@ -48,7 +61,7 @@ for i in range(1): 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() @@ -57,4 +70,4 @@ print("Learning time : " + str(endTime - startTime)) print(trainLabels[121]) print(myNetwork.process(trainImages[121])) -myNetwork.saveToFile("MNIST30epoch") \ No newline at end of file +myNetwork.saveToFile("MNISTtest3") \ No newline at end of file diff --git a/tests/testLearningNAND.py b/tests/testLearningNAND.py index f6795f6..f5c354a 100644 --- a/tests/testLearningNAND.py +++ b/tests/testLearningNAND.py @@ -35,7 +35,7 @@ for i in range(nbRep): 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() learningTime += endTime - startTime