Thank you, had deleted the question because I cleared the error by using the initialize_with_zeros function instead of basically rewriting it in the model code. I think I understand your comment, but will probably find out if I do when if I get the error in the future, since I’m not really sure why this works now. Basically replaced
w= np.zeros(X_train.shape[0])
b = float(0)
with
w, b = initialize_with_zeros(X_train.shape[0])
Hi, Rich.
The reason your “hand-written” version fails is that you used the wrong syntax for np.zeros. Compare that to the code you wrote in initialize_with_zeros. See the difference? Your version will result in a 1D object, not a 2D object.
But the “global variables” mistake is illustrated in the earlier post on this thread from Abhishek. Look at the arguments that are passed to predict() in this post. It references the global variables w_optimize and b_optimize. That might work in the test case in the notebook, since those are the variables passed in at least one test case. But it will not work in general. As mentioned above, referencing global variables in the body of a function is almost always a mistake. At least here in these courses …
Hi all,
I have iterated through some of the same missteps around matching dimensions, and arrived at the posted traceback. Every other module passed, though the model has the attached traceback and I arrive 65% training accuracy later.
Notice that all your w values are zero in that failing assertion. That probably means that you did not extract the new values from the dictionary that you got back from calling optimize. The local variables w and b in the scope of the model function are not changed when you call optimize, right? You get the updated values back packaged in a dictionary with a completely different name. So the last time those local variables were updated was when you originally called the “init” routine, which is why they are still zero.
Here’s the comment in the template code that is relevant:
# Retrieve parameters w and b from dictionary "params"
# w = ...
# b = ...
Of course that only works if params is the name you used for the return value from the call to optimize.
The reason you get 65% train accuracy and 34% test accuracy is that with all zero coefficients, you always predict “not a cat”. It turns out that the train dataset has 65% “no” samples, but the test dataset has 66% yes samples.
Here’s a thread which plays some games with trying to rebalance the train and test datasets to see how that affects the result, although those experiments were run using the 4 layer model we develop in Week 4 of the course.
Fantastic, thank you for the prompt Paul. Now we are cooking with electricity 
Hey Paul,
Thanks for giving guidance, I finally got able to finish the assignment based on your advice. However, I still have not totally grasped what is the difference between these two cases:
Case 1:
some_dict = some_func1()
some_func2(some_dict['key1'], some_dict['key2'])
Case 2:
some_dict = sum_func3()
a, b = some_dict['key1'], some_dict['key2']
some_func4(a, b)
I know that the some_func2() in case 1 can change the some_dict directly but some_func4 in case 2 won’t change some_dict directly because it has received values totally independent from some_dict
However, in the assignment the function predict() doesn’t have any code that changes its input w, b rather than reshaping w so the next call of predict() will receive w in the desired shape at the beginning and calling one extra reshape on it. I don’t get why the first case doesn’t work in assignment but the second one works. I really appreciate if you clarify this matter to me !
The two pieces of code you show result in the same values being passed to the subsequent functions, but note that there is an important side effect of the second method: it defines a and b as local variables. If you do it the first way, that probably means that you neglected to reset the values of w and b (the local variables in the scope of the model function) to be the updated values returned in the dictionary from optimize. Notice what the value is that is returned by the model function: it is another dictionary, but it uses the local variables w and b to define the contents of that dictionary.
This is the same bug that I discussed in my earlier reply to Tim on this thread.
While we’re on the subject of “side effects” due to the way you call functions, here’s a thread which shows some other important python behaviors to be aware of. Please read the whole thread, not just the one linked post.
thanks i got the same mistake and i solve it using your answer
Hi, sorry if it’s an obvious mistake. I managed the testing of the model function. In the next step of logistic_regression_model step, I’m getting error of name “train_set_x” is not defined. Perhaps help me understand what this error is about?
Kind regards,
Gaofeng
Where is train_set_x defined? Use the browser “search” to find all instances of it. It is created by one of the earlier cells in the notebook, right? Are you sure that you have executed that cell since the last time you closed and reopened the notebook or restarted the kernel? There is a topic about variables not being defined on the FAQ Thread. Please have a look and see if that is the cause.
When I run the Exercise 8 I am getting an w error:
AssertionError: Wrong values for d[‘w’]. [[ 0.08632165]
[-0.08227636]
[-0.11789439]
[ 0.1285291 ]] != [[ 0.08639757]
[-0.08231268]
[-0.11798927]
[ 0.12866053]]
As you can see the answers are almost the same. If I hard code the w values to the correct values. I get a similar error for b.
AssertionError: Wrong values for d[‘b’]. -0.03986122376731202 != -0.03983236094816321
I also noticed that I had to use the default num_iterations for the optimize function, but a different learning_rate for the optimize function. The learning_rate I used was neither the default one or the one for given for the model function. I used the one given for logistic_regression_model. If I did not do this, I got errors.
Are these developer side errors, or is there something I am overlooking?
I guess I don’t understand what you mean about needing to use different values for the learning rate and number of iterations. Why don’t you use the actual values that are passed into the model function at the top level? That’s how the test case is telling you which values to use, right? Why not use those?
It’s also worth pointing out that an error in the 4th decimal place is not a rounding error: it’s a real mistake in your code. Now you need to find it. If you are not using the correct values of the passed parameters, that would definitely be at least part of the problem. 
I guess I wasn’t very clear. Regardless I figured it out, but I appreciate you answering the question.
For me it was a matter of the way I was passing the parameters to the optimize function. All I needed was a simple “num_iterations=num_iterations, learning_rate=learning_rate, print_cost=print_cost” instead adding what I thought were the correct values of the parameters (“num_iterations=2000, learning_rate=0.5, print_cost=True”). And of course the function I originally coded would have failed the very next cell (logistic_regression_model = model()) anyway.
Paul, appreciate you always being available to answer questions.
The mistake you made was exactly what I meant when I said the sentence above. You were just using the default values that are declared for those parameters, which means your results never change based on the actual values passed. Glad to hear that you were able to find the error, even if the way I stated my analysis was not that helpful.
I also have an issue. Although i have passed all tests and code runs i get an error on the assertion as below. Until that point everything checks out. Its when i test the model() func that i get issues.
AssertionError 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)
137 assert type(d[‘Y_prediction_test’]) == np.ndarray, f"Wrong type for d[‘Y_prediction_test’]. {type(d[‘Y_prediction_test’])} != np.ndarray"
138 assert d[‘Y_prediction_test’].shape == (1, x_test.shape[1]), f"Wrong shape for d[‘Y_prediction_test’]. {d[‘Y_prediction_test’].shape} != {(1, x_test.shape[1])}"
→ 139 assert np.allclose(d[‘Y_prediction_test’], expected_output[‘Y_prediction_test’]), f"Wrong values for d[‘Y_prediction_test’]. {d[‘Y_prediction_test’]} != {expected_output[‘Y_prediction_test’]}"
140
141 assert type(d[‘Y_prediction_train’]) == np.ndarray, f"Wrong type for d[‘Y_prediction_train’]. {type(d[‘Y_prediction_train’])} != np.ndarray"
AssertionError: Wrong values for d[‘Y_prediction_test’]. [[1. 0. 0.]] != [[1. 1. 0.]]
Hi, George.
Interesting! Did you take a look at the internals of the model_test routine in public_tests.py? What you can see is that if you get to that failure, it’s already checked that your w and b values are correct. So the only theory I can come up with is that you are calling predict incorrectly. E.g. maybe passing it different w and b values than the ones you just optimized. Or passing the wrong X value for that case, but the value you passed has the correct shape. Hmmmm.
Also note that it checks the test predictions before the train predictions, so it might be a clue whether the predictions on the training set are also incorrect. I added some print statements to my code and here’s the output I get from that test case:
type(X_train) <class 'numpy.ndarray'>
X_train.shape (4, 7)
X_test.shape (4, 3)
Y_test.shape (3,)
num_iterations 50 learning_rate 0.01
pred train [[1. 1. 0. 1. 0. 0. 1.]]
pred test [[1. 1. 0.]]
All tests passed!
Not sure. I inserted some print statements. I think i am calling it correctly.
I inserted some print statements inside the model func and in the public_test.py as seen below.
Inside model func
(4, 7)
(4, 3)
(3,)
50
0.01
End model
Inside model_test
printing "d"
{'costs': [array(0.69314718)], 'Y_prediction_test': array([[1., 0., 0.]]), 'Y_prediction_train': array([[1., 0., 0., 1., 0., 0., 1.]]), 'w': array([[ 0.08639757],
[-0.08231268],
[-0.11798927],
[ 0.12866053]]), 'b': -0.03983236094816321, 'learning_rate': 0.01, 'num_iterations': 50}
Notice that your Y_prediction_train value also differs from the one I show. So there is something consistently wrong in the two invocations of predict. Are you sure that your predict function passes the internal tests in the notebook?