I’m getting the below error on my implementation of scaled_dot_product_attention.
I’ve done the ffg:
- used np.matmul() to combine q and k
- used k.shape[-1] and np.sqrt() to scale the above to get scaled_attention_logits
- Applied this as the mask: (1.0 - mask) * -1e9
- Used the ffg with axis=-1 to get the softmax: tf.keras.activations.softmax()
- then used np.matmul() to combine attention_weights and v to get the output.
Please can someone assist?
AttributeError 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)
55 v = np.array([[0, 0], [1, 0], [1, 0], [1, 1]]).astype(np.float32)
56
—> 57 attention, weights = target(q, k, v, None)
58 assert tf.is_tensor(weights), “Weights must be a tensor”
59 assert tuple(tf.shape(weights).numpy()) == (q.shape[0], k.shape[1]), f"Wrong shape. We expected ({q.shape[0]}, {k.shape[1]})"
in scaled_dot_product_attention(q, k, v, mask)
33 # softmax is normalized on the last axis (seq_len_k) so that the scores
34 # add up to 1.
—> 35 attention_weights = tf.keras.activations.softmax(scaled_attention_logits, axis=-1)
36 # (…, seq_len_q, seq_len_k)
37
/opt/conda/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
199 “”“Call target, and fall back on dispatchers if there is a TypeError.”“”
200 try:
→ 201 return target(*args, **kwargs)
202 except (TypeError, ValueError):
203 # Note: convert_to_eager_tensor currently raises a ValueError, not a
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/activations.py in softmax(x, axis)
72 ValueError: In case dim(x) == 1
.
73 “”"
—> 74 rank = x.shape.rank
75 if rank == 2:
76 output = nn.softmax(x)
AttributeError: ‘tuple’ object has no attribute ‘rank’