C5W4 Ex2 Positional Encodings


AssertionError Traceback (most recent call last)
in
1 # UNIT TEST
----> 2 positional_encoding_test(positional_encoding, get_angles)

~/work/W4A1/public_tests.py in positional_encoding_test(target, get_angles)
34
35 ones = sin_part ** 2 + cos_part ** 2
—> 36 assert np.allclose(ones, np.ones((1, position, d_model // 2))), “Sum of square pairs must be 1 = sin(a)**2 + cos(a)**2”
37
38 angs = np.arctan(sin_part / cos_part)

AssertionError: Sum of square pairs must be 1 = sin(a)**2 + cos(a)**2

“Help needed, Please. I have been stuck all day”

Edit: I used this: “get_angles(np.arange(positions)[:, np.newaxis],
np.arange(d)[np.newaxis, :],
np.arange(d)[np.newaxis, :])” #to change the dim
And my get_angles() ran properly!

Please click my name and message your notebook as an attachment.

Thank you Balaji,

I have sent it.

Please pay attention to the last parameter of get_angles function and fix the mistake:
d(integer) -- Encoding size.

Sincerely Boss, I do not understand.
Am I to cast the into an int? I did that and this is the error msg:

"---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in
1 # UNIT TEST
----> 2 positional_encoding_test(positional_encoding, get_angles)

~/work/W4A1/public_tests.py in positional_encoding_test(target, get_angles)
26 d_model = 16
27
—> 28 pos_encoding = target(position, d_model)
29 sin_part = pos_encoding[:, :, 0::2]
30 cos_part = pos_encoding[:, :, 1::2]

in positional_encoding(positions, d)
16 angle_rads = get_angles(np.arange(positions)[:, np.newaxis],
17 np.arange(d)[np.newaxis, :],
—> 18 np.arange(d)[np.newaxis, :])
19 #angle_rads = get_angles(np.arange(positions).reshape(-1, 1), np.arange(d).reshape(1, -1), np.arange(d).reshape(1, -1))
20 #angle_rads = get_angles(np.arange(positions)[:, np.newaxis], np.arange(d)[np.newaxis, :], np.arange(d)[np.newaxis, :])

in get_angles(pos, k, d)
19
20 # Calculate the angles using pos, i and d
—> 21 angles = pos / 10000**(2 * i / int(d))
22 # END CODE HERE
23

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

Please help me!

Solved, after spending my entire day on it. The challenge was not in the get_angles() function. It was in the shape of the arguments as passed into get_angles() during call in the position_encoding() function.

{code removed - moderator}

Thank you Balaji, Paulinpaloalto

Yes, I agree. Make sure your shapes are correct for the casting operation.