From efec6fffad2adc2020e997c96f5f6b79c2f6439d Mon Sep 17 00:00:00 2001 From: fwe Date: Thu, 3 Dec 2020 10:37:41 +0100 Subject: [PATCH] add a plotter and modify network 1 to use it --- MyPlotter.py | 24 ++++++++++++++++++++++++ demo1.py | 13 +++++++++++++ network.py | 27 +++++++++++++++++++++++---- test.py | 27 ++++++++++++++++----------- 4 files changed, 76 insertions(+), 15 deletions(-) create mode 100644 MyPlotter.py create mode 100644 demo1.py diff --git a/MyPlotter.py b/MyPlotter.py new file mode 100644 index 0000000..e916323 --- /dev/null +++ b/MyPlotter.py @@ -0,0 +1,24 @@ +from numpy import asarray +from matplotlib import pyplot +import numpy as np +from matplotlib.pyplot import plot, draw, show + + +class MyPloter(object): + def __init__(self, framesize, max_rows, max_columns): + self.__data_array = np.zeros((framesize * max_rows, framesize * max_columns)) + self.__framesize = framesize + self.__max_rows = max_rows + self.__max_columns = max_columns + pyplot.ion() + + + def add(self, pic, row, column): + if row >= self.__max_rows or column >= self.__max_columns: + return + self.__data_array[row*self.__framesize:(row+1)*self.__framesize, column *self.__framesize:(column+1)*self.__framesize] = pic + + def show(self): + pyplot.imshow(self.__data_array , cmap='gray', vmin=0, vmax=255) + pyplot.show() + pyplot.pause(0.001) \ No newline at end of file diff --git a/demo1.py b/demo1.py new file mode 100644 index 0000000..a61fe7e --- /dev/null +++ b/demo1.py @@ -0,0 +1,13 @@ +import mnist_loader + +training_data, validation_data, test_data = mnist_loader.load_data_wrapper() +training_data = list(training_data) + +# --------------------- +# - network.py example: +import network + + +net = network.Network([784, 28, 10]) +net.SGD(training_data, 20, 10, 3.0, test_data=test_data) +input("any key to continue") \ No newline at end of file diff --git a/network.py b/network.py index ad9f26e..4e804cd 100644 --- a/network.py +++ b/network.py @@ -16,9 +16,13 @@ # Standard library import random + + # Third-party libraries import numpy as np +import MyPlotter + class Network(object): def __init__(self, sizes): @@ -35,8 +39,8 @@ def __init__(self, sizes): self.num_layers = len(sizes) self.sizes = sizes self.biases = [np.random.randn(y, 1) for y in sizes[1:]] - self.weights = [np.random.randn(y, x) - for x, y in zip(sizes[:-1], sizes[1:])] + self.weights = [np.random.randn(y, x) for x, y in zip(sizes[:-1], sizes[1:])] + self.plotter = MyPlotter.MyPloter(28, 20, 20) def feedforward(self, a): """Return the output of the network if ``a`` is input.""" @@ -70,7 +74,7 @@ def SGD(self, training_data, epochs, mini_batch_size, eta, for mini_batch in mini_batches: self.update_mini_batch(mini_batch, eta) if test_data: - print("Epoch {} : {} / {}".format(j,self.evaluate(test_data),n_test)); + print("Epoch {} : {} / {}".format(j,self.evaluate(test_data, j),n_test)) else: print("Epoch {} complete".format(j)) @@ -125,13 +129,28 @@ def backprop(self, x, y): nabla_w[-l] = np.dot(delta, activations[-l-1].transpose()) return (nabla_b, nabla_w) - def evaluate(self, test_data): + def evaluate(self, test_data, epoch): """Return the number of test inputs for which the neural network outputs the correct result. Note that the neural network's output is assumed to be the index of whichever neuron in the final layer has the highest activation.""" test_results = [(np.argmax(self.feedforward(x)), y) for (x, y) in test_data] + + count = 0 + for result, data in zip(test_results, test_data): + if count == 30: + break + (x,y) = result + if x != y: + (a,_) = data + a = a.reshape(28,28) + self.plotter.add(a*255.0, epoch, count) + count += 1 + self.plotter.show() + + + return sum(int(x == y) for (x, y) in test_results) def cost_derivative(self, output_activations, y): diff --git a/test.py b/test.py index d1f4996..67bddc2 100644 --- a/test.py +++ b/test.py @@ -17,19 +17,22 @@ # ---------------------- # - read the input data: -''' + + import mnist_loader + training_data, validation_data, test_data = mnist_loader.load_data_wrapper() training_data = list(training_data) -''' + # --------------------- # - network.py example: -#import network +import network -''' -net = network.Network([784, 30, 10]) + +net = network.Network([784, 28, 10]) net.SGD(training_data, 30, 10, 3.0, test_data=test_data) -''' + + # ---------------------- # - network2.py example: @@ -148,18 +151,18 @@ def testTheano(): else: print('Used the gpu') # Perform check: -#testTheano() +# testTheano() # ---------------------- # - network3.py example: -import network3 -from network3 import Network, ConvPoolLayer, FullyConnectedLayer, SoftmaxLayer # softmax plus log-likelihood cost is more common in modern image classification networks. +#import network3 +#from network3 import Network, ConvPoolLayer, FullyConnectedLayer, SoftmaxLayer # softmax plus log-likelihood cost is more common in modern image classification networks. # read data: -training_data, validation_data, test_data = network3.load_data_shared() +#training_data, validation_data, test_data = network3.load_data_shared() # mini-batch size: -mini_batch_size = 10 +#mini_batch_size = 10 # chapter 6 - shallow architecture using just a single hidden layer, containing 100 hidden neurons. ''' @@ -195,6 +198,7 @@ def testTheano(): ''' # chapter 6 - rectified linear units and some l2 regularization (lmbda=0.1) => even better accuracy +''' from network3 import ReLU net = Network([ ConvPoolLayer(image_shape=(mini_batch_size, 1, 28, 28), @@ -208,3 +212,4 @@ def testTheano(): FullyConnectedLayer(n_in=40*4*4, n_out=100, activation_fn=ReLU), SoftmaxLayer(n_in=100, n_out=10)], mini_batch_size) net.SGD(training_data, 60, mini_batch_size, 0.03, validation_data, test_data, lmbda=0.1) +''' \ No newline at end of file