Help with C5W1 Jazz Improv with LSTM assignment

Link to assignment: Coursera | Online Courses & Credentials From Top Educators. Join for Free | Coursera

I need help with this assignment and I have already looked at previous posts and tried their suggestions.
In the unit test of after the definition of djmodel, I am getting an error which I think is due to the fact that the test does not expect the slice operation to appear as a layer in the model summary. I initially implemented the slice, as suggested in the notebook, using the python array index syntax, but I have also used tf.slice and tf.strided_slice and they also didn’t solve it. I have also played with changing method input and output params in all layers and it also didn’t help…
The error and the model summary are below:

Test failed at index 2 
 Expected value 

 ['TensorFlowOpLayer', [(None, 90)], 0] 

 does not match the input value: 

 ['Reshape', (None, 1, 90), 0]

Here is the output of the model summary:

Model: "model_1"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_2 (InputLayer)            [(None, 30, 90)]     0                                            
__________________________________________________________________________________________________
tf_op_layer_strided_slice_30 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_31 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_32 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_33 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_34 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_35 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_36 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_37 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_38 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_39 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_40 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_41 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_42 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_43 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_44 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_45 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_46 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_47 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_48 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_49 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_50 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_51 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_52 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_53 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_54 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_55 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_56 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_57 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_58 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
tf_op_layer_strided_slice_59 (T [(None, 90)]         0           input_2[0][0]                    
__________________________________________________________________________________________________
reshape (Reshape)               (None, 1, 90)        0           tf_op_layer_strided_slice_30[0][0
                                                                 tf_op_layer_strided_slice_31[0][0
                                                                 tf_op_layer_strided_slice_32[0][0
                                                                 tf_op_layer_strided_slice_33[0][0
                                                                 tf_op_layer_strided_slice_34[0][0
                                                                 tf_op_layer_strided_slice_35[0][0
                                                                 tf_op_layer_strided_slice_36[0][0
                                                                 tf_op_layer_strided_slice_37[0][0
                                                                 tf_op_layer_strided_slice_38[0][0
                                                                 tf_op_layer_strided_slice_39[0][0
                                                                 tf_op_layer_strided_slice_40[0][0
                                                                 tf_op_layer_strided_slice_41[0][0
                                                                 tf_op_layer_strided_slice_42[0][0
                                                                 tf_op_layer_strided_slice_43[0][0
                                                                 tf_op_layer_strided_slice_44[0][0
                                                                 tf_op_layer_strided_slice_45[0][0
                                                                 tf_op_layer_strided_slice_46[0][0
                                                                 tf_op_layer_strided_slice_47[0][0
                                                                 tf_op_layer_strided_slice_48[0][0
                                                                 tf_op_layer_strided_slice_49[0][0
                                                                 tf_op_layer_strided_slice_50[0][0
                                                                 tf_op_layer_strided_slice_51[0][0
                                                                 tf_op_layer_strided_slice_52[0][0
                                                                 tf_op_layer_strided_slice_53[0][0
                                                                 tf_op_layer_strided_slice_54[0][0
                                                                 tf_op_layer_strided_slice_55[0][0
                                                                 tf_op_layer_strided_slice_56[0][0
                                                                 tf_op_layer_strided_slice_57[0][0
                                                                 tf_op_layer_strided_slice_58[0][0
                                                                 tf_op_layer_strided_slice_59[0][0
__________________________________________________________________________________________________
a0 (InputLayer)                 [(None, 64)]         0                                            
__________________________________________________________________________________________________
c0 (InputLayer)                 [(None, 64)]         0                                            
__________________________________________________________________________________________________
lstm (LSTM)                     [(None, 64), (None,  39680       reshape[30][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[31][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[32][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[33][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[34][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[35][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[36][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[37][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[38][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[39][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[40][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[41][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[42][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[43][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[44][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[45][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[46][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[47][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[48][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[49][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[50][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[51][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[52][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[53][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[54][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[55][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[56][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[57][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[58][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
                                                                 reshape[59][0]                   
                                                                 a0[0][0]                         
                                                                 c0[0][0]                         
__________________________________________________________________________________________________
dense (Dense)                   (None, 90)           5850        lstm[30][1]                      
                                                                 lstm[31][1]                      
                                                                 lstm[32][1]                      
                                                                 lstm[33][1]                      
                                                                 lstm[34][1]                      
                                                                 lstm[35][1]                      
                                                                 lstm[36][1]                      
                                                                 lstm[37][1]                      
                                                                 lstm[38][1]                      
                                                                 lstm[39][1]                      
                                                                 lstm[40][1]                      
                                                                 lstm[41][1]                      
                                                                 lstm[42][1]                      
                                                                 lstm[43][1]                      
                                                                 lstm[44][1]                      
                                                                 lstm[45][1]                      
                                                                 lstm[46][1]                      
                                                                 lstm[47][1]                      
                                                                 lstm[48][1]                      
                                                                 lstm[49][1]                      
                                                                 lstm[50][1]                      
                                                                 lstm[51][1]                      
                                                                 lstm[52][1]                      
                                                                 lstm[53][1]                      
                                                                 lstm[54][1]                      
                                                                 lstm[55][1]                      
                                                                 lstm[56][1]                      
                                                                 lstm[57][1]                      
                                                                 lstm[58][1]                      
                                                                 lstm[59][1]                      
==================================================================================================
Total params: 45,530
Trainable params: 45,530
Non-trainable params: 0

It’s a little hard to read the output of the model summary as they show it. For clarity, I added the following cell to my notebook:

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

Here’s the first relevant part of the output I get with code that passes all the tests and the grader:

Generated model:
layer 0: ['InputLayer', [(None, 30, 90)], 0]
layer 1: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 2: ['Reshape', (None, 1, 90), 0]
layer 3: ['InputLayer', [(None, 64)], 0]
layer 4: ['InputLayer', [(None, 64)], 0]
layer 5: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 6: ['LSTM', [(None, 64), (None, 64), (None, 64)], 39680, [(None, 1, 90), (None, 64), (None, 64)], 'tanh']
layer 7: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 8: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 9: ['TensorFlowOpLayer', [(None, 90)], 0]

What do you see when you try that? You must have ordered the calls differently in your code to produce a different “compute graph”.

Also note that it is not necessary to use tf.slice in order to perform the slicing: they give you some hints in the instructions about how to do it just by indexing the array on the second (timestep) dimension.

Hi!
Thanks for the reply.
I did the slicing with the python array slice syntax (based on the hint) and I was just mentioning other methods I tried in my post.
Here’s what I get from running your code segment:

Generated model:
layer 0: ['InputLayer', [(None, 30, 90)], 0]
layer 1: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 2: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 3: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 4: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 5: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 6: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 7: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 8: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 9: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 10: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 11: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 12: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 13: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 14: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 15: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 16: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 17: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 18: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 19: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 20: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 21: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 22: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 23: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 24: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 25: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 26: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 27: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 28: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 29: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 30: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 31: ['Reshape', (None, 1, 90), 0]
layer 32: ['InputLayer', [(None, 64)], 0]
layer 33: ['InputLayer', [(None, 64)], 0]
layer 34: ['LSTM', [(None, 64), (None, 64), (None, 64)], 39680, [(None, 1, 90), (None, 64), (None, 64)], 'tanh']
layer 35: ['Dense', (None, 90), 5850, 'softmax']
Expected model:
layer 0: ['InputLayer', [(None, 30, 90)], 0]
layer 1: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 2: ['Reshape', (None, 1, 90), 0]
layer 3: ['InputLayer', [(None, 64)], 0]
layer 4: ['InputLayer', [(None, 64)], 0]
layer 5: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 6: ['LSTM', [(None, 64), (None, 64), (None, 64)], 39680, [(None, 1, 90), (None, 64), (None, 64)], 'tanh']
layer 7: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 8: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 9: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 10: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 11: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 12: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 13: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 14: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 15: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 16: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 17: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 18: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 19: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 20: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 21: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 22: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 23: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 24: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 25: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 26: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 27: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 28: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 29: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 30: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 31: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 32: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 33: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 34: ['TensorFlowOpLayer', [(None, 90)], 0]
layer 35: ['Dense', (None, 90), 5850, 'softmax']

It seems like mine is out of order for some reason. I didn’t change the order of ops from its initial ordering in the notebook, nor do I see any other logical ordering for them from what they are.

But your compute graph is clearly very different than the expected one, so you must be missing something in terms of translating the instructions into code.

I realize that may not be enough of a hint to be much help. Maybe it’s time to just look at your code. We can’t do that on a public thread, but please check your DMs for a message from me about how to proceed.

To close the loop on the public thread, the bug was one that I’d seen before, but I had unfortunately forgotten what effect that has on the compute graph. The problem was on the LSTM invocation in the for loop: the initial state was the same for every timestep (a0, c0). But that’s not how RNNs work: the cell state at the start of each timestep is the cell state that was output by the previous timestep, right?

1 Like