W2_A2_Ex-8_Model_Test_Value Error

ValueError: cannot reshape array of size 16 into shape (4,1)

Hi @Sarvajna_B_V_Acharya

Which exercise are you referring to ?

Week 2 Logistic Regression with Neural Network Mindset

Hi @Sarvajna_B_V_Acharya ,

There are a few exercises in that lab, which one are you referring to? eg. exercise1, or 2, or etc…

This one

Hi @Kic,

All the functions defined independently are working fine. But when compiled into a master function ‘model’ , it is throwing a Value error while referencing to the ‘predict’ function.

Can you please help me out?

Hello Sarvajna,

Did you get the predictions right or you are still facing the issue? Please share us the traceback error that you are receiving.


Hi Rashmi,

This is what I am getting

In More Detail

Ok, that says that the w value that you are passing down to predict is the wrong shape. It should be 4 x 1, but it’s not. So what shape is it? Put this statement right before the call to predict:

print(f"w.shape = {w.shape}")

What does that show? Then you have to figure out how it turned out wrong.

The bug is most likely somewhere in the model logic, not in optimize or propagate.

This is how debugging works: you have to start and reason from the point of the error, but realize that the actual mistake may not be in the function that throws the error. Note that predict doesn’t do anything to change w, right? So the value that was passed in must be wrong.

Tried the solution. It is not working.

It is giving the same error.

What I gave you was not a solution: it was a way to get more information about where the problem is. So what did those print statements show you?

Hello Sarvajna,

Are you hard-coding the variables while calling the function for the model?

The d[‘costs’] asks about a list, which is not able to be retrieved within the model. Why is it happening? Please have a look over it again.

Note that it’s not the line the references d['cost'] that is “throwing”: it never gets there. The problem is that the w value being passed down to predict is wrong somehow. I would put those “print shape” statements after the initialize call and in the optimize code right before the return statement in addition to the one right before the call to predict. The bug is most likely in model. My guess would be that they are not correctly retrieving the w value that was returned by optimize: note that it needs to be extracted from the dictionary containing the parameters that is returned by optimize.

It shows w.shape to be [4,4]
And it is giving a right error that it cannot be reshaped into a [4,1] array.
I am not able to find the error iin the code as I have entered the code based on the instructions provided in the workbook.

The point where I am stuck is that although the individual function works perfectly independently it suddenly stops working when it is integrated into the model.

Also here is another assertion error that is thrown while using initialise with zeroes.

Hello Sarvajna,

The assertion error says that type(b) should be a float, which means the value should be in decimals. Writing 0 and 0.0 makes a difference.

Thank you Rashmi.

In this example of initialise with zeroes, this is what I have coded

b = np.zeros((dim, 1))

After that there is a hard code, while calling the function, which I am not able to change, which adds the below code after the function is called

assert type(b) == float

And then it throws the error

I am quite puzzled by this.

Hello Sarvajna,

You have to initialize w as a vector of zeros. The expression that you are putting is for w and not b.
In my previous reply, I have already mentioned that you need to put a float value for b.

Please go through the instructions again.

def initialize_with_zeros(dim):
    This function creates a vector of zeros of shape (dim, 1) for w and initializes b to 0.

Thank you Rashmi.

The solution worked.

I have completed the assignment.

Thank you so much