C1_W2_A2_Ex8: costs

Hi - count me as the one of many on this forum stuck on an assignment but not at all sure how to fix it. I’m also not sure how I am supposed to ask for help with my code if I’m not supposed to post the code on the discussion board? I’m pretty new to Python, so I literally have no idea how to even begin to troubleshoot.

Anyhow, Exercise 8: I define w, b by calling initialize_with_zeros, using the X.shape function as the input, then define w = and b = by pulling it from params, then define params, grads, costs using the optimize function. My prior code blocks passed the checks, but when I run this, I get the following traceback (sorry if this counts as posting code?):

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

~/work/release/W2A2/public_tests.py in model_test(target)
    123     y_test = np.array([[0, 1, 0]])
    124 
--> 125     d = target(X, Y, x_test, y_test, num_iterations=50, learning_rate=0.01)
    126 
    127     assert type(d['costs']) == list, f"Wrong type for d['costs']. {type(d['costs'])} != list"

<ipython-input-65-f69fe946eed8> in model(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, print_cost)
     37 
     38     w,b = initialize_with_zeros(X_train.shape[1])
---> 39     params, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)
     40 
     41     w = params["w"]

<ipython-input-63-4a9ba1de2c64> in optimize(w, b, X, Y, num_iterations, learning_rate, print_cost)
     36         # YOUR CODE STARTS HERE
     37 
---> 38         grads, cost = propagate(w,b,X,Y)
     39 
     40         # YOUR CODE ENDS HERE

<ipython-input-50-43f88b84d918> in propagate(w, b, X, Y)
     32     # YOUR CODE STARTS HERE
     33 
---> 34     A = sigmoid(np.dot(w.T,X) + b)
     35     cost = np.sum(np.dot(Y,np.log(A.T)) + np.dot((1-Y),np.log(1-A.T)))*(-1/m)
     36 

<__array_function__ internals> in dot(*args, **kwargs)

ValueError: shapes (1,7) and (4,7) not aligned: 7 (dim 1) != 4 (dim 0)

It looks like there’s something wrong with the np.dot(w.T, X) step but I genuinely have no idea what I did wrong.

Sorry if this is obvious to everyone but me. : P

Your w value is the wrong shape. In that particular test case, the X input matrix is 4 x 7. That means there are 4 features (n_x = 4) and 7 samples (m = 7). So what should the shape of w be in that case? Why did it turn out incorrectly?

The error is thrown in propagate, but that doesn’t mean that’s where the bug is. Where is the shape of w determined?

BTW it is fine to show exception traces as you’ve done above. It gives us enough information to reason about the problem.

Thank you so much!!! I’d simply used the wrong axis when I called “initialize_with_zeros()” in the model. :person_facepalming: ..

1 Like