C5 | W1 A3 |Improvise a Jazz Solo with LSTM network| Not understanding why getting this error

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

Hi @Shubham_Singh3 ,

Which part of the exercise is causing this error output?

With looking at errors, I see “a0” and “c0”. So, I suppose you are working on W1A3, Improvise a Jazz Solo with an LSTM Network. (note that it is better that you title includes this information.)

In this exercise, one of key components, LSTM_cell is defined as a global variable. And, this is easily broken. (Personally, this is not suitable for a programming exercise, because a learner may not complete programming without any errors…)

Anyway, here is a trick. At first, let’s check what kind of input that LSTM_cell expects with LSTM_cell.input_spec.

LSTM_cell.input_spec
InputSpec(ndim=3)

Then, after a completion of model creation by music_inference_model(), it becomes

LSTM_cell.input_spec
InputSpec(shape=(None, None, 90), ndim=3)

The problem is, if you fail to create a model with some bugs in music_inference_model(), it becomes

LSTM_cell.input_spec
[InputSpec(shape=(None, None, 90), ndim=3),
InputSpec(shape=(None, 64), ndim=2),
InputSpec(shape=(None, 64), ndim=2)]

If you run music_inference_model() again, then, it becomes,

LSTM_cell.input_spec
[InputSpec(shape=(None, None, 90), ndim=3),
InputSpec(shape=(None, 64), ndim=2),
InputSpec(shape=(None, 64), ndim=2),
InputSpec(shape=(None, 64), ndim=2),
InputSpec(shape=(None, 64), ndim=2)]

This results in changing error messages regardless where is the real bug in music_inference_model(). (In the other words, the real problem is hidden by this input spec change.)

So, if we call music_inference_model() again, again and again, the error message will change like this.

Layer lstm_15 expects 5 inputs, but it received 3 input tensors. Inputs received: …
Layer lstm_15 expects 7 inputs, but it received 3 input tensors. Inputs received: …
Layer lstm_15 expects 9 inputs, but it received 3 input tensors. Inputs received: …

We really can not understand what is happening…

Then, the first thing to do, after you get an error, is to reset LSTM_cell() to the initial condition. Of course, it also resets the result of training. So, once you complete implementation with no error, then, please re-run all cells above to train a model. The way to reset LSTM_cell() is;

LSTM_cell = LSTM(n_a, return_state = True)

Then, the first error is your real error. If you run again after the first error without resetting, then, you will fall into the above unnecessary error caused by LSTM_cell. So, during debugging, you are better to reset LSTM_cell everytime.

With the above, I suppose you can start “real” debugging on your code.

2 Likes

Week 1 3rd assignment