Week 2 Excecise 8 ValueError

Hi. I get “ValueError: cannot reshape array of size 16 into shape (4,1)” and i cant find the mistake. Thanks in advance.

{moderator edit - solution code removed}

the error i get:

ValueError                                Traceback (most recent call last)
<ipython-input-20-7f17a31b22cb> in <module>
----> 1 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
    109     y_test = np.array([1, 0, 1])
--> 111     d = target(X, Y, x_test, y_test, num_iterations=50, learning_rate=1e-4)
    113     assert type(d['costs']) == list, f"Wrong type for d['costs']. {type(d['costs'])} != list"

<ipython-input-19-3fd161463d46> in model(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, print_cost)
      7     w=params['w']
      8     b=params['b']
----> 9     Y_prediction_test = predict(w, b, X_test)
     10     Y_prediction_train = predict(w, b, X_train)
     11     if print_cost:

<ipython-input-17-e2cb49b1cb55> 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)
     20     # Compute vector "A" predicting the probabilities of a cat being present in the picture

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

and the function predict that i am using:

{moderator edit - solution code removed}

And the full notebook:

what is the shape of your X_train and X_test

They are only for the function. Any shape.

but i could add:

train_set_x shape: (12288, 209)
train_set_y shape: (1, 209)
test_set_x shape: (12288, 50)
test_set_y shape: (1, 50)

I am not sure but I could see the X_train.shape[0] to be 16. is it the same for the X_test for which you are running. Just wanted to make sure of that, thats why I asked for the shape of X using were using while running

problem is, makes no sense that the shape is (16,1) in first place. Should be way larger. Today i spent long time with this, maybe later i could continue.

Not all the test cases use the full data. Note that your code for initializing w and b is wrong: for starters, you should be calling the initialization routine that you wrote earlier. But even if you decide to write that again here, you’ve written it incorrectly so that w will end up being a 1D array. Print the shape and you’ll see. The easier fix is to call the correct routine.

There are other bugs in your implementation. Notice that you are not passing the learning rate or number of iterations to optimize, so you will be getting the default values for those regardless of what is passed in to model at the top level. That will not end well.

Also note that it’s not supposed to be our job to debug your code for you. That’s your job. :nerd_face:

1 Like


Sorry, i am no trying to avoid my job! :innocent:
But i was stucked.