W2_A2_Error in model_test(model)

Hi All,

I am very new to using such platforms, so apologies if I don’t phrase my question properly -

Facing an issue for several hours now when I run code cell 20 in Week 2 Assignment. I receive the following error message :

AssertionError                            Traceback (most recent call last)
<ipython-input-20-9408a3dffbf6> in <module>
      1 from public_tests import *
----> 3 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
    127     assert type(d['costs']) == list, f"Wrong type for d['costs']. {type(d['costs'])} != list"
    128     assert len(d['costs']) == 1, f"Wrong length for d['costs']. {len(d['costs'])} != 1"
--> 129     assert np.allclose(d['costs'], expected_output['costs']), f"Wrong values for d['costs']. {d['costs']} != {expected_output['costs']}"
    131     assert type(d['w']) == np.ndarray, f"Wrong type for d['w']. {type(d['w'])} != np.ndarray"

AssertionError: Wrong values for d['costs']. [array(0.15900538)] != [array(0.69314718)]

I have checked my formula to calculate cost and I believe it is correct. I have also passed all test up until this point (including the earlier cost calculation which matches the expected output).

Furthermore , when I run the next cell to train the model , I get the expected accuracy figures :

Cost after iteration 0: 0.693147
Cost after iteration 100: 0.584508
Cost after iteration 200: 0.466949
Cost after iteration 300: 0.376007
Cost after iteration 400: 0.331463
Cost after iteration 500: 0.303273
Cost after iteration 600: 0.279880
Cost after iteration 700: 0.260042
Cost after iteration 800: 0.242941
Cost after iteration 900: 0.228004
Cost after iteration 1000: 0.214820
Cost after iteration 1100: 0.203078
Cost after iteration 1200: 0.192544
Cost after iteration 1300: 0.183033
Cost after iteration 1400: 0.174399
Cost after iteration 1500: 0.166521
Cost after iteration 1600: 0.159305
Cost after iteration 1700: 0.152667
Cost after iteration 1800: 0.146542
Cost after iteration 1900: 0.140872
train accuracy: 99.04306220095694 %
test accuracy: 70.0 %

Really confused why code cell 20 when calling the model function gives me the wrong value for cost error. Any assistance in this regard will be really appreciated!


My guess is that you must be referencing global variables from within your model code. You get the correct costs on the real data, but not on the test for model. So are you directly referencing the real data from within model, instead of referencing the parameter values (X_train, Y_train and so forth)?

1 Like

You were correct ! The devil was in the detail :

‘cost’ v/s ‘costs’

I was using cost instead of costs which was referencing a global variable in the model.

Thank you :slight_smile:

It’s great news that you were able to find the solution just based on that hint. Onward! :nerd_face:

1 Like