C2W2_E4_Update_parameters_with_momentum_test_case equation_output_check is failing


I am facing an issue while running the tests for update_parameters_with_momentum() using update_parameters_with_momentum_test_case(). More specifically, the equation_output_check test case is failing. The params and the velocity terms are not matching with expected_params and expected_velocity.

When I try to test the function without using the multiple_test_cases() routines but with update_parameters_with_momentum_test_case() and update_parameters_with_momentum(), I am able to see that the output (params, v) returned by update_parameters_with_momentum() match with (expected_params, expected_v). I also checked the learning_rate, seed, and beta values. They are the same for individual testing and for multiple_test_cases() routines

Below, I am pasting the W1 and dW1 values returned by the update_parameters_with_momentum() when using the test case routines.
W1 expected
[[ 1.62544598 -0.61290114 -0.52907334]
[-1.07347112 0.86450677 -2.30085497]]
W1 returned
[[ 1.63051984 -0.61817831 -0.53322968]
[-1.07578762 0.86035383 -2.29770298]]
Error: Wrong output for variable W1.

dW1 expected
[[-0.11006192 0.11447237 0.09015907]
[ 0.05024943 0.09008559 -0.06837279]]
dW1 returned
[[-0.2982678 0.31022013 0.24433109]
[ 0.13617597 0.24413196 -0.18529025]]
Error: Wrong output for variable dW1.

I am not changing anything in the test case routines.

Below is the logic I used for update_parameters_with_momentum().

[snippet removed by mentor]

I could not wrap my head around this. It would be of great help if someone could help me with this issue.

Sorry for pasting the logic here. I will remove it once I get the resolution. Thanks for your patience


Your code looks correct to me. Please click my name and message your notebook as an attachment.

Can you mention which week and assignment name.

Assignment Name Optimization method

Momentum update for each parameter

for l in range(1, L + 1):

There is an iteration error in both compute velocities and update parameters.

Also in codes, moment update for each parameter is shown as (1, L) but should (1, L +1) which was given with the template code.

Check carefully, when we compute velocities and update parameter, we do for the layer and not for the next layer.

The below image should help you understand to correct the iterations


Loop body contains reference to l+1.

Hi @Deepti_Prasad , I am starting the range in the for loop from 0 to L. Hence, inside the loop, while updating, I am incrementing the params by 1 i.e. l+1 to access the right parameter. This is equal to using the range(1, L+1) and using l to access parameters. I don’t think, this is the causing the issue

Hi @balaji.ambresh

Thanks for your reply.

I have reproduced the error in the notebook that is sent to you via message. I would really appreciate it if you could point me to the error that I am unable to catch.

Best Regards

@balaji.ambresh - Thanks for reviewing my code.

I am able to solve the issue in my notebook. In my code, the issue is with the execution order of test cases. Every time I run different testcases, the update_parameters_with_momentum() is updating the target params, v. The expected_output, that contains (expected_params, expected_v) are the values returned by the function update_parameters_with_momentum() when it was executed once. Hence, I just changed the order of testcases and made sure that equation_output_check test case is executed first. This way target equation_output matches with expected_output.

Best Regards