W1A3 jazz improvisation exercise 1 - implement djmodel()

In exercise 1 we are asked to implement the djmodel. I am getting the following error during the unit test.

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-36-2ad560a25ebc> in <module>
      3 # UNIT TEST
      4 output = summary(model)
----> 5 comparator(output, djmodel_out)

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

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

I believe I might be making a mistake when slicing X. I’ve been using x = X[t,:] but I believe I might not be doing it right or considering the true shape of the input.

That error is telling you that the number of layers in your model is incorrect, so it’s probably something more fundamental than the slicing of X.

The cell after the one that just “threw” prints out your model but the format is a bit ornate. Here’s a cell that I added to my notebook to print the “summary” of the expected model and my model with the layers numbered to make it a bit easier to see what is going on:

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}")

Thanks Paul, I’m seeing some of the expected layers in my generated model but in an incorrect order.

By looking at layer 1, I see that the shape of x is not what is expected. The notes indicate that the shape of the slice should be (n_values,) but I’m having difficulties putting that together by just slicing the tensor when the shape of X is (none, Tx, n_values). I suspect in other labs we used other methods to rearrange matrices but we’re working with tensors so I’m unsure if this is the right way. [Edited: by slicing X[:,t,:] I can produce the expected shape.]

The order of appearance of the layers is off and I am also unsure how to troubleshoot this.

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

Yes, it looks like you have the correct slicing by using the timestep dimension. The problem with the order of the layers means that your compute graph is somehow incorrect. The way the inputs and outputs are connected by the layer operations is somehow in the wrong order. I hope that you have not changed the fundamental structure of the “for” loop that they gave you in the template code. One thing to check is that you have the correct inputs and outputs on the LSTM invocation: at every timestep in the loop, the input is the state values from the previous timestep, not the initial state values, right?

I’m ruling out changes to the template after reloading the notebook. Therefore, I am looking at the LSTM call and realize that we need to use a and c instead of a0 and c0 because the a and c variables originally have the first assignment of a0 and c0 but will get updated. Thank you, Paul

1 Like

It’s great to hear that you found the problem based on that suggestion. Onward! :nerd_face: