C5w1a2 unq_c4

I am obtaining a list index out of range error when calling the optimization function optimize(X, Y, a_prev, parameters, learning_rate = 0.01) from the model() function. The error seems to be in the rnn_forward call when calculating the loss -= np.log(y_hat[t][Y[t],0]).

I am unsure how passing the optimize function parameters is giving a list index out of range error?

thank you

If your rnn_forward and rnn_cell_forward functions pass their test cases, then the thing to keep in mind is that correct functions can still throw errors if you pass them incorrect arguments. In order to debug this, you start from the point of the error. What does it mean? It sounds like the arguments at that point are the wrong shape. So where did they come from? You need to work backwards to figure out where the real error is.

I cannot reach the embedded code for some of the functions in order to debug my code. Specifically the rnn_forward function which is provided.


Sure you can: you can see from the exception trace that the function rnn_forward is in the file utils.py. So just click “File → Open” and then open that file if you want to see the code. Hint: it looks a lot like what you wrote in the previous assignment “Building your RNN Step by Step”.

Note that just because the error is thrown in code you didn’t write does not mean it’s not your problem: a perfectly correct function can throw errors if you pass it incorrect arguments. Since the call stack here starts from the model function one assumes that your optimize function already passed its test cases, so the error is most likely in the model logic.

I added some print statements to my model code to see the type and shape of X and Y:

initial loss 23.070858062030304
type(Y) = <class 'list'>
 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] 

len(X) 12, len(Y) 12
a_prev.shape (50, 1)
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

Thank you Paul in PA! I had a slice syntax that was not quite accurate. Working now!

2 Likes