C5 W4 A1: Positional_encoding - Sum of square pairs must be 1

I’m stuck with the error:

AssertionError Traceback (most recent call last)
—> 31 positional_encoding_test(positional_encoding)

<ipython-input-76-848c64b9c901> in positional_encoding_test(target)
     13     ones = sin_part ** 2  +  cos_part ** 2
---> 14     assert np.allclose(ones, np.ones((1, position, d_model // 2))), "Sum of square pairs must be 1 = sin(a)**2 + cos(a)**2"
     16     angs = np.arctan(sin_part / cos_part)

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

the previous test for get_angles ran fine.

Hey @petermoser,

It seems that you have an issue with initializing the initial values for sine and cosine functions. Please, check this post for some details on that.

are you saying that my sin / cos calcuation, e.g.:
angle_rads[:, 0::2] = np.sin(angle_rads[:, 0::2])
is wrong or that the initial values in angle_rads are wrong?
As mentioned, the tests for get_angles ran fine.

If tests for the get_angles function ran fine, the problem seems to be in the sin / cos calcuation. Check that you pass different columns of the angle_rads matrix as arguments to sine and cosine functions. Each column provides values for a single curve.

Please, remove the code from your post. That’s against the rules.

I’m having the same problem and I don’t quite understand what you’re saying. I’m passing the columns of angle rads to the sin and cos functions using 0::2 for both, when I try messing with them I get shape errors or steps backward, any help would be greatly appreciated.

edit, the only thing that responds with the same “sum of square pairs must = …” is -1::1

That’s a problem. 0::2 works for sine. 1::2 works for cosine - because sin uses (2i) and cos uses (2i + 1).

when I use 1::2 for cosine it doesn’t work. gives me a previous error .“Did you apply sin and cos to even and odd parts respectively?”.

There is other code that is involved, the 1::2 is just the indexing portion.

# code removed
1 Like