So, I am struggling with part 8 of the exercise. My problem is with cost - when running the model_test(model) command, I get
AssertionError: Wrong length for d[‘costs’]. 20 != 1
Hunting costs down, it is a parameter returned from optimize(). If we look into the code of the function, we see that
# Record the costs
if i % 100 == 0:
costs.append(cost)
where i is looped over all iterations - 2000 in total based on the way the function is called. So it should have 20 elements.
Am I missing some place where costs is compressed to a single element?
Thank you!
Strange. It should be a array since it will be use to plot a graph. If you run just the model()
function (not the model_test()
) does it work fine? If it does what is the len(model()["costs"])
?
Thank you for getting back to me!
The original version expects costs to have a length of 1:
assert len(d[‘costs’]) == 1, f"Wrong length for d[‘costs’]. {len(d[‘costs’])} != 1"
If I just run model(), it runs OK, I think at least. I get several NaN as costs, but the model runs at 100% training and 72% testing accuracy as expected.
Cost after iteration 0: 0.693147
Cost after iteration 100: nan
Cost after iteration 200: nan
Cost after iteration 300: nan
Cost after iteration 400: nan
Cost after iteration 500: nan
Cost after iteration 600: nan
Cost after iteration 700: nan
Cost after iteration 800: nan
Cost after iteration 900: nan
Cost after iteration 1000: nan
Cost after iteration 1100: nan
Cost after iteration 1200: nan
Cost after iteration 1300: nan
Cost after iteration 1400: nan
Cost after iteration 1500: nan
Cost after iteration 1600: nan
Cost after iteration 1700: nan
Cost after iteration 1800: nan
Cost after iteration 1900: nan
train accuracy: 100.0
test accuracy: 72.0
len(logistic_regression_model[“costs”]) gives 20 as output.
Are you dividing the pixel values by 255 or by the standard deviation? I ask that because I am getting the same NAN results when using standard deviation and also the 72% accuracy. When using 255 I don’t get NAN and the accucary is 70%
I have not touched that part. Using the default code, I am dividing by 255. That’s happening in
In[6]: train_set_x = train_set_x_flatten / 255.
test_set_x = test_set_x_flatten / 255.
I don’t think there is another normalization somewhere else.
I am also getting the error message about the length of cost not being 20. Is this a bug in the test code? As Vickyl pointed out, it does add 20 entries in optimize() and then asserts that there should only be one entry.
It may be because of not passing all the parameters inside optimize function in model_test(model) function. Pass it like this in model_test function.
{moderator edit - solution code removed}
because in the original optimize function, you have num_iteration =100 which propagates to every usage of function as it takes the global arguments. But in model_test it is 2000 because of which it doesn’t show the next 19 entries.
I have the same problem and now I solved it. Thanks a lot!
Glad to know that you manged to solved it on your own!
I had this same error. This resolved it, thanks!
Thanks!, I had the same problem and with your advice, it is solved.
If your costs array has 20 elements, it means you did not call optimize
correctly: you did not pass through the actual value of the number of iterations that the test case requests. You hard-coded the number of iterations to be 2000, which is a mistake.
I am adding this reply because I just edited the post from SkyWalker to remove the solution code provided there. We are not supposed to share solutions in a public way. Sorry that I had not noticed this thread before.
This issue has come up many times. Here’s another thread about it which has the above explanation in a bit more detail.
Thanks for your explanation, it was very helpful and I understand what you mean.
Hello. Right now I’m facing this issue and tried the solution of not hardcoding the number of iterations and letting the optimizer use its defaults.
This would eventually solve the costs length problem but now I get an error with the values of dw. To get to the expected values, 2000 iterations are good but not 50 or 100.
It’s a mistake to use any of the defaults at either level, right? You need to use the actual parameter values that are supplied in the outer level call to model
. Those tell you what the test case is asking for. If you use a different value than what they ask for, then you get a different answer.
Correct.
All tests passed!
Thank you very much