C5W3A1 Exercise 2 - modelf

Problem at C5W3A1 Exercise 2 - modelf

Please post the entire error trace.

This is the Neural Machine Translation assignment. Both of those shapes appear in the logic of the model, so it might be necessary to see the initial part of the exception trace to understand where in the logic the error is happening. I added some print statements to my code and here’s what I see when I run that test cell:

before repeator s_prev.shape = (None, 64)
after repeator s_prev.shape = (None, 30, 64)
before repeator s_prev.shape = (None, 64)
after repeator s_prev.shape = (None, 30, 64)
before repeator s_prev.shape = (None, 64)
after repeator s_prev.shape = (None, 30, 64)
before repeator s_prev.shape = (None, 64)
after repeator s_prev.shape = (None, 30, 64)
before repeator s_prev.shape = (None, 64)
after repeator s_prev.shape = (None, 30, 64)
before repeator s_prev.shape = (None, 64)
after repeator s_prev.shape = (None, 30, 64)
before repeator s_prev.shape = (None, 64)
after repeator s_prev.shape = (None, 30, 64)
before repeator s_prev.shape = (None, 64)
after repeator s_prev.shape = (None, 30, 64)
before repeator s_prev.shape = (None, 64)
after repeator s_prev.shape = (None, 30, 64)
before repeator s_prev.shape = (None, 64)
after repeator s_prev.shape = (None, 30, 64)
[['InputLayer', [(None, 30, 37)], 0], ['InputLayer', [(None, 64)], 0], ['Bidirectional', (None, 30, 64), 17920], ['RepeatVector', (None, 30, 64), 0, 30], ['Concatenate', (None, 30, 128), 0], ['Dense', (None, 30, 10), 1290, 'tanh'], ['Dense', (None, 30, 1), 11, 'relu'], ['Activation', (None, 30, 1), 0], ['Dot', (None, 1, 64), 0], ['InputLayer', [(None, 64)], 0], ['LSTM', [(None, 64), (None, 64), (None, 64)], 33024, [(None, 1, 64), (None, 64), (None, 64)], 'tanh'], ['Dense', (None, 11), 715, 'softmax']]
All tests passed!

I added another cell to print the model in a more readable form:

print("Generated model:")
    for index, a in enumerate(summary(model)):
        print(f"layer {index}: {a}")
    print("Expected model:")
    for index, a in enumerate(expected_summary):
        print(f"layer {index}: {a}")

Here’s what I get:

Generated model:
layer 0: ['InputLayer', [(None, 30, 37)], 0]
layer 1: ['InputLayer', [(None, 64)], 0]
layer 2: ['Bidirectional', (None, 30, 64), 17920]
layer 3: ['RepeatVector', (None, 30, 64), 0, 30]
layer 4: ['Concatenate', (None, 30, 128), 0]
layer 5: ['Dense', (None, 30, 10), 1290, 'tanh']
layer 6: ['Dense', (None, 30, 1), 11, 'relu']
layer 7: ['Activation', (None, 30, 1), 0]
layer 8: ['Dot', (None, 1, 64), 0]
layer 9: ['InputLayer', [(None, 64)], 0]
layer 10: ['LSTM', [(None, 64), (None, 64), (None, 64)], 33024, [(None, 1, 64), (None, 64), (None, 64)], 'tanh']
layer 11: ['Dense', (None, 11), 715, 'softmax']
Expected model:
layer 0: ['InputLayer', [(None, 30, 37)], 0]
layer 1: ['InputLayer', [(None, 64)], 0]
layer 2: ['Bidirectional', (None, 30, 64), 17920]
layer 3: ['RepeatVector', (None, 30, 64), 0, 30]
layer 4: ['Concatenate', (None, 30, 128), 0]
layer 5: ['Dense', (None, 30, 10), 1290, 'tanh']
layer 6: ['Dense', (None, 30, 1), 11, 'relu']
layer 7: ['Activation', (None, 30, 1), 0]
layer 8: ['Dot', (None, 1, 64), 0]
layer 9: ['InputLayer', [(None, 64)], 0]
layer 10: ['LSTM', [(None, 64), (None, 64), (None, 64)], 33024, [(None, 1, 64), (None, 64), (None, 64)], 'tanh']
layer 11: ['Dense', (None, 11), 715, 'softmax']
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
   1811   try:
