C1_W3_Assignment - Forward Propagation (Single Perceptron Neural Networks for Linear Regression)

I keep getting this error in the forward propagation despite the inputs dimensions are set as per instructions and are valid (passed all the previous tests):

{moderator edit: code removed}

If I re-write that function in the non-vectorized form (which I am pretty sure it is wrong) I pass that test but I get a dimension error afterwards:

{moderator edit: code removed}

My inputs dimensions are:
Size of input layer:
n_x = (X.shape[0])
Size of output layer:
n_y = (Y.shape[0])
Weights:
W = np.random.randn(n_y, n_x) * 0.01
Bias:
b = np.zeros((n_y, 1))

I understand there is definitely a matrix dimensions problem but as soon as I try changing them in the sections above I start failing the other tests.

Has anyone had the same problem?

1 Like

Yes, I am having the same problem. Cannot seem to find the solution

1 Like

(UPDATE)

I just solved it. Turns out, I did not use np.dot() correctly,

First parameter should be W and the second should be X like this np.dot(W, X).
Alternatively, you can also use W @ X. Hope this helps.

11 Likes

This worked. Thank you so much!
Easy solution, which was actually under my eyes (it is in the formula above that code cell) but which I could not see!
Cheers

2 Likes

Yes, np.dot should have more than 1 argument

1 Like

I am stuck at the 1st exercise itself. It says ‘There was a problem compiling the code from your notebook. Details:
‘NoneType’ object has no attribute ‘shape’’

I am using np.shape(X) but still getting this. X.shape() also did not solve it.

1 Like

thanks a lot, it solved my issue in code. I put multiplication instead!

1 Like

Life saver, thank you!

1 Like

The .shape command doesnt have parenthesis, try using X.shape[1] to get the (,m) size. X.shape[0] provides the n_x. Hope this helps

1 Like

Thanks so much my problem is solved. One quick question though…I used np.multiply and it’s wrong. Now I understand it would function same as np.dot?

1 Like

np.multiply does an element-wise multiplication operation while np.dot does a dot-product.

1 Like

please use np.dot(W,X) , exactly in that order, not X,W because the number of columns of W must match the number of rows of X for the dot product to be valid.

np.multiply() performs element-wise multiplication (not dot operation), which might work in certain cases. that’s why when the test runs different cases, let’s say the shape of X and W are changed to (1,2), (2,5) then element-wise won’t work, only dot product works. Thus, we fail the test.

np.dot(W,X) will work in all cases, regardless the number of rows and columns of W, X as long as we follow our original setup W(n_y,n_x), X (n_x,m) because:
(number of columns of W = number of rows of X = n_x ) is always true, hence it is a valid dot product.

It took me like 2 hours to learn the difference between element-wise multiplication and dot product lol. The key point is, they are similar but depending on the use case. In ML, dot product is preferred to achieve certain goals like the shape of the result matrix should be compatible to the next operation ( in this case, plus b, which in turn will result in z and y-hat)

1 Like

does anyone know how to resolve this error


in exercise 4 .

2 Likes

This assignment was updated on November 8th. I think you are using the previous version of the notebook.

You need to update to the current version of the notebook, and you also need the new version of the w3_unittest.py file.

Instructions for this are in the “M4ML Resources FAQ”. You can find it by using the forum Search tool for that text string.

hello, did you know how to solve the problem?

1 Like

I think starting from the newest version of the notebook will fix the issue.

1 Like

how to do that?

1 Like

@Shuruq_AlQurashi, see my previous reply on this thread from 3 days ago.

1 Like

sorry i didn’t see it, thanks!

1 Like