C5_W4_A1-UNQ_C3 scaled_dot_product_attention()

After completing the above function I get the following error message, please help, thanks in advance.

InvalidArgumentError 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)
69
70 mask = np.array([[[1, 1, 0, 1], [1, 1, 0, 1], [1, 1, 0, 1]]])
β€”> 71 attention, weights = target(q, k, v, mask)
72
73 assert np.allclose(weights, [[0.30719590187072754, 0.5064803957939148, 0.0, 0.18632373213768005],

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=dk) # (…, seq_len_q, seq_len_k)
36
37 output = tf.matmul(attention_weights, v) # (…, seq_len_q, depth_v)

/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)
76 output = nn.softmax(x)
77 elif rank > 2:
β€”> 78 e = math_ops.exp(x - math_ops.reduce_max(x, axis=axis, keepdims=True))
79 s = math_ops.reduce_sum(e, axis=axis, keepdims=True)
80 output = e / s

/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/ops/math_ops.py in reduce_max(input_tensor, axis, keepdims, name)
2744 β€œβ€"
2745 return reduce_max_with_dims(input_tensor, axis, keepdims, name,
β†’ 2746 _ReductionDims(input_tensor, axis))
2747
2748

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py in reduce_max_with_dims(input_tensor, axis, keepdims, name, dims)
2755 return _may_reduce_to_scalar(
2756 keepdims, axis,
β†’ 2757 gen_math_ops._max(input_tensor, dims, keepdims, name=name))
2758
2759

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py in _max(input, axis, keep_dims, name)
5612 return _result
5613 except _core._NotOkStatusException as e:
β†’ 5614 _ops.raise_from_not_ok_status(e, name)
5615 except _core._FallbackException:
5616 pass

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
6860 message = e.message + (" name: " + name if name is not None else β€œβ€)
6861 # pylint: disable=protected-access
β†’ 6862 six.raise_from(core._status_to_exception(e.code, message), None)
6863 # pylint: enable=protected-access
6864

/opt/conda/lib/python3.7/site-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: Invalid reduction dimension (4 for input with 3 dimension(s) [Op:Max]

Hi @hvjrocks ,

The problem is in the call for softmax where the axis is set to dk.
There is no need for that.
attention_weights = tf.keras.activations.softmax(scaled_attention_logits, axis=dk)

1 Like