Course 1- week 2- Exercise 8- model

Hi
I am getting the following error with my code.
can you please give some hint?

{moderator edit - solution code removed}

ValueError Traceback (most recent call last)
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)
50 b = params[“b”]
51
—> 52 Y_prediction_test = predict(w, b, X_test)
53 Y_prediction_train = predict(w, b, X_train)
54

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)

You have misspelled the first return value for the optimize function as prams instead of params. That will not end well. :nerd_face:

1 Like

Hello,
I am having the sa=omewhat similar issue and I have correctly defined the value of params.

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-30-9408a3dffbf6> in <module>
      1 from public_tests import *
      2 
----> 3 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
    120 
    121     assert type(d['w']) == np.ndarray, f"Wrong type for d['w']. {type(d['w'])} != np.ndarray"
--> 122     assert d['w'].shape == (X.shape[0], 1), f"Wrong shape for d['w']. {d['w'].shape} != {(X.shape[0], 1)}"
    123     assert np.allclose(d['w'], expected_output['w']), f"Wrong values for d['w']. {d['w']} != {expected_output['w']}"
    124 

AssertionError: Wrong shape for d['w']. (2, 1) != (4, 1)

I understand that there is some issue with the shape of w. I defined the value of w as:

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

That error probably means you are referencing global variables instead of the parameters passed to the model function. For starters, you don’t need to call np.zeros in model, because you already wrote the function initialize_with_zeros and you can just call that. But passing dim is a mistake, unless you have set dim to the number of rows of the matrix X_train, which is the relevant local variable in the model function.

So should I pass var dim as shape/ size of X_train? But this would mean that I would still have a mismatch in the dimensions when I calculate A.

Also when I pass that value as array like X_train[0]. I still get an error.

TypeError: only integer scalar arrays can be converted to a scalar index

which I can’t do as the X_train is not scalar.

The point is that you need to pass the number of rows of X_train to initialize_with_zeros. So how do you do that? Hint: use the “shape” attribute of X_train, not X_train itself.

2 Likes