python - numpy ValueError shapes not aligned -
so trying adapt neural network michael nielson's http://neuralnetworksanddeeplearning.com/chap1.html
i modified network.py
work on python 3 , made small script test few 15x10 pictures of digits.
import os import numpy np network import network pil import image black = 0 white = 255 cdir = "cells" cells = [] cell in os.listdir(cdir): img = image.open(os.path.join(cdir,cell)) number = cell.split(".")[0][-1] pixels = img.load() pdata = [] x in range(img.width): y in range(img.height): pdata.append(1 if pixels[x,y] == white else 0) cells.append((np.array(pdata), int(number))) net = network([150,30,10]) net.sgd(cells,100,1,3.0,cells)
however have error:
file "network.py", line 117, in backprop nabla_w[-l] = np.dot(delta, activations[-l-1].transpose()) valueerror: shapes (30,30) , (150,) not aligned: 30 (dim 1) != 150 (dim 0)
i tried boolean , without problem, seems issue numpy on python 3 incompatible python 2.7 ?
edit: tried boolean , different number of neurones number hidden layer , input layer , fails
edit2:it not work on python 2.7 here modified network.py
import random import numpy np class network(object): def __init__(self, sizes): """the list ``sizes`` contains number of neurons in respective layers of network. example, if list [2, 3, 1] three-layer network, first layer containing 2 neurons, second layer 3 neurons, , third layer 1 neuron. biases , weights network initialized randomly, using gaussian distribution mean 0, , variance 1. note first layer assumed input layer, , convention won't set biases neurons, since biases ever used in computing outputs later layers.""" self.num_layers = len(sizes) self.sizes = sizes self.biases = [np.random.randn(y, 1) y in sizes[1:]] self.weights = [np.random.randn(y, x) x, y in zip(sizes[:-1], sizes[1:])] def feedforward(self, a): """return output of network if ``a`` input.""" b, w in zip(self.biases, self.weights): = sigmoid(np.dot(w, a)+b) return def sgd(self, training_data, epochs, mini_batch_size, eta, test_data=none): """train neural network using mini-batch stochastic gradient descent. ``training_data`` list of tuples ``(x, y)`` representing training inputs , desired outputs. other non-optional parameters self-explanatory. if ``test_data`` provided network evaluated against test data after each epoch, , partial progress printed out. useful tracking progress, slows things down substantially.""" if test_data: n_test = len(test_data) n = len(training_data) j in range(epochs): random.shuffle(training_data) mini_batches = [ training_data[k:k+mini_batch_size] k in range(0, n, mini_batch_size)] mini_batch in mini_batches: self.update_mini_batch(mini_batch, eta) if test_data: print("epoch {0}: {1} / {2}".format( j, self.evaluate(test_data), n_test)) else: print("epoch {0} complete".format(j)) def update_mini_batch(self, mini_batch, eta): """update network's weights , biases applying gradient descent using backpropagation single mini batch. ``mini_batch`` list of tuples ``(x, y)``, , ``eta`` learning rate.""" nabla_b = [np.zeros(b.shape) b in self.biases] nabla_w = [np.zeros(w.shape) w in self.weights] x, y in mini_batch: delta_nabla_b, delta_nabla_w = self.backprop(x, y) nabla_b = [nb+dnb nb, dnb in zip(nabla_b, delta_nabla_b)] nabla_w = [nw+dnw nw, dnw in zip(nabla_w, delta_nabla_w)] self.weights = [w-(eta/len(mini_batch))*nw w, nw in zip(self.weights, nabla_w)] self.biases = [b-(eta/len(mini_batch))*nb b, nb in zip(self.biases, nabla_b)] def backprop(self, x, y): """return tuple ``(nabla_b, nabla_w)`` representing gradient cost function c_x. ``nabla_b`` , ``nabla_w`` layer-by-layer lists of numpy arrays, similar ``self.biases`` , ``self.weights``.""" nabla_b = [np.zeros(b.shape) b in self.biases] nabla_w = [np.zeros(w.shape) w in self.weights] # feedforward activation = x activations = [x] # list store activations, layer layer zs = [] # list store z vectors, layer layer b, w in zip(self.biases, self.weights): z = np.dot(w, activation)+b zs.append(z) activation = sigmoid(z) activations.append(activation) # backward pass delta = self.cost_derivative(activations[-1], y) * \ sigmoid_prime(zs[-1]) nabla_b[-1] = delta nabla_w[-1] = np.dot(delta, activations[-2].transpose()) # note variable l in loop below used little # differently notation in chapter 2 of book. here, # l = 1 means last layer of neurons, l = 2 # second-last layer, , on. it's renumbering of # scheme in book, used here take advantage of fact # python can use negative indices in lists. l in range(2, self.num_layers): z = zs[-l] sp = sigmoid_prime(z) delta = np.dot(self.weights[-l+1].transpose(), delta) * sp nabla_b[-l] = delta nabla_w[-l] = np.dot(delta, activations[-l-1].transpose()) return (nabla_b, nabla_w) def evaluate(self, test_data): """return number of test inputs neural network outputs correct result. note neural network's output assumed index of whichever neuron in final layer has highest activation.""" test_results = [(np.argmax(self.feedforward(x)), y) (x, y) in test_data] return sum(int(x == y) (x, y) in test_results) def cost_derivative(self, output_activations, y): """return vector of partial derivatives \partial c_x / \partial output activations.""" return (output_activations-y) #### miscellaneous functions def sigmoid(z): """the sigmoid function.""" return 1.0/(1.0+np.exp(-z)) def sigmoid_prime(z): """derivative of sigmoid function.""" return sigmoid(z)*(1-sigmoid(z))
ok, found bug, had reshape data
cells.append(np.reshape((np.array(pdata),(150,1)), int(number)))
seems array
dimensions (x,1) , (x,) treated differently numpy
during computations
Comments
Post a Comment