W2_A2_Ex-8_Value Error for w

I have passed all my tests except Exercise 8, it is showing a valueError for w, mentioning that “cannot reshape array of size 12 into shape (4,1)”, which refers to my predict function. I am not able to fix this issue, please help.


Welcome to the DL Specialization, @Mohit18. The ValueError here means that you are trying to reshape an array with 12 elements into one with only 4 elements–not going to work. The shape of X is determined by the test, so it must be that your weight matrix w is at fault. Maybe you are getting a bad input for w in your function?


Exactly. If the previous parts of model worked, then one place to look is where you got the w value from. Note that you are referencing the dictionary params. Is that the variable you used to store the output from the optimize?


Thank you for the reply,
Fixed the problem, my code had 2 bugs, the parameters passed in optimize function were wrong and had not correctly initialised w.


I got similar error //shapes (1,2) and (4,7) not aligned: 2 (dim 1) != 4 (dim 0)//, refers to the sigmoid(…) on propagate function , I guess the problem is on dot multiplication between w.T and X yes m extracting w from params with (1,2) shape it should be (1,4) and i don’t know where’s the problem


If your propagate function passed the “unit test”, then that is not where the bug is, even though that is where the error is thrown. The bug is higher up the call stack, probably in your model function. So the question is: where are the shapes of w and X that get passed to propagate determined? Why are they wrong? You can start by looking at the dimensions of the test case. Click “File → Open” and have a look at the model_test function or just put a print statement early in your model function to show the shape of X_train and you’ll see that it is 4 x 7. So that means that w should be 4 x 1, right? So that when you take w^T, the dimensions will be correct for matrix multiplication. So we know that the shape of w is wrong and now you need to figure out why. Where is that shape determined? By the arguments you pass to the initialize routine, right? So why do you get the wrong value?


thanks , the problem of shape is now fixed after following your helpful instruction but I got the wrong value of w I don’t know why

thanks for the feedback

1 Like

If your optimize function passed the unit tests, then it is the same story as in the previous bug: the bug is in model. One common mistake is to “hard-code” the parameters that are passed from model to optimize. If you just “copy/paste” the function definition of optimize as the invocation of optimize, then that means you are ignoring the values of learning rate, number of iterations and print flag that are passed into the model function. That will cause failures like the one you are seeing. Defining a function is a completely different thing than calling a function, right?


Mr. Paul,
I wanna thank you for your educative feedback It really guided me to solve problems and to understand more the algorithm
I wanna help the other student so they don’t lose time like me:
be careful not to copy and past the unit test to the merged model as Mr. Paul said
second, be sure to update all functions when you are implementing them on the model function
if you are already given a value to a variable like learning_rate for example
just call it without specifying another value for it for example
def(…,learning rate = a ) :

function(…, learning_rate) and not
function(…,learning_rate = a) this was the source of error for me

happy coding!


Hi ,

In my case

{moderator edit - solution code removed}

The w array went in the shape (4,1) and output after optimization came as (4,4)
I traced it back to the computation of dw .

A (sigmoid function ) shape is coming as (4,7) during this optimization which is wrong .

Please guide .

1 Like

Why are you rewriting the code to initialize w and b “by hand” again here in the model function? You already wrote the function initialize_with_zeros to do that. Why not just call it? If you like doing more work than you need to, it should work to write the function over again here, but you have written it incorrectly this time: your b value will also be n_x x 1, right?

Z = w^T \cdot X + b

And A is the same shape as Z. If w is 4 x 1 and X is 4 x 7, then w^T \cdot X will be 1 x 7. But if you then add a 4 x 1 vector to it, you’ll end up with a 4 x 7 result because of “broadcasting”. b is supposed to be a scalar.

Also note that you’ve made the “hard-coding” mistake that was discussed earlier on this thread w.r.t. the print_cost flag that you are passing to optimize. I don’t think the grader cares, but your output won’t look the same in the training later.

1 Like

I have completed the initialize_with_zeros function and passed that step.
I am confused with the input for this function. What should be replaced with dim for this function in exercise 8? could you please help me?
non of X_train.shape[0] or X_train.shape[1] worked for me

1 Like

The value of dim to pass to the initialize routine is the number of rows of the “sample” matrix. That is X_train in this case, so that would be the first element of the “shape” attribute of that array. So the first of your choices should have worked. What makes you think that it failed?


Thanks for your response.
I am not sure what was the problem that I was getting an error regarding the dimension of the w. So I tried X_train.shape[1] which obviously did not work. That was the point that I got confused about what is wrong with this function that passed the previous steps but not working now. Anyways, I have rewrite everything from the scratch again an it is working now. However, I could not figure out what was the root cause of that error. Thanks again for your support.

1 Like

Hi everyone,

I did the previous 7 exercises of week 2, but I don’t understand the next error in the exercise 8:

Can you help me please??

1 Like

You passed the wrong value as the third argument to predict on the line in model that is the root of that exception trace.


Hi @paulinpaloalto ,

Yes i noticed it later and have fixed it.
Thanks for the heads up

1 Like

I am getting a error in exercise 8 in propogate function whereas the function itself had passed all the test cases! @paulinpaloalto @Ramtin_Azami @kenb

1 Like

I had figured out my mistake … It was great fun struggling :slightly_smiling_face:

HAPPPY LEARNING ! :slightly_smiling_face:

1 Like

Glad to hear you figured it out under your own power. It must have been a pretty creative mistake to end up with something of shape 1 x 48. The number 48 does not appear anywhere here. :nerd_face: