C2W2A1 - broadcasting error

I am currently experiencing an issue with the C2W2A1 Optimization Methods which is giving the following error in the screenshot. Based on the instructions it seemed that I got everything correct so not sure exactly which area I went wrong.

The error you encounter, ValueError: operands could not be broadcast together with shapes (3,2) (2,3), indicates that there is a mismatch in the matrix dimensions for the operations in line 80 or 81 within the update_parameters_with_adam function. This is usually due to the shapes of v_corrected["dW" + str(l)] and s_corrected["dW" + str(l)] not matching with the shape of parameters["W" + str(l)].

Make sure that grads["dW" + str(l)], v_corrected["dW" + str(l)], and s_corrected["dW" + str(l)] have the same shape as parameters["W" + str(l)]. Similarly, check that grads["db" + str(l)], v_corrected["db" + str(l)], and s_corrected["db" + str(l)] match the shape of parameters["b" + str(l)]. You can add print statements to display the shapes of parameters["W" + str(l)], v_corrected["dW" + str(l)], and s_corrected["dW" + str(l)] before line 80. This may help to locate the specific variable that is causing the problem.

I hope this helps you locate and fix the dimension mismatch causing the broadcasting error.

@AaronRampersad @nadtriana I did this slightly different than they did, but looked. I wondered if it was an order of operations issue.

I manage to fix the issue - essentially I had a l+1 somewhere in the loop. But now I have another issue (See screenshot).

One common mistake is to put epsilon in the square root. It is not a part of the square root.

3 Likes

@saifkhanengr is right; placing epsilon inside the np.sqrt function can cause the problem. The correct implementation should place epsilon outside the square root. Try this and it should fix the AssertionError.

1 Like

Thanks, all help is appreciated. My last issue concerns the weird output since I tried this in my own compiler and it gave the correct answers.


@AaronRampersad sorry to be anal, but what do you mean by your ‘own compiler’. Python doesn’t get compiled, it is ‘interpreted’.

What do you get from the results of running:

python -v

?

3.12.7

@AaronRampersad so your learning rate code checks ‘okay’ with me, but I am just ‘weird’ and like to put spaces between my math operations so everything isn’t just all ‘bundled together’. Should have no effect on output. Your Python isn’t the latest, but not the oldest. Not much should have changed.

I am still confused as to how you are getting better results/it working locally.

I think something is still wrong ‘upstream’.

Please check the spelling of the variable learning_rate in your update_lr function and make sure you return it correctly.

1 Like

Please follow the Note and Hint provided in the exercise instructions:

Note: The fraction in the denominator uses the floor operation.
Hint: numpy.floor

Thanks @nadtriana, that I miss English (as a former ESL teacher) tells me I was looking at this way too early in the morning :wink:

1 Like