Class EncoderLayer, C5_W4_A1_Transformer_Subclass_v1

I am stuck in the part UNQ_C4 ‘Class EncoderLayer’. Thanks for any suggestions.


InvalidArgumentError Traceback (most recent call last)
in
1 # UNIT TEST
----> 2 EncoderLayer_test(EncoderLayer)

~/work/W4A1/public_tests.py in EncoderLayer_test(target)
84 encoder_layer1 = target(4, 2, 8)
85 tf.random.set_seed(10)
—> 86 encoded = encoder_layer1(q, True, np.array([[1, 0, 1]]))
87
88 assert tf.is_tensor(encoded), “Wrong type. Output must be a tensor”

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in call(self, *args, **kwargs)
1010 with autocast_variable.enable_auto_cast_variables(
1011 self._compute_dtype_object):
→ 1012 outputs = call_fn(inputs, *args, **kwargs)
1013
1014 if self._activity_regularizer:

in call(self, x, training, mask)
48 # apply layer normalization on sum of the input and the attention output to get the
49 # output of the multi-head attention layer (~1 line)
—> 50 out1 = self.layernorm1(tf.math.add(x, attn_output) ) # (batch_size, input_seq_len, fully_connected_dim)
51 # pass the output of the multi-head attention layer through a ffn (~1 line)
52 ffn_output = self.ffn(out1) # (batch_size, input_seq_len, fully_connected_dim)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py in add(x, y, name)
330 try:
331 return add_eager_fallback(
→ 332 x, y, name=name, ctx=_ctx)
333 except _core._SymbolicException:
334 pass # Add nodes to the TensorFlow graph.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py in add_eager_fallback(x, y, name, ctx)
364
365 def add_eager_fallback(x, y, name, ctx):
→ 366 _attr_T, _inputs_T = _execute.args_to_matching_eager([x, y], ctx, [_dtypes.bfloat16, _dtypes.half, _dtypes.float32, _dtypes.float64, _dtypes.uint8, _dtypes.int8, _dtypes.int16, _dtypes.int32, _dtypes.int64, _dtypes.complex64, _dtypes.complex128, _dtypes.string, ])
367 (x, y) = _inputs_T
368 _inputs_flat = [x, y]

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/execute.py in args_to_matching_eager(l, ctx, allowed_dtypes, default_dtype)
278 dtype = tensor.dtype
279 else:
→ 280 ret = [ops.convert_to_tensor(t, dtype, ctx=ctx) for t in l]
281
282 # TODO(slebedev): consider removing this as it leaks a Keras concept.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/execute.py in (.0)
278 dtype = tensor.dtype
279 else:
→ 280 ret = [ops.convert_to_tensor(t, dtype, ctx=ctx) for t in l]
281
282 # TODO(slebedev): consider removing this as it leaks a Keras concept.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/profiler/trace.py in wrapped(*args, **kwargs)
161 with Trace(trace_name, **trace_kwargs):
162 return func(*args, **kwargs)
→ 163 return func(*args, **kwargs)
164
165 return wrapped

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
1538
1539 if ret is None:
→ 1540 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1541
1542 if ret is NotImplemented:

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py in _autopacking_conversion_function(v, dtype, name, as_ref)
1523 elif dtype != inferred_dtype:
1524 v = nest.map_structure(_cast_nested_seqs_to_dtype(dtype), v)
→ 1525 return _autopacking_helper(v, dtype, name or “packed”)
1526
1527

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py in _autopacking_helper(list_or_tuple, dtype, name)
1429 # checking.
1430 if all(isinstance(elem, core.Tensor) for elem in list_or_tuple):
→ 1431 return gen_array_ops.pack(list_or_tuple, name=name)
1432 must_pack = False
1433 converted_elems =

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py in pack(values, axis, name)
6378 return _result
6379 except _core._NotOkStatusException as e:
→ 6380 _ops.raise_from_not_ok_status(e, name)
6381 except _core._FallbackException:
6382 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: Shapes of all inputs must match: values[0].shape = [1,3,4] != values[1].shape = [1,2,3,3] [Op:Pack] name: packed

In layernorm1(), try just using the ‘+’ operator. Not the tensor flow add function.

Thanks!
Now I receive the error:

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

~/work/W4A1/public_tests.py in EncoderLayer_test(target)
97 assert np.allclose(encoded.numpy(), [[ 0.5167701 , -0.92981905, -0.9731106 , 1.3861597 ],
98 [-1.120878 , 1.0826552 , -0.8671041 , 0.905327 ],
—> 99 [ 0.28154755, -0.3661362 , -1.3330412 , 1.4176297 ]]), “Wrong values when training=False”
100 print("\033[92mAll tests passed")
101

AssertionError: Wrong values when training=False

I am passing training=training, return_attention_scores=False in self.mha(). I am wondering if the error is due to these arguments. Thanks.

I am finally able to debug and the part of the code works!

@Ari_M without giving away code, would you mind sharing your solution ? I think I’m in the same spot. :slight_smile:

1 Like

::bump::
@Barb @Ari_M
Were y’all able to figure this out?
I’m in the same boat.

Hey,
It’s been a while so I don’t remember how I did it.
However, I remember following some link about the original tutorial and copy pasting.
Transformers are definitely something that was tough to handle, even less actually learning.
Look around for a link towards that content.
Sorry I couldn’t help more.