Week 2 Programming: Error in model function

Please advise on this error from the model function test.
All prior tests pass.


AssertionError: Wrong length for d[‘costs’]. 20 != 1


Hi, is that error happening in the model_test(model) line?

I’m struggling a little bit to understand how that error is happening. The costs array is filled in the optimize function, one value every 100 iterations and in the model function that’s included in the d dictionary.

So the length of costs in the dictionary d depends only on the number of iterations (which by default is 2000). In principle you are not expected to change the code which takes care of the recording of the costs, just the calculation of every cost value which is saved in the list every 100 iterations, so that would result in len(d[‘costs’]) = 20

Could you provide us with additional information about the error?

Maybe you could print the dictionary d before the return statement to see the content.

1 Like

{‘costs’: [array(0.69314718), array(0.03844515), array(0.01922401), array(0.012763), array(0.00954021), array(0.00761264), array(0.00633123), array(0.00541815), array(0.00473474), array(0.00420412), array(0.00378026), array(0.00343392), array(0.00314563), array(0.00290194), array(0.00269325), array(0.00251253), array(0.00235451), array(0.00221517), array(0.0020914), array(0.00198071)], ‘Y_prediction_test’: array([[1., 0., 1.]]), ‘Y_prediction_train’: array([[1., 0., 1.]]), ‘w’: array([[ 5.38236503],
[ 4.70953022],
[ 1.83575827]]), ‘b’: -1.4116050299767378, ‘learning_rate’: 0.0001, ‘num_iterations’: 50}

AssertionError Traceback (most recent call last)
----> 1 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
113 assert type(d[‘costs’]) == list, f"Wrong type for d[‘costs’]. {type(d[‘costs’])} != list"
→ 114 assert len(d[‘costs’]) == 1, f"Wrong length for d[‘costs’]. {len(d[‘costs’])} != 1"
115 assert np.allclose(d[‘costs’], expected_output[‘costs’]), f"Wrong values for pred. {d[‘costs’]} != {expected_output[‘costs’]}"

AssertionError: Wrong length for d[‘costs’]. 20 != 1

In your dictionary d the num_iterations is 50, therefore, only one cost should have been added to the costs list. To debug the error you could add a print statement in the optimize function right before the cost is added to the cost list. Something like this:

        # Record the costs
        if i % 100 == 0:
            print('append cost', i)
1 Like

I added some print statements and my costs list does have length 20.

It looks to me like the test assertion is expecting length 1. My number of iterations is set to 2000.


the append cost gets printed 20 times

That’s a bit odd, the number of iterations when model_test(model) is called is 50 as you can see in your printout of the dictionary d, therefore, the length of costs should be 1. Are you hardcoding the number of iterations in the optimize function? Maybe you could print also that value before the for loop (for i in range(num_iterations):) in optimize function, that value should be 50 when calling model_test.

1 Like

i have not changed anything in optimize function . I printed the value of num iterations which came out to be 2000 on executing model test.

I’m seeing that there was an update of this notebook on the 28th of April, did you start working on this prior that date? I’m thinking that maybe you are working on an older version of the notebook but the tests are performed with the newer one.

I updated to the last version prior to check this issue and in the model_test(model) cell there are 3 tests and in all of them the number of iterations is 50.

Please, let me know if this hint helps you otherwise I will contact the course staff to check the issue because the number of iterations in your case is different to mine.

1 Like

I had copied the parameters of model when I used arguments in optimize in it.
optimize(w,b,X_train,Y_train,num_iterations = 2000, learning_rate = 0.05, print_cost = true)
I changed them to
optimize(w,b,X_train,Y_train,num_iterations, learning_rate , print_cost) and am no longer getting any error.