C1W3_Assognment_5.2_Test the model on the planar dataset

Hello,

I m trying to complete ex 5.2 and i m getting the following error…

I have seen a lot of posts regarding this issue and none describes my condition. I m not using any Global Variables or hardcode in nn_model function… additionally, i dont understand the predictions assignment. Why is it hard coded to [[ True False True]], i assumed user needed to enter a test case to test the model… can some help me with both questions…?

Thank you.

Have you passed all the previous exercises? Can you see All tests passed!?

Hi @Neal_M,

Could you, please, run:

parameters = nn_model(X, Y, n_h = 4, num_iterations = 10000, print_cost=True)
x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1
y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1
h = 0.01
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
P = lambda x: predict(parameters, x.T)
Z = P(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

X.shape, Y.shape, xx.shape, yy.shape, Z.shape

in a new cell before the cell of your interest and share the results here?

Best regards,

Dmitry.

Hello Saif,

Thank you for responding… most of the function output does indicate “All Tests passed !” with the exception of the following functions:

  • Update_parameters - The output shows the following… AssertionError: Wrong values for W1
  • nn_model - The output also projects the same message.. AssertionError: Wrong values for W1

I have read old posts by a moderator indicating that the values don’t match the expected because of a bug.

please let me know your thoughts… thank you.

Hello Dmitry,

Thank you for reaching out… please see below the output.

Did you add the “lambda” function (at line 6), or was that part of the provided code?

Aha, I believe you already see the cause of the problem, but to make it absolutely obvious, please run the adjusted code:

parameters = nn_model(X, Y, n_h = 4, num_iterations = 10000, print_cost=True)
x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1
y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1
h = 0.01
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
P = lambda x: predict(parameters, x.T)
Z = P(np.c_[xx.ravel(), yy.ravel()])
#Z = Z.reshape(xx.shape)

X.shape, Y.shape, xx.shape, yy.shape, np.c_[xx.ravel(), yy.ravel()].T.shape, Z.shape, parameters["W1"].shape, parameters["W2"].shape

And BTW all tests of this assignment should give you “All tests passed!” without any exceptions.

Best regards,

Dmitry

Here’s a thread from 4 years ago with exactly the same error message and it provides a description of what the problem was in that particular instance. In that case it was a classic bug of referencing a global variable from within the body of one of your functions.

Although that bug is in predict and would not have the effect you see of getting incorrect W1 values when you run nn_model_test. So you may have more than one bug. My guess would be that the other one is also some form of ignoring parameter values or referencing global variables.

You should never move to the next exercise until you have passed the previous one, especially when the next exercise depends on it.

As you said, you haven’t passed the update_parameters exercise. So, first, focus on it. Double check your implementation of this exercise.

hello Saif,

I agree… i will make a note of this instance. I corrected the issues and made sure the functions passed the tests. The error for the functions was the update function where i didnt include the learning rate in calculation. Addition of this gave me the correct values…

I m still trying to figure out why i m getting the value Error for 5.2.

ValueError: cannot reshape array of size 3 into shape (1008,1030)


I noticed few other moderators have joined the discussion and gave me some direction.. i m looking into it to figure out,

Good, after correcting your previous exercise, try

Kernel -> Restart and Clear Output
Cell -> Run All

Then let us know if you face any error or any new error.

Hello Paul,

Thank you for your input and advice. I looked into the thread provided and i read that post yesterday. That was the initial issue and i corrected it to have the prediction assignment as numpy array.

image

the issue here is … i dont understand why the dimensions changed when its using the same dataset.

Are you still facing any issues or problem solved?

If you had the same bug as on that other thread, the problem was that the predict logic was relying on the value of the global variable cache and not setting that variable by actually calling forward_propagation. So it just happened to work when you ran the tests for predict, because the tests actually set the value of cache for you. But then when you ran the graphing function later, the cache variable was still set to the values from predict_test. The predict_test logic does not use the real dataset as the inputs.

Hello Saif,

Thank you for the follow up.. i appreciate your support. Yes i solved the issues. I hard coded the predictions assignment to the expected output. I never understood the details of the function but paul’s explanation helped me understand why the shape never aligned.

Thanks again for your time. All cells are now correct. Assignment submitted.

thanks paul. I didn’t understand the role of predict function and the subsequent functions. Your explanation guided me. The predictions assignment was hard coded to the expected output than evaluated to greater than 0.5. This resulted in (1,3) array output for predictions causing the Value Error.

Thanks for taking the time to explain. Have a good one!