Course 4, week 1, Convolution_model_Step_by_Step_v1 - Error: Wrong output for variable in position 0

Team,
I am trying Convolution_model_Step_by_Step_v1 assignment. With my best efforts, I got

2  Tests passed
 1  Tests failed
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-10-182241fd5e53> in <module>
     11 print("cache_conv[0][1][2][3] =\n", cache_conv[0][1][2][3])
     12 
---> 13 conv_forward_test(conv_forward)

~/work/release/W1A1/public_tests.py in conv_forward_test(target)
    118     ]
    119 
--> 120     multiple_test(test_cases, target)
    121 
    122 

~/work/release/W1A1/test_utils.py in multiple_test(test_cases, target)
    151         print('\033[91m', len(test_cases) - success, " Tests failed")
    152         raise AssertionError(
--> 153             "Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.".format(target.__name__))

AssertionError: Not all tests were passed for conv_forward. Check your equations and avoid using global variables inside the function.

I tried to debug the issue and I found the following error.

> /home/jovyan/work/release/W1A1/test_utils.py(36)equation_output_check()
-> def equation_output_check(expected_output, target_output, error):
(Pdb) l
 31  	    else:
 32  	        assert isinstance(target_output, type(expected_output))
 33  	        return 1
 34  	
 35  	
 36  ->	def equation_output_check(expected_output, target_output, error):
 37  	    success = 0
 38  	    if isinstance(target_output, dict):
 39  	        for key in target_output.keys():
 40  	            try:
 41  	                success += equation_output_check(expected_output[key],
(Pdb) n
> /home/jovyan/work/release/W1A1/test_utils.py(37)equation_output_check()
-> success = 0
(Pdb) n
> /home/jovyan/work/release/W1A1/test_utils.py(38)equation_output_check()
-> if isinstance(target_output, dict):
(Pdb) p target_output.keys()
*** AttributeError: 'tuple' object has no attribute 'keys'
(Pdb) n
> /home/jovyan/work/release/W1A1/test_utils.py(50)equation_output_check()
-> elif isinstance(target_output, tuple) or isinstance(target_output, list):
(Pdb) l
 45  	                                                          key))
 46  	        if success == len(target_output.keys()):
 47  	            return 1
 48  	        else:
 49  	            return 0
 50  ->	    elif isinstance(target_output, tuple) or isinstance(target_output, list):
 51  	        for i in range(len(target_output)):
 52  	            try:
 53  	                success += equation_output_check(expected_output[i],
 54  	                                                 target_output[i], error)
 55  	            except:
(Pdb) p tuple
<class 'tuple'>
(Pdb) n
> /home/jovyan/work/release/W1A1/test_utils.py(51)equation_output_check()
-> for i in range(len(target_output)):
(Pdb) len(target_output)
2
(Pdb) l
 46  	        if success == len(target_output.keys()):
 47  	            return 1
 48  	        else:
 49  	            return 0
 50  	    elif isinstance(target_output, tuple) or isinstance(target_output, list):
 51  ->	        for i in range(len(target_output)):
 52  	            try:
 53  	                success += equation_output_check(expected_output[i],
 54  	                                                 target_output[i], error)
 55  	            except:
 56  	                print("Error: {} for variable in position {}.".format(error, i))
