# Error regarding shapes not aligned back propagation

75
February 10, 2019, at 3:30 PM

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...

POPULAR ONLINE

#### Deleting a cognito user as admin

READ ALSO ### Fullpage.js doesn't scroll in any browser (chrome, firefox, edge)

I've been trying to make a really simple website using fullpagejs but it just won't scroll a whole section like its supposed to

85 ### Unknown URL: content://media/external/audio/media/null?

I am creating a mp3player where all audio files are fetched from the local device storageI show the items in a ListView

62 ### Static Methods in Private class

I have recently started learning java and its my first OOP languageI read that static methods do not require the class to instantiated they run when you feed the class to the JVM

40 ### How to solve Parameter name must conform to the pattern ^accounts/[^/]+/locations/[^/]+\$?

I am trying to deal with GMB API but unable to fetch my location data as it gives error on location Name Location Link

57