Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

np.dot(w, activation) throws an error in backprop(x, y) #29

Open
xXCoolinXx opened this issue May 20, 2020 · 6 comments
Open

np.dot(w, activation) throws an error in backprop(x, y) #29

xXCoolinXx opened this issue May 20, 2020 · 6 comments

Comments

@xXCoolinXx
Copy link

xXCoolinXx commented May 20, 2020

This code

for b, w in zip(self.biases, self.weights):
            z = np.dot(w, activation)+b
            zs.append(z)
            activation = sigmoid(z)
            activations.append(activation)

throws the following error, and I don't really know why. (I am using Python 3.8.3 and numpy 1.18.4)

ValueError: operands could not be broadcast together with shapes (10,784) (10,30)

@xXCoolinXx
Copy link
Author

Note also that VS Code says that the shapes of w and activation were (10, 30) and (30, 784) respectively. This seems to be different from the shapes in the error.

@Willt125
Copy link

Willt125 commented Jun 12, 2020

I can confirm on Python 3.8.2 with Numpy 1.18.3 that this error occurs (albeit with recorded sizes of (30, 1) and (10, 1)), with both the 2.x and 3.x versions of the code.

The line in question is line 123 of network.py.

@doublethink13
Copy link

me too! any fixes?

@miles-1
Copy link

miles-1 commented Aug 17, 2020

I'm late to the game, but I'm curious for other people if they were using the load_data function instead of the load_data_wrapper function from the mnist_loader file.

I'd be surprised if others made the same mistake that I did, but I thought I had the same problem in this ticket for a while until I realized my mistake. That's what was going wrong for me.

I just think a problem big enough that the vectors can't match for dot products is a good indicator that it's probably a math logic problem and not a python/numpy version issue.

I'm using Python 3.6.9 and Numpy 1.19.1

@hamolicious
Copy link
Contributor

I'm late to the game, but I'm curious for other people if they were using the load_data function instead of the load_data_wrapper function from the mnist_loader file.

training_data, validation_data, test_data = load_data() returns
ValueError: shapes (16,2296) and (50000,784) not aligned: 2296 (dim 1) != 50000 (dim 0)
and
training_data, validation_data, test_data = load_data_wrapper() returns
ValueError: setting an array element with a sequence.

I also wrote my own function to load and format the data and that throws
ValueError: shapes (16,2296) and (784,) not aligned: 2296 (dim 1) != 784 (dim 0)

All 3 are complaining about line 101 (z = np.dot(w, activation) + b)
and I can't seem to find the issue with either?!

@yc-cui
Copy link

yc-cui commented Nov 15, 2021

fixed.
change
np.dot(delta, activations[-2].transpose())
to
np.outer(delta, activations[-2].transpose())

The shape of the matrices are (a, ) and (b, ), respectively. When we use np.dot while the matrices are 1d both, it cannot give us a result of axb. Use np.outer instead to get what we want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants
@Willt125 @MichalDanielDobrzanski @miles-1 @hamolicious @doublethink13 @xXCoolinXx @yc-cui and others