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

Here’s my code for the music_inference_model:

{code removed by mentor}

I am getting this error:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-29-7ced16cd24e3> in <module>
      3 # UNIT TEST
      4 inference_summary = summary(inference_model)
----> 5 comparator(inference_summary, music_inference_model_out)

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

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

Can someone help me, as to what is wrong? Tried several things, but can’t get past this assignment

Hi @chandank

Welcome to the community.

What course are you referring to? It turns out that you posted in the general category.

Best regards

Thanks @elirod! I will move it to DLS Course 5, and @chandank, please post in the right category next time :wink:

Let’s go through the error message:

This tells us the test is just comparing the lengths of your output, which is learner, which is inference_summary, to the model answer, which is music_inference_model_out, and as the message said, they have different lengths.

So, to precisely find out where the problem is, I recommend you to do a visual check on the elements in both lists - inference_summary and music_inference_model_out, pair by pair, to find out where starts to go wrong.

Now, I know the list in the model answer has over 150 items, so it will be a nightmare, but we can write some code to aid ourselves.

for i in range(max(len(inference_summary), len(music_inference_model_out))):
    print(f'layer number: {i}')
    print(inference_summary[i])
    print(music_inference_model_out[i])
    print('')

This is the basic version, it still requires you to visually compare each pair of items, but you can improve it to make Python do the comparison for you, by using the == operator. Check this out for how it works, or google for more. You can ask Python to only print the items if the comparison is failed, because, in this way, you read a lot less.

You find the first pair of different items, you know which layer it is, you find out why yours is different, and then you can fix it.

Lastly, if you decide to use, in your assignment notebook, my inspection code above or a modified version of it, please remember to remove it after your inspection work is finished, because leaving it there may interfere with the autograder and fail your submission.

Good luck!
Raymond

PS: we can’t share assignment work here, so I have to remove it for you.

1 Like

Thank you for your support. @rmwkwok

Have a nice learning @chandank

I ran into this exact issue and had to dig around for quite some time. My mistake was, instead of using the globally defined layers: reshaper, densor, LSTM_cell, I was defining those layers by myself directly from the keras.layers module. Although the parameters were exactly the same, the test case passed after I used the globally defined layers (why though? Not sure).

1 Like

Hello!

I’m reviving this topic because I just signed up for this class, and the course still lists this forum as the place to post answers. I hope I’m in the right place… :grin:

I’m having the same AssertionError mentioned by the Original Poster. I did follow the debugging advice given by @rmwkwok and found that my model has 49 extra TensorFlowOpLayers. This coincides exactly with the number of RepeatVector layers so I suppose I’m doing something incorrect the onehot/repeatvector. But do not see what the error could be…

This is my debug code:

print(len(inference_summary), len(music_inference_model_out))
your_layers = {}
prof_layers = {}
for l in inference_summary:
    if l[0] not in your_layers:
        your_layers[l[0]] = 0
    your_layers[l[0]] += 1

for l in music_inference_model_out:
    if l[0] not in prof_layers:
        prof_layers[l[0]] = 0
    prof_layers[l[0]] += 1
import pprint
pprint.pprint(your_layers)
pprint.pprint(prof_layers)

and this is the output:

201 152
{'Dense': 1,
 'InputLayer': 3,
 'LSTM': 1,
 'RepeatVector': 49,
 'TensorFlowOpLayer': 147}
{'Dense': 1,
 'InputLayer': 3,
 'LSTM': 1,
 'RepeatVector': 49,
 'TensorFlowOpLayer': 98}

I suppose I cannot post the code that creates and shapes the one-hot vector, but it is pretty straightfoward: argmax along the longest axis, one-hot on x[:,0] and then RepeatVector(1)(x) as suggested in 2.E.

I’m at the end of my wits here on how I ended up with this extra 49 layers… :thinking:

Any help will be greatly appreciated!