Course 5: Week 1: Music inference model (LSTM)

Hey guys,
i have some problems with the inference model and i am not able to fix it.
When running my code it tells me:
Input 0 is incompatible with layer lstm: expected shape=(None, None, 90), found shape=[90, 1, 1]

Seems like something with the shapes is wrong.

When I run the block again the error changes to:
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>]

Restarting the kernel brings me back to the first error.

Actually I am quite confused. With the instructions of the task i have written the following:
a, _, c = LSTM_cell(x, initial_state=[a, c])

This is more or less the same like in task 2 C of the djmodel, where it works…

x, a and c are in this task predefined in the lines above as:
x0 = Input(shape=(1, n_values))
a0 = Input(shape=(n_a,), name=‘a0’)
c0 = Input(shape=(n_a,), name=‘c0’)

Updating to the latest version did not fix the problem, like in another thread. I think some shapes must be wrong, but i am not sure about it.
Can somebody help me?
Bests,
Hinnerk8

1 Like

I have the same error as you. I’m not able to go pass that.
I have also restarted the kernel but it is still not working.
Any help will be welcome.
Thanks,

2 Likes

I have still no idea. Spend the last two days to find the error but actually i do not find it. Quite frustrating. If anyone can help, i would be very thankful…

4 Likes

Hello,
I suspect that your x is shaped incorrectly, you can try to print x.shape for each step of the loop.
x is supposed to have the shape: (None, 1, 90), but apparently you got (90, 1, 1)

1 Like

I have a found solution! After 100000000 attempts!
You just need to put :

LSTM_cell = LSTM(n_a, return_state = True)

before this line on code :

inference_model = music_inference_model(LSTM_cell, densor,reshaper, Ty = 50)

I have noticed that the input size was adding up after each run, so reseting the LSTM_cell solve the problem,

18 Likes

Hi,

are you already solved music_inference_model function?

I have a problem in this graded function.

1 Like

Hey Dyxuki,
thanks for your reply.
Actually I have already checked the shapes and can not find the error. The djmodel returns me in the summary the following shape of x after reshaper: reshape (Reshape) (None, 1, 90)
I think this should be right, or not?
I am not touching x till the LSTM cell beside the two lines, which are already given:
x0 = Input(shape=(1, n_values))
x = x0
I do not get, why it tells me, wrong shape… Reseting the LSTM_cell did not solve my problem.
Do you have any further idea?

1 Like

yes I did the same and could not find a way , if you have solved it pls share some idea

1 Like

Has somebody of the mentors any idea? I am still interested in the solution…

1 Like

The shape of x might be changed before and after LSTM_cell. Since the error message mentioned incorrect shape at LSTM_cell, one way to debug the problem is print out x.shape anywhere it might be changed. For example:

    # Step 2: Loop over Ty and generate a value at every time step
    for t in range(None):
        # Step 2.A: Perform one step of LSTM_cell. Use "x", not "x0" (≈1 line)
        print('before LSTM_cell:', x.shape)
        a, _, c = None
        print('after LSTM_cell:', x.shape)
        
        # Step 2.B: Apply Dense layer to the hidden state output of the LSTM_cell (≈1 line)
        out = None
        # Step 2.C: Append the prediction "out" to "outputs". out.shape = (None, 90) (≈1 line)
        outputs.append(None)
 
        # Step 2.D: 
        # Select the next value according to "out",
        # Set "x" to be the one-hot representation of the selected value
        # See instructions above.
        x = None
        print('after argmax:', x.shape, ', must be (None,)')
        x = None
        print('after one_hot:', x.shape, ', must be (None, 90)')
        # Step 2.E: 
        # Use RepeatVector(1) to convert x into a tensor with shape=(None, 1, 90)
        x = None
        print('after RepeatVector:', x.shape)

Except argmax and one_hot will change x.shape, others must be (None, 1, 90). If somewhere is different, it might be the problem.

5 Likes

Thank you very, very much! I had the wrong axis in tf.argmax. Now its working!

2 Likes

You are my hero! That worked for me too!

I think the mentors/instructors should really revise their exercises. They seem to be many orders of magnitude more difficult than the lectures and quizzes. I mean really - how could any of us be expected to know that the code runs differently each time we run the cell?

Thanks a million, though, really. You really made my day.

5 Likes

This solved my problem as well, thank you so much!

1 Like

Thank you very much. It’s working now.

1 Like

thanks a lot, this really helped! although I still have no idea why…

1 Like

Could you please tell me what ‘axis’ value you used?

1 Like

Thanks for the hint. Problem for me still persists. I noticed you have an additional parameter ‘reshaper’ in your music_inference_model call. Mine does not have. Here is what I have:
inference_model = music_inference_model(LSTM_cell, densor, Ty = 50)
Is there a problem with the version (I’ve updated multiple times)?

Thanks!

2 Likes

For me it also worked, but I had to put the said line

LSTM_cell = LSTM(n_a, return_state = True)

before the following line:

def music_inference_model(LSTM_cell, densor, Ty=100):

At least the beginning of this Course 5 (RNN) is quite frustrating! I needed quite some extra time for this “week 1”.

3 Likes

I was having this problem as well until I restarted my kernel and reran all cells above. That’s the funny thing about using global state objects… Once they’re messed up, you sometimes have to rebuild everything to get them back to functional.

3 Likes

I have the exact same issue, i think there’s some versioning issue.

1 Like