-> 1812     c_op = pywrap_tf_session.TF_FinishOperation(op_desc)
   1813   except errors.InvalidArgumentError as e:

InvalidArgumentError: Shape must be rank 2 but is rank 3 for '{{node concatenate/concat}} = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32](bidirectional/concat, repeat_vector/Tile, concatenate/concat/axis)' with input shapes: [?,64], [?,30,64], [].

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-12-1492e4d59c1c> in <module>
     34 
     35 
---> 36 modelf_test(modelf)

<ipython-input-12-1492e4d59c1c> in modelf_test(target)
     11 
     12 
---> 13     model = target(Tx, Ty, n_a, n_s, len_human_vocab)
     14 
     15     print(summary(model))

<ipython-input-11-c76f00690ede> in modelf(Tx, Ty, n_a, n_s, human_vocab_size, machine_vocab_size)
     42 
     43         # Step 2.A: Perform one step of the attention mechanism to get back the context vector at step t (≈ 1 line)
---> 44         context = one_step_attention(a, s)
     45 
     46         # Step 2.B: Apply the post-attention LSTM cell to the "context" vector. (≈ 1 line)

<ipython-input-8-03dbbcad282b> 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)
    924     if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
    925       return self._functional_construction_call(inputs, args, kwargs,
--> 926                                                 input_list)
    927 
    928     # Maintains info about the `Layer.call` stack.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
   1115           try:
   1116             with ops.enable_auto_cast_variables(self._compute_dtype_object):
-> 1117               outputs = call_fn(cast_inputs, *args, **kwargs)
   1118 
   1119           except errors.OperatorNotAllowedInGraphError as e:

/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)
   1220   _attr_N = len(values)
   1221   _, _, _op, _outputs = _op_def_library._apply_op_helper(
-> 1222         "ConcatV2", values=values, axis=axis, name=name)
   1223   _result = _outputs[:]
   1224   if _execute.must_record_gradient():

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(op_type_name, name, **keywords)
    742       op = g._create_op_internal(op_type_name, inputs, dtypes=None,
    743                                  name=scope, input_types=input_types,
--> 744                                  attrs=attr_protos, op_def=op_def)
    745 
    746     # `outputs` is returned as a separate return value so that the output

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device)
    591     return super(FuncGraph, self)._create_op_internal(  # pylint: disable=protected-access
    592         op_type, inputs, dtypes, input_types, name, attrs, op_def,
--> 593         compute_device)
    594 
    595   def capture(self, tensor, name=None, shape=None):

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device)
   3483           input_types=input_types,
   3484           original_op=self._default_original_op,
-> 3485           op_def=op_def)
   3486       self._create_op_helper(ret, compute_device=compute_device)
   3487     return ret

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in __init__(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
   1973         op_def = self._graph._get_op_def(node_def.op)
   1974       self._c_op = _create_c_op(self._graph, node_def, inputs,
-> 1975                                 control_input_ops, op_def)
   1976       name = compat.as_str(node_def.name)
   1977     # pylint: enable=protected-access

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
   1813   except errors.InvalidArgumentError as e:
   1814     # Convert to ValueError for backwards compatibility.
-> 1815     raise ValueError(str(e))
   1816 
   1817   return c_op

ValueError: Shape must be rank 2 but is rank 3 for '{{node concatenate/concat}} = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32](bidirectional/concat, repeat_vector/Tile, concatenate/concat/axis)' with input shapes: [?,64], [?,30,64], [].

I would guess there is an error in the value of ‘a’ in this line:

context = one_step_attention(a, s)

It is set in the prior line a = Bidirectional(LSTM(...

Instructions for that line are here:

2 Likes

Oh yes yes.
I forget to set return_sequences=True.

Thank you sir.

1 Like