Week 3 assignment 1 UNQ_C1 how to use repeator?

how should I use the repeator?
the s_prev just equals to repeator ?
I always got the following error.


ValueError Traceback (most recent call last)
in
19 print("\033[92mAll tests passed!")
20
—> 21 one_step_attention_test(one_step_attention)

in one_step_attention_test(target)
9 a = np.random.uniform(1, 0, (m, Tx, 2 * n_a)).astype(np.float32)
10 s_prev =np.random.uniform(1, 0, (m, n_s)).astype(np.float32) * 1
—> 11 context = target(a, s_prev)
12
13 assert type(context) == tf.python.framework.ops.EagerTensor, “Unexpected type. It should be a Tensor”

in one_step_attention(a, s_prev)
20 # Use concatenator to concatenate a and s_prev on the last axis (≈ 1 line)
21 # For grading purposes, please list ‘a’ first and ‘s_prev’ second, in this order.
—> 22 concat = concatenator([a,s_prev])
23 # Use densor1 to propagate concat through a small fully-connected neural network to compute the “intermediate energies” variable e. (≈1 lines)
24 e = densor1(concat)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in call(self, *args, **kwargs)
983
984 with ops.enable_auto_cast_variables(self._compute_dtype_object):
→ 985 outputs = call_fn(inputs, *args, **kwargs)
986
987 if self._activity_regularizer:

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/merge.py in call(self, inputs)
181 return y
182 else:
→ 183 return self._merge_function(inputs)
184
185 @tf_utils.shape_type_conversion

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/merge.py in _merge_function(self, inputs)
520
521 def _merge_function(self, inputs):
→ 522 return K.concatenate(inputs, axis=self.axis)
523
524 @tf_utils.shape_type_conversion

/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/backend.py in concatenate(tensors, axis)
2879 return ragged_concat_ops.concat(tensors, axis)
2880 else:
→ 2881 return array_ops.concat([to_dense(x) for x in tensors], axis)
2882
2883

/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/array_ops.py in concat(values, axis, name)
1652 dtype=dtypes.int32).get_shape().assert_has_rank(0)
1653 return identity(values[0], name=name)
→ 1654 return gen_array_ops.concat_v2(values=values, axis=axis, name=name)
1655
1656

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py in concat_v2(values, axis, name)
1210 try:
1211 return concat_v2_eager_fallback(
→ 1212 values, axis, name=name, ctx=_ctx)
1213 except _core._SymbolicException:
1214 pass # Add nodes to the TensorFlow graph.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py in concat_v2_eager_fallback(values, axis, name, ctx)
1240 "‘concat_v2’ Op, not r." values)
1241 _attr_N = len(values)
→ 1242 _attr_T, values = _execute.args_to_matching_eager(list(values), ctx)
1243 _attr_Tidx, (axis,) = _execute.args_to_matching_eager([axis], ctx, _dtypes.int32)
1244 _inputs_flat = list(values) + [axis]

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/execute.py in args_to_matching_eager(l, ctx, default_dtype)
265 dtype = ret[-1].dtype
266 else:
→ 267 ret = [ops.convert_to_tensor(t, dtype, ctx=ctx) for t in l]
268
269 # 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)
265 dtype = ret[-1].dtype
266 else:
→ 267 ret = [ops.convert_to_tensor(t, dtype, ctx=ctx) for t in l]
268
269 # TODO(slebedev): consider removing this as it leaks a Keras concept.

/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)
1497
1498 if ret is None:
→ 1499 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1500
1501 if ret is NotImplemented:

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
336 as_ref=False):
337 _ = as_ref
→ 338 return constant(v, dtype=dtype, name=name)
339
340

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name)
262 “”"
263 return _constant_impl(value, dtype, shape, name, verify_shape=False,
→ 264 allow_broadcast=True)
265
266

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
273 with trace.Trace(“tf.constant”):
274 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
→ 275 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
276
277 g = ops.get_default_graph()

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
298 def _constant_eager_impl(ctx, value, dtype, shape, verify_shape):
299 “”“Implementation of eager constant.”""
→ 300 t = convert_to_eager_tensor(value, ctx, dtype)
301 if shape is None:
302 return t

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
96 dtype = dtypes.as_dtype(dtype).as_datatype_enum
97 ctx.ensure_initialized()
—> 98 return ops.EagerTensor(value, ctx.device_name, dtype)
99
100

ValueError: Attempt to convert a value (<tensorflow.python.keras.layers.core.RepeatVector object at 0x7f82b8a54f10>) with an unsupported type (<class ‘tensorflow.python.keras.layers.core.RepeatVector’>) to a Tensor.

RepeatVector is a callable. Here’s a sample invocation: repeat_vector_instance(my_tensor).

Now that you know how to invoke, can you figure out how to use it to concatenate a with s_prev?

1 Like

You have to pass repeator() an argument.
Pass it the name of the layer you want to repeat.
The comment in the notebook above that line of code tells you what layer to use.

1 Like

If it can help someone, here’s the mistake I made with repeator.
In the code above the assignment, repeator is already defined to repeat some layer Tx times. So now we are supposed to apply repeator to the layer we want to repeat.
On the contrary, I would pass Tx as the argument to repeator and kept wondering why I would receive an error message …
Ivan