Music_inference_model dimension issue

Hi,
I am getting the following error

ValueError Traceback (most recent call last)
in
----> 1 inference_model = music_inference_model(LSTM_cell, densor, Ty = 50)

in music_inference_model(LSTM_cell, densor, Ty)
38 for t in range(Ty):
39 # Step 2.A: Perform one step of LSTM_cell. Use “x”, not “x0” (≈1 line)
—> 40 a, _, c = LSTM_cell(inputs=x, initial_state=[a, c])
41
42 # Step 2.B: Apply Dense layer to the hidden state output of the LSTM_cell (≈1 line)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/recurrent.py in call(self, inputs, initial_state, constants, **kwargs)
707 # Perform the call with temporarily replaced input_spec
708 self.input_spec = full_input_spec
→ 709 output = super(RNN, self).call(full_input, **kwargs)
710 # Remove the additional_specs from input spec and keep the rest. It is
711 # important to keep since the input spec was populated by build(), and

/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)
1090 # TODO(reedwm): We should assert input compatibility after the inputs
1091 # are casted, not before.
→ 1092 input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
1093 graph = backend.get_graph()
1094 # Use self._name_scope() to avoid auto-incrementing the name.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
156 str(len(input_spec)) + ’ inputs, ’
157 'but it received ’ + str(len(inputs)) +
→ 158 ’ input tensors. Inputs received: ’ + str(inputs))
159 for input_index, (x, spec) in enumerate(zip(inputs, input_spec)):
160 if spec is None:

ValueError: Layer lstm expects 7 inputs, but it received 3 input tensors. Inputs received: [<tf.Tensor ‘input_4:0’ shape=(None, 1, 90) dtype=float32>, <tf.Tensor ‘a0_3:0’ shape=(None, 64) dtype=float32>, <tf.Tensor ‘c0_3:0’ shape=(None, 64) dtype=float32>]

Welcome to the community.

In this assignment, LSTM_cell is used as a global variable, and used by music_inference_model().
If any errors occur in coding, then, LSTM_cell has an incorrect information in there and keeps those.

So, during coding/debugging, it is better to reset LSTM_cell with this.

LSTM_cell = LSTM(n_a, return_state = True)

Here is the details.

But, of course, this does not mean a root cause of an initial error is fixed. But, once you reset LSTM_cell, then, you can tackle a root cause of an error if exist.

Hope this helps.

By the way, is this an assignment 3 in the first week of the Course 5 ?

Hi,
Yeah it is the assignment 3 in the first week of the Course 5. I tried everything but no luck. Is it possible that I can show my code here so you may have a look ?

Please post your traceback after resetting LSTM_cell. That is the first step. Your previous post is not valid to find your real bug due to LSTM_cell issue that I explained.

WARNING:tensorflow:Functional inputs must come from tf.keras.Input (thus holding past layer metadata), they cannot be the output of a previous non-Input layer. Here, a tensor specified as input to “functional_2” was not an Input tensor, it was generated by layer repeat_vector_50.
Note that input tensors are instantiated via tensor = tf.keras.Input(shape).
The tensor that caused the issue was: repeat_vector_50/Tile:0
WARNING:tensorflow:Functional inputs must come from tf.keras.Input (thus holding past layer metadata), they cannot be the output of a previous non-Input layer. Here, a tensor specified as input to “functional_2” was not an Input tensor, it was generated by layer lstm_50.
Note that input tensors are instantiated via tensor = tf.keras.Input(shape).
The tensor that caused the issue was: lstm_50/PartitionedCall:0
WARNING:tensorflow:Functional inputs must come from tf.keras.Input (thus holding past layer metadata), they cannot be the output of a previous non-Input layer. Here, a tensor specified as input to “functional_2” was not an Input tensor, it was generated by layer lstm_50.
Note that input tensors are instantiated via tensor = tf.keras.Input(shape).
The tensor that caused the issue was: lstm_50/PartitionedCall:3

AssertionError Traceback (most recent call last)
in
----> 1 inference_model = music_inference_model(LSTM_cell, densor, Ty = 50)

