Dls5 week 4 error

I am stuck with the following error

ValueError Traceback (most recent call last)
in
29
30
—> 31 positional_encoding_test(positional_encoding)

in positional_encoding_test(target)
4 d_model = 16
5
----> 6 pos_encoding = target(position, d_model)
7 sin_part = pos_encoding[:, :, 0::2]
8 cos_part = pos_encoding[:, :, 1::2]

in positional_encoding(positions, d)
20
21 # apply sin to even indices in the array; 2i
—> 22 angle_rads[:, 0::2] = np.sin(get_angles(positions, dims, d))
23
24 # apply cos to odd indices in the array; 2i+1

ValueError: could not broadcast input array from shape (1,16) into shape (8,8)

First of all, you don’t have to call get_angles here, since you already did just above the line.
image
Secondly, look at the get_angles arguments pos and i, they are 2D array, however, the positions value you provided in get_angles function call is a scalar. You can use np.arange to generate sequence, and use np.newaxis to add a new dimension. Besides, dims is a global variable in previous unit test cell, you should not use it. Or you can reference get_angles_test function to see how to use get_angles.

2 Likes