Issue with predict_and_sample in Jazz with LSTM Assignment (Week 1)

I am unable to understand what is wrong with my implementation of the predict_and_sample function. All functions upto that one pass all the tests.

However, when the following code given in the notebook is executed, I am getting results that are different from the expected ones.

results, indices = predict_and_sample(inference_model, x_initializer, a_initializer, c_initializer)

print("np.argmax(results[12]) =", np.argmax(results[12]))
print("np.argmax(results[17]) =", np.argmax(results[17]))
print("list(indices[12:18]) =", list(indices[12:18]))
np.argmax(results[12]) = 0
np.argmax(results[17]) = 0
list(indices[12:18]) = [array([79]), array([86]), array([23]), array([9]), array([75]), array([84])]

I am unable to figure out where the error is. Please advise. Thank you!

Note that they do say this in the instructions for that function:

  • Your results may likely differ because Keras’ results are not completely predictable.

Does your code pass the grader?

Oh, sorry, there must be something wrong, because results and indices are just two different ways to present the output and they should agree, meaning that if the indices[12] is array([79]), then argmax(results[12]) should be 79 and not 0.

Are you sure you followed the instructions for how to convert the index values to the results values?

No, it did not pass.

It seems like I have missed something. I don’t know what I have missed though.

The instructions for “Step 3” were pretty clear and point you to the to_categorical function.

yes, I looked up the doc available at tf.keras.utils.to_categorical  |  TensorFlow v2.12.0.
I have set y to indices and num_classes to x_initializer.shape[2].

That sounds correct. Are you saying that successfully fixed the problem or that your results are still incorrect?

1 Like

my results are still incorrect. I think I am not correctly converting pred, a list, to numpy array. Do I need to convert pred to an numpy array before passing it to np.argmax?

Yes, you do, but your indices values are already correct and that’s where you’d have to do that conversion. Or did you use pred instead of indices as the input to to_categorical? FWIW here’s my output from that test cell with a few added prints to show shapes and types

len(pred) = 50
type(pred) = <class 'list'>
indices.shape = (50, 1)
results.shape = (50, 90)
np.argmax(results[12]) = 21
np.argmax(results[17]) = 44
list(indices[12:18]) = [array([21]), array([5]), array([43]), array([25]), array([46]), array([44])]

Note that my results values are consistent with corresponding indices values.

I am passing indices as input to to_categorical. However, my results, as you can see from below, are different from yours:

pred len:  50
pred type:  <class 'list'>
indices shape:  (50, 1)
results shape:  (50, 90)
np.argmax(results[12]) = 79
np.argmax(results[17]) = 84
list(indices[12:18]) = [array([79]), array([86]), array([23]), array([9]), array([75]), array([84])]

Two questions:

  1. Can I pass the initializer variables that were passed as arguments directly to inference_model.predict() or do I need to do any processing?
  2. can I get the indices using the following code?

{moderator edit - solution code removed}

That all sounds correct: you don’t need any preprocessing on the initializer values and now your results and indices match. So I think everything is ok and it’s just the point that I made in my first reply: the results here are not reproducible. Try submitting to the grader and I’ll bet you get full marks.

Thank you. I got full marks. But, the results were not even close to the expected outputs.

That’s good news, so your code must be correct. So I guess we’re back to the statement they made about the results not being always the same. I notice that they did not set the random seeds anywhere in this assignment, which is something they normally do in order to get reproducible results for ease of testing and grading. I tried a few experiments and if I just rerun only the test cell for predict_and_sample multiple times, I get the same answer every time. But if I do “Kernel → Restart and Clear Output” and then run everything again, I get different answers. So it must be that the training of the model is starting from random initializations and we don’t end up with exactly the same weights every time. You could try some experiments by adding code to set the random seed before the training and see if that has any effect on the reproducibility of the output.


What results did you get?

The same one I posted above:

np.argmax(results[12]) = 79
np.argmax(results[17]) = 84
list(indices[12:18]) = [array([79]), array([86]), array([23]), array([9]), array([75]), array([84])]

Thanks. I wasn’t sure which output you were discussing.

My values for np.argmax(results[12]) and [17] were 1 and 35.

thank you for your advice!