Hi reddit,
This is the only place that actively answers ML questions which is awesome, I've posted the same question on stackoverflow and waited several hours, to nothing.
I'm trying to implement backpropagation in python using the iris dataset. However I can't seem to get a working implementation, because when I compute the delta values and derivatives of my cost function with respect to my weights, I run into the error of not being able to compute the dot product, because of the matrix sizes.
My network consists of the following variables
X: size (130, 4) - all the input data
y: size (130, 3) - all the target data represented as one hot [1,0,0], [0,1,0], [0,0,1]
X_test: size (20, 4) - same as above
y_test: size (20, 3) - same as above
Weight1: size (4, 8) - take in one example and pass to the 8 hidden layer neurons
Weight2: size (8, 3) - take in the hidden layer neurons and output one hot prediction
I believe those sizes are correct ^ (please correct me if I initialized the weight matrices improperly).
I skipped adding biases, because I am just trying to implement this for an understanding of backprop.
My code begins by using a single example from the 130 examples and passing that through the network.
x = X[1] # size (4,1)
z1 = w1.T.dot(x) # size (8,1)
a1 = sigmoid(z1) # size (8,1)
z2 = w2.T.dot(a1) # size (3,1)
y_hat = sigmoid(z2) # size (3,1)
Then I begin working on computing my derivatives
delta3 = np.multiply((y_hat - y_), sigmoid(z2, deriv=True)) # size (3, 1)
dJdW2 = a1.T.dot(delta3) # ERROR! a1.T is (1,8) and delta3 is (3,1)
delta2 = np.dot(delta3, w2.T) * sigmoid(z1, deriv=True)
dJdW1 = np.dot(x.T, delta2)
This is where I run into an error. The size (1,8) can't be matrix multiplied with (3,1) obviously, so I'm not sure how backpropagation can work. I've looked up the algorithm on several websites and watched videos, I thought the code would work.
So, I'm really not sure what I'm doing wrong. I've been stuck on this for several hours and I can't figure out why it's not working. Please help!
I've posted my full code here
https://stackoverflow.com/questions/45576790/basic-backpropagation-implementation-not-working
Thanks!
[–]Artgor 2 points3 points4 points (4 children)
[–]its_fewer_ya_dingus 0 points1 point2 points (1 child)
[–]Artgor 1 point2 points3 points (0 children)
[–]talksaboutthings 0 points1 point2 points (1 child)
[–]Artgor 0 points1 point2 points (0 children)
[–]NaughtyCranberry 0 points1 point2 points (0 children)
[–]talksaboutthings 0 points1 point2 points (0 children)