W2 A2 | Exercise-8 | Error: Wrong shape for d['w']. (2, 1) != (4, 1)

Greetings, i’ve encoutered a problem where the shape of parameter ‘w’ in array ‘d’ returned from the model function is wrong. Attached below is the screenshot of the error. I’ve already passed all the test before this, I’ve tried to tune the parameters in the code but the error still there. I really cannot find where the bug is, hope anyone of you guys can help me. Thank you.

Hi @KCH1220 and welcome to the DLS specialization. A couple of things before we get going. First, d is not a (NumPy) array, but rather a Python dictionary, or dict for short. The string 'w' in d['w'] is a dict key, which access the value stored in that key-value pair–in this case the weight matrix w. That’s your array.

Second, I do not know exactly what you mean by “tune the parameters” but please don’t, unless explicitly instructed to do so. Your work should confined to the comment block # YOUR CODE STARTS HERE and # YOUR CODE ENDS HERE. Also, if you are changing (“hard-coding”) the values for the arguments of the functions embedded in the model function, that is wrong-headed.

Please conduct the following review of your code:

Study the “signatures” of all of your functions that help comprise the model() function. The signature of a function is like the following:

my_function(arg1, arg2, ..., kwarg1 = value1, kwarg2 = value2, ...)

Note that the argument values can be any number of Python object types: int, float, string, list, np.array, Boolean string, etc

  1. Check that you are using the arguments in your required code block. Do not “hard-code” a number or anything else. If you do, it may pass a test, but it will not generalize to other cases. If you are not using the arguments in the signature, it will be wrong.

  2. Note the signature of the model() function: nn_model(X_train, Y_train, n_h, num_iterations = 2000, learning_rate=0.5, print_cost=False). Naturally, the same applies here. Note, however, that you are filling out your contribution to the function with calls to functions that you have already helped to complete. So the calls to those functions must be consistent with the arguments to the new function.

Hey y’all. Hope you all doing great.
I have passed all the tests but keep receiving this error in ‘model’:

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

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

AssertionError: Wrong values for d['w']. [[ 0.14449502]
 [-0.1429235 ]
 [ 0.21265053]] != [[ 0.08639757]
 [ 0.12866053]]

I was wondering if the issue originates from my ‘optimize’ function and ‘params’ where I am tweaking ‘w, b’, or somewhere else. I passed X_train and Y_train as arguments for optimize function. The only thing I’ve done differently than advised by the notebook was defining ‘cost’ using ‘np.sum’ (cost = (-1/m) * np.sum(Y * np.log(A) + (1-Y) * np.log(1-A))) and not ‘np.dot’.
Would appreciate it if you could help me with this.

Welcome to the community.

I passed X_train and Y_train as arguments for optimize function.

There are three more parameters passed from a test program. Those are num_iterations, learning_rate, and others. Have you passed those to optimize() as well ?

Please double check.

Thanks so much, dear Nobu_Asai. Yes, seems like a simple oversight cost me a couple of hours. The model works perfectly fine, now; thanks to you.
I do have one more question though. I had this issue with ‘np.dot’, and was wondering if you could help me with this, as well.

(-1/m) * (np.dot(Y, np.log(A)) + np.dot(np.log(1-Y), 1-A))

The error is shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0).

It’s very close.
Dot product works for two matrices (n,m), (m,s) to create (n,s) matrix. In your case, the shape of Y is (1,3) and the shape of A is (1,3). So you need to “transpose” “A” to create (3,1) matrix.

By the way, please see below. log should be applied to “A”, not"Y". :slight_smile:

J = -\frac{1}{m}\sum_{i=1}^{m}(y^{(i)}\log(a^{(i)})+(1-y^{(i)})\log(1-a^{(i)}))

Thanks a lot, dear Nobu_Asai for your help. :blush:
I honestly have no idea why it shows that np.log is applied to Y in this piece of code up here but I double-checked and it was correct in my notebook.
I tried ‘transpose’ before posting here, and there should have been something wrong with the way I was implementing it, cuz I still got errors. I tried it again and it works now! Without even changing anything else, lol! Thanks a lot! Appreciate your help!