in music_inference_model(LSTM_cell, densor, Ty)
57
58 # Step 3: Create model instance with the correct “inputs” and “outputs” (≈1 line)
—> 59 inference_model = Model(inputs=[x, a, c], outputs=outputs)
60
61 ### END CODE HERE ###

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in new(cls, *args, **kwargs)
240 # Functional model
241 from tensorflow.python.keras.engine import functional # pylint: disable=g-import-not-at-top
→ 242 return functional.Functional(*args, **kwargs)
243 else:
244 return super(Model, cls).new(cls, *args, **kwargs)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
455 self._self_setattr_tracking = False # pylint: disable=protected-access
456 try:
→ 457 result = method(self, *args, **kwargs)
458 finally:
459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py in init(self, inputs, outputs, name, trainable)
113 # ‘arguments during initialization. Got an unexpected argument:’)
114 super(Functional, self).init(name=name, trainable=trainable)
→ 115 self._init_graph_network(inputs, outputs)
116
117 @trackable.no_automatic_dependency_tracking

/opt/conda/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
455 self._self_setattr_tracking = False # pylint: disable=protected-access
456 try:
→ 457 result = method(self, *args, **kwargs)
458 finally:
459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py in _init_graph_network(self, inputs, outputs)
183 # and one tensor output.
184 assert node_index == 0
→ 185 assert tensor_index == 0
186 self._input_layers.append(layer)
187 self._input_coordinates.append((layer, node_index, tensor_index))

AssertionError:

OK, let’s do one by one.
To create a model, you need to specify inputs and outputs. Your inputs [x, a, c] are not actual inputs, but updated during iterations. At first, you need to set initial values for those.

Should I set zeros as initial values ?

I have initial values like these as of now
a0 = Input(shape=(n_a,), name=‘a0’)
c0 = Input(shape=(n_a,), name=‘c0’)
a = a0
c = c0
x = x0

So, what help do you need ? :slight_smile:
You have everything on the table. As I wrote, x, a, c are updated during iterations. For example, error message " Here, a tensor specified as input to “functional_2” was not an Input tensor, it was generated by layer repeat_vector_50." says that “x” is created by RepeatVector. So, it is not an input tensor. You do have initial values to be set to create a model.

I have completed the coding of function “music_inference_model” but it is generating errors. If possible than please have a look at my code. I am stuck at this point. I have done exactly what needs to be done but still errors

Did you set proper initial values for these ?

inference_model = Model(inputs=[x, a, c], outputs=outputs)

This is the first thing to do. Nothing complex. You only have one choice for each. This is the same advance even if I look at your code, as I can not tell the answer.

Yes I did set to initial values and now I am not getting errors in here “inference_model = music_inference_model(LSTM_cell, densor, Ty = 50)”
but I am getting following error when running “# UNIT TEST”
AssertionError Traceback (most recent call last)
in
1 # UNIT TEST
2 inference_summary = summary(inference_model)
----> 3 comparator(inference_summary, music_inference_model_out)

~/work/W1A3/test_utils.py in comparator(learner, instructor)
16 def comparator(learner, instructor):
17 if len(learner) != len(instructor):
—> 18 raise AssertionError(“Error in test. The lists contain a different number of elements”)
19 for index, a in enumerate(instructor):
20 b = learner[index]

AssertionError: Error in test. The lists contain a different number of elements

AssertionError: Error in test. The lists contain a different number of elements

This means that you could not create a model correctly. And, this is the first check in the test. Another test follows.

Please try this after a unit test.
summary(inference_model)

Then output should be;

[['InputLayer', [(None, 1, 90)], 0],
 ['InputLayer', [(None, 64)], 0],
 ['InputLayer', [(None, 64)], 0],
 ['LSTM',
  [(None, 64), (None, 64), (None, 64)],
  39680,
  [(None, 1, 90), (None, 64), (None, 64)],
  'tanh'],
 ['Dense', (None, 90), 5850, 'softmax'],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1],
 ['TensorFlowOpLayer', [(None,)], 0],
 ['TensorFlowOpLayer', [(None, 90)], 0],
 ['RepeatVector', (None, 1, 90), 0, 1]]

Please compare with your output and find differences. The error says the number of layers (elements) are different. But, I think you do not need to count. Just need to check first 10 layers or so, then, find missing layers or incorrect layers.

There is another issue… You can not post your code… Lot’s of restrictions, but need to follow…
Please remove that.

Good progress, actually. You need to fix one line.
Usually, we start the dimension check to see how each function can transform the dimension.

LSTM input x =(None, 1, 90)
x.shape after argmax =(None,)
x.shape after one_hot =(None, 90)
x.shape after repeatvec =(None, 1, 90)

With checking the dimension of each output, you will see which is wrong. :slight_smile:
And, please do not hard code the value.

Thank you
Issue resolved :slight_smile: