0

I am a newbie to machine learning and currently learning through Michael Nielsen's website... I am currently running code for handwritten digit recognition... The code is perfectly the same as given in website but I am facing an error in backpropagation function...

def backprop(self, x, y):
    """Return a tuple ``(nabla_b, nabla_w)`` representing the
    gradient for the cost function C_x.  ``nabla_b`` and
    ``nabla_w`` are layer-by-layer lists of numpy arrays, similar
    to ``self.biases`` and ``self.weights``."""
    nabla_b = [np.zeros(b.shape) for b in self.biases]
    nabla_w = [np.zeros(w.shape) for w in self.weights]
    # feedforward
    activation = x
    activations = [x]  # list to store all the activations, layer by layer
    zs = []  # list to store all the z vectors, layer by layer
    for 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 that the variable l in the loop below is used a little
    # differently to the notation in Chapter 2 of the book.  Here,
    # l = 1 means the last layer of neurons, l = 2 is the
    # second-last layer, and so on.  It's a renumbering of the
    # scheme in the book, used here to take advantage of the fact
    # that Python can use negative indices in lists.
    for l in xrange(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)

In above code error is in following line:

z = np.dot(w, activation) + b

Error is:

ValueError: shapes (784,30) and (784,1) not aligned: 30 (dim 1) != 784 (dim 0)

I understand that dimensions are aligned for dot product but taking the transpose of w to give further complications in code although solves this line of code Please help...

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Try printing each and every shape for the arrays you use. The problem is in incorrect shapes for + or .dot. And what you can easily do is just write down what shapes you need and check with the code. – sooobus Feb 10 '19 at 16:17
  • Check this link He has explained it clearly. [check this](https://stackoverflow.com/questions/47844093/matrix-dimensions-not-matching-in-back-propagation) – Veer May 07 '19 at 10:43

0 Answers0