Help with adam parameters update

TypeError Traceback (most recent call last)
Cell In[24], line 3
1 parametersi, grads, vi, si, t, learning_rate, beta1, beta2, epsilon = update_parameters_with_adam_test_case()
----> 3 parameters, v, s, vc, sc = update_parameters_with_adam(parametersi, grads, vi, si, t, learning_rate, beta1, beta2, epsilon)
4 print(f"W1 = \n{parameters[‘W1’]}“)
5 print(f"W2 = \n{parameters[‘W2’]}”)

Cell In[23], line 78, in update_parameters_with_adam(parameters, grads, v, s, t, learning_rate, beta1, beta2, epsilon)
69 s_corrected[“db” + str(l)] = s[“db” + str(l)]/(1-beta2**t)
71 # YOUR CODE ENDS HERE
72
73 # Update parameters. Inputs: “parameters, learning_rate, v_corrected, s_corrected, epsilon”. Output: “parameters”.
(…)
76 # parameters[“b” + str(l)] = …
77 # YOUR CODE STARTS HERE
—> 78 parameters[“W” + str(l)] = parameters[“W” + str(l)]-learning_ratev_corrected[“dW” + str(l)]/(math.sqrt(s_corrected[“dW” + str(l)])+epsilon)
79 parameters[“b” + str(l)] = parameters[“b” + str(l)]-learning_rate
v_corrected[“db” + str(l)]/(math.sqrt(s_corrected[“db” + str(l)])+epsilon)
81 # YOUR CODE ENDS HERE

TypeError: only size-1 arrays can be converted to Python scalars

is it the same assignment that you were doing?

it is optimization methods lab week2, i could send you my code, i guess i am not using the proper function either to square or square root. i could share my codes
thank you

Yes your parameters are not getting update as your codes are incorrect. Did you try searching this error in the forum?

yes, i just did

none has answered yet

ok send the code in the personal dm

Hi,

please notice the v_corrected, s, s_corrected and parameters update codes are incorrect. Please see the below image to make the correction. wherever you see a code or parameter raise to the power, one needs to use np.power. In your update parameters, we are suppose to use np.sqrt and not math.sqrt.

Regards
DP

yes i did corrected to np.sqrt, but still it is giving me errors. here is the latest version

{moderator edit - solution code removed}

C:\Users\User\AppData\Local\Temp\ipykernel_15988\128279702.py:79: RuntimeWarning: invalid value encountered in sqrt
parameters[“b” + str(l)] = parameters[“b” + str(l)]-learning_rate*v_corrected[“db” + str(l)]/(np.sqrt(s_corrected[“db” + str(l)])+epsilon)
W1 =
[[ 1.63937725 -0.62327448 -0.54308727]
[-1.0578897 0.85032154 -2.31657668]]
W2 =
[[ 0.33400549 -0.23563857]
[ 1.47715417 -2.04561842]
[-0.33729882 -0.36908457]]
b1 =
[[ 1.73419514]
[-0.77549071]]
b2 =
[[nan]
[nan]
[nan]]

AssertionError Traceback (most recent call last)
Cell In[26], line 9
6 print(f"b1 = \n{parameters[‘b1’]}“)
7 print(f"b2 = \n{parameters[‘b2’]}”)
----> 9 update_parameters_with_adam_test(update_parameters_with_adam)

File ~\public_tests.py:270, in update_parameters_with_adam_test(target)
268 assert type(s[key]) == np.ndarray, f"Wrong type for s[‘{key}’]. Expected np.ndarray"
269 assert s[key].shape == si[key].shape, f"Wrong shape for s[‘{key}’]. The update must keep the dimensions of s inputs"
→ 270 assert np.allclose(s[key][0], expected_s[key]), f"Wrong values. Check you formulas for s[‘{key}’]"
271 #print(f"s["{key}"]: \n {str(s[key])}")
273 for key in sc.keys():

AssertionError: Wrong values. Check you for

AssertionError: Wrong values. Check you formulas for s[‘db1’]

Look at the stage where you use np.square on the dW term. Why don’t you also use that on the db expression? Why is it different?

Why did you remove all the other material from the notebook? You can just save the ipynb file as is if you want to share it. It’s a lot more work to modify it the way you did.

Please do all the corrections mentioned in the comment

I told you to do correction of your codes for
v_corrected
s
s_corrected and then
update parameters correction

Hi,

Once you have done the corrections and got the desired result, let us know. Please do not be in hurry or panic when an assignment doesn’t work. Always take a break, and start fresh with your work as sometimes not finding solution is frustrating. I almost have seen when things get stuck with assignment, I close my workspace, do something I like, and when my mind is fresh, I start from the first cell and not the cell where I am stuck.

Regards
DP

Good eye, that was the whole problem, thank you

Well, to be fair, the error actually pointed you right to the result that was incorrect. If the s['dW1'] was correct, but s['db1'] was wrong, then the logical thing to check would be what is different between those two computations. Of course it’s always easy to see something like that in retrospect. :laughing:

1 Like

I was mislead by the course notes, that does not square the db term in the Sdb equation & in the lab guidelines is omitted
C2_W2 24.pdf (65.9 KB)

That’s a good point: that slide is wrong. But there is already a reading item right before that lecture that points out that error.

I grant you that it would have been better if they’d actually fixed the lecture video, but I guess that’s harder to do.

you are right the reading mentioned that, but i forgot