Week 2, exercise 8: ValueError: cannot reshape array of size 2 into shape (4,1)

Everything pass until exercise 8.
I don’t understand why it’s not working . Especially I didn’t touch X_train and w used into the predict function.

ERROR I get when running exercice 8:

in
1 from public_tests import *
2
----> 3 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
113 y_test = np.array([0, 1, 0])
114
→ 115 d = target(X, Y, x_test, y_test, num_iterations=50, learning_rate=0.01)
116
117 assert type(d[‘costs’]) == list, f"Wrong type for d[‘costs’]. {type(d[‘costs’])} != list"

in model(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, print_cost)
38
39 # YOUR CODE STARTS HERE
—> 40 Y_prediction_train = predict(w,b, X_train)
41 Y_prediction_test = predict(w,b, X_test)
42

in predict(w, b, X)
16 m = X.shape[1]
17 Y_prediction = np.zeros((1, m))
—> 18 w = w.reshape(X.shape[0], 1)
19
20 # Compute vector “A” predicting the probabilities of a cat being present in the picture

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

Hi @Kulssaka , difficult to judge from what you posted. The calculation of Y_prediction_train and Y_prediction_test seems okay so if previous exercises were passed then the error might be in the gradient descent step of the code. Suggest to have a look at that first. Hope this helps.

1 Like

I already checked each step one by one even if test were passing. I spent around 4 hours but didn’t find :frowning:

My code for gradient descent is simple as it should be.

Blockquote
for i in range(num_iterations):
# (≈ 1 lines of code)
# Cost and gradient calculation
# grads, cost = …
# YOUR CODE STARTS HERE
grads , cost = propagate(w, b, X, Y)

    # YOUR CODE ENDS HERE
    
    # Retrieve derivatives from grads
    dw = grads["dw"]
    db = grads["db"]
    
    # update rule (≈ 2 lines of code)
    # w = ...
    # b = ...
    # YOUR CODE STARTS HERE
    w = w - learning_rate * dw
    b = b - learning_rate * db
    # YOUR CODE ENDS HERE

Blockquote

The type of error you show typically means that you are referencing global variables someplace in your model code instead of the parameters that are actually passed in.

2 Likes

I used X_train and X_test, I don’t see any global variable I didnt edit code outside codeblock, except for prints.
I will check once more.

Where is there any object here which is a 1D array with shape (2,)? That is what the error message is saying about your w value. So how did that happen?

If no errors were thrown during the call to optimize, then your error probably happens after the return from optimize.

Print the shape of w before you call predict. Why is it (2,) instead of (4,1)?

when I print values as follows:
m = X.shape[1]
Y_prediction = np.zeros((1, m))
print(“X.shape =” + str(X.shape))
print(“Y_prediction.shape =” + str(Y_prediction.shape))
print(“W.shape =” + str(w.shape))
w = w.reshape(X.shape[0], 1)

I get:
X.shape =(4, 3)
Y_prediction.shape =(1, 3)
W.shape =(2, 1)

I found it was params that was wrong ! thanks for the help, I looked at right place after :slight_smile:

1 Like

Nice detective work! So it was a global variable after all, just not in the place you would have expected. :nerd_face:

Exactly haha hard to find this one !

Could you please explain what was wrong with params ? I’m receiving the same issue and it seems that params dictionary was defined in notebook so I haven’t amend it…

Hi, Sinem Ozmen.

Welcome to the community!

Please check the parameters, you are calling for optimize. They may be hard-wired or inconsistent.

With looking at the trace, one possibility is mixed-use of W and w. Unfortunately, w exists as a global value in the past, and the final shape is (2,1).
The another possibility is, typo for the first output from optimize(). There is a global variable “params” which is a dictionary that includes ‘w’. So if you get the output like “param”, and retrieve “w” from “params”, then, you will get different “w”, which also results in (2,1) shape.