C1W2 Exercise8 Wrong values for d['Y_prediction_test']

Hi all,

I do not understand why I am getting this error in exercise 8: AssertionError: Wrong values for d[‘Y_prediction_test’]. [[1. 1. 1.]] != [[1. 1. 0.]]

I’ve passed all the tests before exercise 8. I’ve checked ‘public_tests.py’ and compared the values of w, b, and costs from my model and expected values of w, b, and costs from ‘public_tests.py’. The values are the same.

In this exercise, I initalized w and b by calling initialize_with_zeros(X_train.shape[0]), getting params, grads, costs by calling optimize function, and getting the Y_prediction_test and Y_prediction_train by calling predict function. Both Y_prediction_test and Y_prediction_train are different from the expected values in ‘public_tests.py’. I did not hardcode num_iterations or learning_rate.

I would greatly appreciate if anyone can help me.

Thank you.

Your description of your code sounds right, but there must be some details that aren’t correct to get the wrong answers.

I would start by making sure you are not referencing any global variables in the body of your model function. Make sure you are referencing X_train, Y_train and X_test in your code, not X and Y.

Are you sure you retrieved w and b from the correct dictionary after the call to optimize? If your function returns params, but you reference parameters, that would be another case of referencing a global variable. Or if you just forgot to retrieve w and b, then you’d be using the zero initialized values, instead of the actual trained values.

If those suggestions aren’t enough to shed any light, please let us know and there are other steps we can take to help debug this.

Thanks for such a prompt response!

I am referencing X_train, Y_train, X_test in my code. I am retrieving w and b from ‘params’ dictionary. Here are the values of w, b, and costs after calling optimize. (I added print statements before calling predict function).

For y_prediction_test, I call predict(w, b, X_test). For Y_prediction_train, I call the predict function with X_train.

Thanks for checking those things. That all sounds right. One other thing I can think of is that when you call optimize, it’s important not to “hard-code” the learning rate and the number of iterations. You need to simply pass through the values of those parameters that are passed into the model function as arguments. There should be no “assignments” in the list of parameters that you pass to optimize (no equal signs). And if you simply omit the learning_rate and num_iterations, then you are still ignoring the actual requested values and using the default values declared in the definition of the optimize function.

I added some print statements to my model code and here’s what I see:

model with num_iterations 50 learning_rate 0.01
X_train.shape (4, 7)
X_test.shape (4, 3)
Y_test.shape (1, 3)
before optimize w.shape (4, 1)
optimize with num_iterations 50 learning_rate 0.01
in model before predict call w.shape (4, 1)
predictions = [[1. 1. 0. 1. 0. 0. 1.]]
predictions = [[1. 1. 0.]]
w [[ 0.08639757]
 [-0.08231268]
 [-0.11798927]
 [ 0.12866053]]
b -0.039832360948163205
All tests passed!

Interesting! Note that my w and b values agree with yours. So that suggests that my previous theory about the arguments to optimize might be incorrect is not applicable. I think we’re left with just the invocations of the predict function itself. You did mention that your predict function passed its tests earlier (exercise 7), so it must be that you are passing incorrect arguments when you call it somehow.

If that suggestion doesn’t help, then maybe it’s time to actually look at your code. We can’t do that on a public thread, but please check your DMs for a message from me about how to proceed with that.

I’m going to guess there is an error in either your optimize() or predict() functions.

Note that you can’t rely on the built-in test cases to prove your code is perfect.

Yes, to close the loop on the public thread, it turns out that the issue was in the predict function: the implementation was problematic, but passed the local unit tests. @spark698 was able to find the problem just based on the suggestion of looking more closely at the predict logic.

I’m doing a few experiments based on these results and I’ve found several implementations that pass the unit tests but are not correct. I’ll file a git request in the hopes that we can get the unit tests for predict beefed up.

1 Like