# Course 5, Week 4 , Exercise 3

On applying this :

``````# START CODE HERE
{mentor edit: code removed}
# END CODE HERE
``````

InvalidArgumentError Traceback (most recent call last)
in
30 print("\033[92mAll tests passed")
31
β> 32 scaled_dot_product_attention_test(scaled_dot_product_attention)

in scaled_dot_product_attention_test(target)
5 v = np.array([[0, 0], [1, 0], [1, 0], [1, 1]]).astype(np.float32)
6
----> 7 attention, weights = target(q, k, v, None)
8 assert tf.is_tensor(weights), βWeights must be a tensorβ
9 assert tuple(tf.shape(weights).numpy()) == (q.shape[0], k.shape[1]), f"Wrong shape. We expected ({q.shape[0]}, {k.shape[1]})"

36 attention_weights = tf.nn.softmax(scaled_attention_logits*-1e9,axis=-1) # (β¦, seq_len_q, seq_len_k)
37 # attention_weights * V
β> 38 output = attention_weights*v # (β¦, seq_len_q, depth_v)
39
40 # END CODE HERE

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py in binary_op_wrapper(x, y)
1162 with ops.name_scope(None, op_name, [x, y]) as name:
1163 try:
β 1164 return func(x, y, name=name)
1165 except (TypeError, ValueError) as e:
1166 # Even if dispatching the op failed, the RHS may be a tensor aware

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py in _mul_dispatch(x, y, name)
1494 return sparse_tensor.SparseTensor(y.indices, new_vals, y.dense_shape)
1495 else:
β 1496 return multiply(x, y, name=name)
1497
1498

/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 multiply(x, y, name)
516 ββ"
517
β 518 return gen_math_ops.mul(x, y, name)
519
520

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py in mul(x, y, name)
6066 return _result
6067 except _core._NotOkStatusException as e:
β 6068 _ops.raise_from_not_ok_status(e, name)
6069 except _core._FallbackException:
6070 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: Incompatible shapes: [3,4] vs. [4,2] [Op:Mul]

1. -1e9 multiplication should apply to mask before adding to the weight.

2. You should use tf.linalg.matmul to do the multiplication for the last step

3 Likes

@Sameep
Please edit your message to remove the code. That will clear you with the course Honor Code.

1 Like