(Pdb) n
> /home/jovyan/work/release/W1A1/test_utils.py(52)equation_output_check()
-> try:
(Pdb) n
> /home/jovyan/work/release/W1A1/test_utils.py(53)equation_output_check()
-> success += equation_output_check(expected_output[i],
(Pdb) l
 48  	        else:
 49  	            return 0
 50  	    elif isinstance(target_output, tuple) or isinstance(target_output, list):
 51  	        for i in range(len(target_output)):
 52  	            try:
 53  ->	                success += equation_output_check(expected_output[i],
 54  	                                                 target_output[i], error)
 55  	            except:
 56  	                print("Error: {} for variable in position {}.".format(error, i))
 57  	        if success == len(target_output):
 58  	            return 1
(Pdb) expected_output[0]
array([[[[ -2.65112363,  -0.37849177,  -1.97054929,  -1.96235299,
           -1.72259872,   0.4676693 ,  -6.43434016,   1.10764994],
         [  4.67692928,   4.29865415,  -1.3608031 ,   0.80532859,
           -2.88480108,   8.95280034,   5.32627807,  -1.82635258],
         [ -2.05881174,   3.40859795,   0.3502282 ,   0.68303626,
           -1.88328065,  -1.87480174,   5.8008721 ,   0.0700918 ],
         [ -3.50141791,   2.704286  ,   0.28341346,   4.15637411,
           -0.46575834,  -0.43668824,  -5.56866106,   1.72288033]],

        [[ -2.32126108,   0.91040602,   2.31852532,   0.98842271,
            3.31716611,   4.05638832,  -2.48135123,   0.95872443],
         [  6.03978907,  -6.96477888,  -1.20799344,   2.68913374,
           -4.35744033,  10.59355329,   3.20856901,  13.98735978],
         [ -3.01280755,  -2.90226517,  -8.34171936,  -5.26220853,
            5.6630696 ,   1.08704033,   2.20430705, -10.73218294],
         [ -6.24198266,  -0.53158832,  -3.29654954,  -1.81865997,
            0.59196322,   2.51134745,  -4.24924673,   5.21936641]],

        [[ -2.22187412,  -0.95259173,  -5.99441273,   0.79147932,
            1.16919278,  -0.17321161,  -3.26346299,  -3.62407578],
         [ -2.17796037,   8.07171329,  -0.5772704 ,   3.36286738,
            4.48113645,  -2.89198428,  10.99288867,   3.03171932],
         [-12.49991261,   5.26845833,  -1.67648614,  -8.65695762,
          -10.68157258,   6.71492428,   2.83839971,   4.47259772],
         [  0.11421092,  -1.90872424,  -3.28117601,   0.89922467,
            0.83985348,  -0.25127044,  -0.94409718,   5.17244412]]],


       [[[  1.97649814,   2.76743075,  -6.39611007,   2.95378171,
           -0.81235239,  -0.53333631,   0.71268871,   4.91385105],
         [ -5.14401869,   6.97041391,  -4.53976469,   5.89092653,
           -5.74606931,   2.74256558,   3.02124802, -10.04187592],
         [  5.53871187,  -8.55886701,  -4.70962135,   2.55966738,
           -2.66959504,   5.60010695,  -8.37253342,   4.18848278],
         [  0.63364517,  -3.71848223,  -3.67072772,   4.34226476,
           -1.21894465,   3.68929452,   5.89166305,   0.94256457]],

        [[  2.36049402,  -3.09696204,   8.33521755,   3.04680748,
            3.7964542 ,   0.66488788,   1.9935476 ,   1.54396221],
         [ -7.73457048,   0.287562  ,   7.97481218,   3.32415996,
           -4.07121488,   2.69182963,   4.1356109 ,  -5.16178423],
         [ -6.95635186,  -0.10924121,  -4.12526441,   0.62578199,
            4.69492086,  -3.52748877,   3.63168271,   0.64007629],
         [  7.94980014,   5.71855659,   3.49970333,  12.7718152 ,
            8.84959478,   2.37150319,  -1.42531648,  -0.51126641]],

        [[ -5.29658283,  -4.20466999,  -6.63067766,  -9.87831724,
           -5.32130395,   7.32417919,   2.96011091,   7.60669481],
         [ 11.54630784,  -1.93157244,   2.26699242,   7.62184275,
            5.40584348,  -2.88837958,  -1.46981877,   7.91314719],
         [  5.94067877,   3.50739649,   0.82512202,   4.80655489,
           -4.1044945 ,   4.14358541,   0.13194885,   4.35397285],
         [  4.91298364,  -1.44499772,   5.9392078 ,  -3.92690408,
            2.12840309,   1.27237402,   1.56992581,   0.44270565]]]])
(Pdb) s
> /home/jovyan/work/release/W1A1/test_utils.py(54)equation_output_check()
-> target_output[i], error)
(Pdb) l
 49  	            return 0
 50  	    elif isinstance(target_output, tuple) or isinstance(target_output, list):
 51  	        for i in range(len(target_output)):
 52  	            try:
 53  	                success += equation_output_check(expected_output[i],
 54  ->	                                                 target_output[i], error)
 55  	            except:
 56  	                print("Error: {} for variable in position {}.".format(error, i))
 57  	        if success == len(target_output):
 58  	            return 1
 59  	        else:
