C5 W4 A1:Scaled dot product attention error

Good day, Please kindly help i am getting the following error;

AssertionError Traceback (most recent call last)
in
1 # UNIT TEST
----> 2 scaled_dot_product_attention_test(scaled_dot_product_attention)

~/work/W4A1/public_tests.py in scaled_dot_product_attention_test(target)
60 assert np.allclose(weights, [[0.2589478, 0.42693272, 0.15705977, 0.15705977],
61 [0.2772748, 0.2772748, 0.2772748, 0.16817567],
—> 62 [0.33620113, 0.33620113, 0.12368149, 0.2039163 ]])
63
64 assert tf.is_tensor(attention), “Output must be a tensor”

AssertionError:

Looks like the weights value returned by scaled_dot_product_attention() is incorrect.

As suggested in the post it looks like the weights aren’t getting calculated correctly (output is already a tensor).
Can somebody help me in finding where it could have gone wrong?

Here’s my error:

AssertionError Traceback (most recent call last)
in
1 # UNIT TEST
----> 2 scaled_dot_product_attention_test(scaled_dot_product_attention)

~/work/W4A1/public_tests.py in scaled_dot_product_attention_test(target)
60 assert np.allclose(weights, [[0.2589478, 0.42693272, 0.15705977, 0.15705977],
61 [0.2772748, 0.2772748, 0.2772748, 0.16817567],
—> 62 [0.33620113, 0.33620113, 0.12368149, 0.2039163 ]])
63
64 assert tf.is_tensor(attention), “Output must be a tensor”

AssertionError:

Hi @ydsk1234

Check this or this out for previous experience. If they are not relevant to your situation, search the community with the keyword “0.33620113” for others’ posts which are likely to be sharing the same error message.

Raymond

I am doing the following to calculate dk (𝑑𝑘 is the dimension of the keys), and I am not sure if it’s correct:

dk = k.ndim

could someone please explain what’s wrong with the above, if any?

Hi @ydsk1234 ,

k.dim will give you the number of dimensions in k.
k.shape will give you the number of elements in each of the dimensions in k

for example:
k = [[1,2,3],[7,8,9]] → k.ndim = 2 two dimensions
k.shape = (2,3) two dimensions, 2 elements in row and 3 elements in column
k = [1,2,3] → k.ndim = 1
k.shape = (3,) one dimension with 3 elements (here, we don’t know if this is a row vector or a column vector)