Week 2 Exercise 8 Stuck

I’ve been staring at the error codes and read through a whole bunch of discussions for this exercise and still don’t understand what I should be doing/ what I’m doing wrong.

Please help me understand :pray:


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)
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"

in model(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, print_cost)
35 # YOUR CODE STARTS HERE
36 w, b=initialize_with_zeros(2)
—> 37 params, grads, costs = optimize(w,b,X_train,Y_train)
38 w = params[“w”]
39 b = params[“b”]

in optimize(w, b, X, Y, num_iterations, learning_rate, print_cost)
35 # grads, cost = …
36 # YOUR CODE STARTS HERE
—> 37 grads, cost = propagate(w,b,X,Y)
38
39 # YOUR CODE ENDS HERE

in propagate(w, b, X, Y)
30 # cost = …
31 # YOUR CODE STARTS HERE
—> 32 A = sigmoid(np.dot(w.T,X)+b)
33 cost = (-1/m)np.sum((Ynp.log(A))+(1-Y)*np.log(1-A))
34

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

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

I added some print statements to my model code and the shape X_train is indeed 4 x 7 for the test case in model_test. So then the question is why is your w value 2 x 1 instead of being 4 x 1? Where is the shape of w determined? On the first iteration it is determined by the call to the initialize routine, right? So if it fails on the very first iteration, that probably means you referenced a global variable for the dim value that you passed. If it happens on a later iteration, then it probably means something is wrong with your update parameters logic in optimize, but that probably would have been caught by the test cases for optimize. So I think the “bad initialize call” is the first thing to check.

1 Like

Thank you! I’m starting to get the hang of reading error codes.

Great! The first step in debugging is always to understand what the error message is telling you. In that case, you can see that it is complaining about the “inner” dimensions not matching, so it is the np.dot call in the sigmoid line that is causing the problem. Note that the error is thrown in propagate, but you already know your propagate code is correct and it doesn’t modify the shapes of any of the inputs, so the bug is not in propagate. The problem is that the w value you passed in is incorrect. So then you track backwards up the call stack to figure out why the shape of w is wrong. In this case, the bug is two levels up in the model function (at least if my theory is right).

2 Likes

This is an awesome tip, thank you!