W1 A3, Problem with Jazz LSTM, predict_and_sample

This is my error.

IndexError Traceback (most recent call last)
----> 1 results, indices = predict_and_sample(inference_model, x_initializer, a_initializer, c_initializer)
3 print(“np.argmax(results[12]) =”, np.argmax(results[12]))
4 print(“np.argmax(results[17]) =”, np.argmax(results[17]))
5 print(“list(indices[12:18]) =”, list(indices[12:18]))

in predict_and_sample(inference_model, x_initializer, a_initializer, c_initializer)
21 indices = np.argmax(pred, axis=-1)
22 # Step 3: Convert indices to one-hot vectors, the shape of the results should be (1, )
—> 23 results = to_categorical(indices, num_classes=78)
24 ### END CODE HERE ###

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/utils/np_utils.py in to_categorical(y, num_classes, dtype)
76 n = y.shape[0]
77 categorical = np.zeros((n, num_classes), dtype=dtype)
—> 78 categorical[np.arange(n), y] = 1
79 output_shape = input_shape + (num_classes,)
80 categorical = np.reshape(categorical, output_shape)
IndexError: index 82 is out of bounds for axis 1 with size 78

I suspect it to be either in step 2 or 3, but can exactly diagnose the issue, and I’m confused why it’s arbitrarily bringing up the number 82. I also believe my music_inference_model should be correct.

Thanks for the help in advance!


I just read one of the discourse messages from one of the mentors.
(step 3 of predict and sample)
results = … num_classes=n_values)

I originally had it set to
results = … num_classes=78)

I just wanted to ask why in the instructions, it specifies not to use num_classes=n_values when the grader only deems it correct once you have it as results = … num_classes=n_values)?

Edit2: Source Code Removed

Hard-coding the dimensions is usually a bad idea. The test cases used by the grader are totally different than the tests in the notebook.

1 Like
  • Note that using a global variable such as n_values will not work for grading purposes.

It says this somewhere in the instructions. I interpreted this as “don’t use n_values”, but I was of course wrong in some sense. Can you explain what the “correct” interpretation is?

Thanks for the reply to my previous message!

Inside the predict_and_sample() function, there is a line of code that sets a local variable called n_values.

n_values = x_initializer.shape[2]

That’s the variable you should use in the to_categorical() function.

The note in the instructions is unfortunately confusing. I’ve submitted a bug report to make this more clear.