Week 1 Assignment 3 Jazz LSTM - Exercise 2: music_inference_model

Hi there!

I am stuck in assignment Improvise_a_Jazz_Solo_with_an_LSTM_Network_v4 Exercise 2: music_inference_model:

Step 2.A: Perform one step of LSTM_cell. Use “x”, not “x0” (≈1 line)
_, a, c = LSTM_cell(x, initial_state=[a,c])

I get the error

ValueError: Input 0 is incompatible with layer lstm: expected shape=(None, None, 90), found shape=[90, 1, 1]

Any idea how to fix that?

Please share your full error.

Maybe your code for “x = …” is not correct.

If you add “print("shape of x:",x.shape)”, what do you get?
It should be (None, 90)


ValueError Traceback (most recent call last)
in
1 ### YOU CANNOT EDIT THIS CELL
----> 2 inference_model = music_inference_model(LSTM_cell, densor, Ty = 50)

in music_inference_model(LSTM_cell, densor, Ty)
39 for t in range(Ty):
40 # Step 2.A: Perform one step of LSTM_cell. Use “x”, not “x0” (≈1 line)
—> 41 _, a, c = LSTM_cell(x, initial_state=[a,c])
42
43 # 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)
225 ’ is incompatible with layer ’ + layer_name +
226 ‘: expected shape=’ + str(spec.shape) +
→ 227 ‘, found shape=’ + str(shape))
228
229

ValueError: Input 0 is incompatible with layer lstm: expected shape=(None, None, 90), found shape=[90, 1, 1]

shape of x: (None, 1, 90)

I pass the variable x to the LSTM cell without modification:
_, a, c = LSTM_cell(x, initial_state=[a,c])

The shape of x is correct. This is my output:

x: Tensor("input_2:0", shape=(None, 1, 90), dtype=float32)
a: Tensor("a0_1:0", shape=(None, 64), dtype=float32)
c: Tensor("c0_1:0", shape=(None, 64), dtype=float32)

Your code of LSTM_cell is correct. Note that x, a, and c are already given to you. Have you made any changes to them, or add something else? If not, try Kernel → Restart and then run all the cells again.

I did not change x, x0, a, a0, c or c0.
Also, I restarted the kernel several times.

Running the faulty code cell the first, the error is:

ValueError: Input 0 is incompatible with layer lstm: expected shape=(None, None, 90), found shape=[90, 1, 1]

Running it a second time, the error changes:

ValueError: Layer lstm expects 5 inputs, but it received 3 input tensors. Inputs received: [<tf.Tensor 'input_3:0' shape=(None, 1, 90) dtype=float32>, <tf.Tensor 'a0_2:0' shape=(None, 64) dtype=float32>, <tf.Tensor 'c0_2:0' shape=(None, 64) dtype=float32>]

and so on (expected inputs increases, received input increases):

  • 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>]
  • ValueError: Layer lstm expects 9 inputs, but it received 3 input tensors. Inputs received: [<tf.Tensor 'input_5:0' shape=(None, 1, 90) dtype=float32>, <tf.Tensor 'a0_4:0' shape=(None, 64) dtype=float32>, <tf.Tensor 'c0_4:0' shape=(None, 64) dtype=float32>]

Send me your code of music_inference_model in a private message. Click my name and message.

Did that. Thanks for helping!

OK. Your # Step 2.D is not correct. Double-check the instructions:

2.D: Convert the last output into a new input for the next time step. You will do this in 2 substeps:

  • Get the index of the maximum value of the predicted output using tf.math.argmax along the last axis. [How to do along the last axis?]
  • Convert the index into its n_values-one-hot encoding using tf.one_hot. [You may don’t know how to use tf.one_hot. Read this documentation].

Also, your implementation of inference_model is wrong. What should be outputs equals to?

Thank you, it works now.

I was focusing on the line _, a, c = LSTM_cell(x, initial_state=[a,c]), knowing that I had to fix the lines afterwards.

So when errors are thrown, that relate to certain line, it can be affected by wrong code for later lines?

Anyways, thank you for your help!

Note that we are using the for loop. The later wrong line code will feed to the former line after each iteration, right?

You have to pass next_hidden_state to the densor. Do you know what is the notation for next_hidden_state in the assignment?

Can you tell me what is out?

I don’t understand this. Can you please explain it again?

  • what to pass in the depth for tf.one_hot ? i think we should pass the out.shape[1] as depth , since we are trying to output the max output as one hot vector, but it is giving a error

From the notebook, two sentences are:

  • Convert the index into its n_values-one-hot encoding using tf.one_hot.
  • Set “x” to be the one-hot representation of the selected value
  • you mean depth is n_values?

{Moderator Edit: Solution Code Removed}

  • is it like this, i am not able to understand, what do you mean by selected value?

Yes.

It’s x.

PS: Please note that sharing your code is not allowed.

@saifkhanengr Regarding the incremental lstm input expectation, I found that I have to restart my kernal and run all after I make new edits to the code. is that the most efficient way or is there a way to reset the lstm layer everytime? It looks like it’s keeping in memory the expected number of inputs each time +2

  • 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>]

Simply re-running the code cell that creates the LSTM_cell object is generally sufficient.

This is necessary because LSTM_cell is a global object, and there is no method for un-doing any later code in the notebook that modifies it.