Course 5: Week 1: Dinosaur Exercise 4 list index out of range in rnn_forward

Hi,

Trying to implement the model() function for exercise 4, but I’m receiving an index error: list index out of range in the rnn_forward function when I run my cells for exercise 4.

Here’s the output with my code censored with ‘<myCode>’.

j =  0 idx =  0
single_example = turiasaurus
single_example_chars ['t', 'u', 'r', 'i', 'a', 's', 'a', 'u', 'r', 'u', 's']
single_example_ix [20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19]
 X =  [None, 20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19] 
 Y =        [20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19, 0] 

Iteration: 0, Loss: 23.087336

Nkzxwtdmfqoeyhsqwasjkjvu
Kneb
Kzxwtdmfqoeyhsqwasjkjvu
Neb
Zxwtdmfqoeyhsqwasjkjvu
Eb
Xwtdmfqoeyhsqwasjkjvu


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-12-725c093d6b91> in <module>
----> 1 parameters, last_name = model(data.split("\n"), ix_to_char, char_to_ix, 22001, verbose = True)
      2 
      3 assert last_name == 'Trodonosaurus\n', "Wrong expected output"
      4 print("\033[92mAll tests passed!")

<ipython-input-11-1dca6d0b0775> in model(data_x, ix_to_char, char_to_ix, num_iterations, n_a, dino_names, vocab_size, verbose)
     61         # Perform one optimization step: Forward-prop -> Backward-prop -> Clip -> Update parameters
     62         # Choose a learning rate of 0.01
---> 63         curr_loss, gradients, a_prev = <myCode>
     64 
     65         ### END CODE HERE ###

<ipython-input-9-fca0aba78113> in optimize(X, Y, a_prev, parameters, learning_rate)
     32 
     33     # Forward propagate through time (≈1 line)
---> 34     loss, cache = <myCode>
     35 
     36     # Backpropagate through time (≈1 line)

~/work/W1A2/utils.py in rnn_forward(X, Y, a0, parameters, vocab_size)
    100 
    101         # Update the loss by substracting the cross-entropy term of this time-step from it.
--> 102         loss -= np.log(y_hat[t][Y[t],0])
    103 
    104     cache = (y_hat, a, x)

IndexError: list index out of range

I re-ran all the cells before this one and all the tests passed, so doubt there’s anything wrong with any of the prior functions.

I re-read all the steps and still can’t figure out why I’m receiving this index-out-of-range error. I assume Y[t] is the problem?

Any help would be much appreciated, thanks.

Hi @aPerson ,

The problem arises in rnn_forward(), an utility function that is provided for you.
Try refresh the kernel and rerun the code from start to see if it helps.

Typically that’s an invalid assumption. Passing the unit tests does not prove that your code is perfect.

Nevermind, I figured it out. This error occurred on the second iteration and turns out I was setting Y incorrectly. Thanks anyway.

Hi @aPerson ,

Great to hear you have got it sorted. Thanks.