(Pdb) s
--Call--
> /home/jovyan/work/release/W1A1/test_utils.py(36)equation_output_check()
-> def equation_output_check(expected_output, target_output, error):
(Pdb) l
 31  	    else:
 32  	        assert isinstance(target_output, type(expected_output))
 33  	        return 1
 34  	
 35  	
 36  ->	def equation_output_check(expected_output, target_output, error):
 37  	    success = 0
 38  	    if isinstance(target_output, dict):
 39  	        for key in target_output.keys():
 40  	            try:
 41  	                success += equation_output_check(expected_output[key],
(Pdb) n
> /home/jovyan/work/release/W1A1/test_utils.py(37)equation_output_check()
-> success = 0
(Pdb) n
> /home/jovyan/work/release/W1A1/test_utils.py(38)equation_output_check()
-> if isinstance(target_output, dict):
(Pdb) n
> /home/jovyan/work/release/W1A1/test_utils.py(50)equation_output_check()
-> elif isinstance(target_output, tuple) or isinstance(target_output, list):
(Pdb) n
> /home/jovyan/work/release/W1A1/test_utils.py(63)equation_output_check()
-> if hasattr(target_output, 'shape'):
(Pdb) n
> /home/jovyan/work/release/W1A1/test_utils.py(64)equation_output_check()
-> np.testing.assert_array_almost_equal(
(Pdb) l
 59  	        else:
 60  	            return 0
 61  	
 62  	    else:
 63  	        if hasattr(target_output, 'shape'):
 64  ->	            np.testing.assert_array_almost_equal(
 65  	                target_output, expected_output)
 66  	        else:
 67  	            assert target_output == expected_output
 68  	        return 1
 69  	
(Pdb) n
> /home/jovyan/work/release/W1A1/test_utils.py(65)equation_output_check()
-> target_output, expected_output)
(Pdb) n
AssertionError: 
Arrays are not almost equal to 6 decimals

Mismatched elements: 176 / 192 (91.7%)
Max absolute difference: 18.53970168
Max relative difference: 30.3425308
 x: array([[[[ -2.651124,  -0.378492,  -1.970549,  -1.962353,  -1.722599,
            0.467669,  -6.43434 ,   1.10765 ],
         [  0.      ,   0.      ,   0.      ,   0.      ,   0.      ,...
 y: array([[[[ -2.651124,  -0.378492,  -1.970549,  -1.962353,  -1.722599,
            0.467669,  -6.43434 ,   1.10765 ],
         [  4.676929,   4.298654,  -1.360803,   0.805329,  -2.884801,...
> /home/jovyan/work/release/W1A1/test_utils.py(65)equation_output_check()
-> target_output, expected_output)
(Pdb) n
--Return--
> /home/jovyan/work/release/W1A1/test_utils.py(65)equation_output_check()->None
-> target_output, expected_output)
(Pdb) n
AssertionError: 
Arrays are not almost equal to 6 decimals

Mismatched elements: 176 / 192 (91.7%)
Max absolute difference: 18.53970168
Max relative difference: 30.3425308
 x: array([[[[ -2.651124,  -0.378492,  -1.970549,  -1.962353,  -1.722599,
            0.467669,  -6.43434 ,   1.10765 ],
         [  0.      ,   0.      ,   0.      ,   0.      ,   0.      ,...
 y: array([[[[ -2.651124,  -0.378492,  -1.970549,  -1.962353,  -1.722599,
            0.467669,  -6.43434 ,   1.10765 ],
         [  4.676929,   4.298654,  -1.360803,   0.805329,  -2.884801,...
> /home/jovyan/work/release/W1A1/test_utils.py(54)equation_output_check()
-> target_output[i], error)
(Pdb) n
> /home/jovyan/work/release/W1A1/test_utils.py(55)equation_output_check()
-> except:
(Pdb) n
> /home/jovyan/work/release/W1A1/test_utils.py(56)equation_output_check()
-> print("Error: {} for variable in position {}.".format(error, i))
(Pdb) n
Error: Wrong output for variable in position 0.
> /home/jovyan/work/release/W1A1/test_utils.py(51)equation_output_check()
-> for i in range(len(target_output)):
(Pdb) c
 2  Tests passed
 1  Tests failed

I am not able to figure out the cause of this issue. I need some hints to move forward. Please help.

I salute your profiency with the debugger and your willingness to engage at this level, but what is the point of debugging the test code? That is not really going to tell you anything helpful, right? What it tells you is that your code produces the wrong answer, but looking at the comparison between the correct answer and your answer doesn’t shed any light on why your answer is wrong. That’s the question that matters.

The better direction in which to investigate is comparing what your code actually does to the description of what it is supposed to do. Unfortunately most of the common mistakes cause an exception to get thrown because of wrong dimensions. If your code does not throw any exceptions, but still generates wrong answers, that’s a bit trickier.

Thanks @paulinpaloalto . With the above test code debugging and printing target_output and expected_output, I got the hint to solve this issue further. My target_output has only 8.3% valid values and rest are zeros. It means I have some issue with my for loops.

If you are seeing zeros in your outputs, it may indicate a problem with how you are handling the “stride” values. One thing to note is that the stride only applies to the input space, not the output space. Your for loops must touch every element of the output space.

Thanks @paulinpaloalto . I was taking care of stride but not well enough with my for loops. I got the solution. It is working fine for me.

That’s great news! Congrats and thanks for confirming!