General Debugging

When faced with an error like this:

IndexError Traceback (most recent call last)
----> 1 parameters, last_name = model(data.split(“\n”), ix_to_char, char_to_ix, 22001, verbose = True)
3 assert last_name == ‘Trodonosaurus\n’, “Wrong expected output”
4 print(“\033[92mAll tests passed!”)

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 = optimize(X, Y, a_prev, parameters, learning_rate = 0.01)
65 ### END CODE HERE ###

in optimize(X, Y, a_prev, parameters, learning_rate)
33 # Forward propagate through time (≈1 line)
—> 34 loss, cache = rnn_forward(X, Y, a_prev, parameters)
36 # Backpropagate through time (≈1 line)

~/work/W1A2/ in rnn_forward(X, Y, a0, parameters, vocab_size)
94 x[t] = np.zeros((vocab_size,1))
95 if (X[t] != None):
—> 96 x[t][X[t]] = 1
98 # Run one step forward of the RNN

IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

If the most recent call is last, should I start from the beginning (oldest/first call) when looking for the bug that is causing the issues. Or should I start at the last most recent call… in this case ‘rnn_forward.’

I’ve mostly been using the last most recent parts of errors to debug thus far in the DLS courses. I’m wondering if theres any more information I can squeeze out of this error message.

Start from the bottom. The “IndexError:…” message refers to the line of code marked with line 96, where the arrow points.

The text just above that says that locates the error in the rnn_forward() function.


Yes, you can use bottom to top approach. Check the bottom most arrow, then the 2nd last arrow and so on. Moving from bottom to top and check where bug is…


1 Like

Ok thanks. So its telling me I’m incorrectly indexing and points to an index that I don’t do inside of a function that I only reference, didn’t write myself. So if I didn’t write the function, and I didn’t write how they’re using it. The only thing I can think of is it doesn’t like my initialization of x with np.zeros((vocab_size, 1)). But that wouldn’t explain an index problem would it?
also worth noting I think that whole rnn_forwards and x initialization happened in a different cell that I already passed the tests for.

Sorry, i didn’t review the error stack far enough, as I was thinking of a different assignment.

If you go further toward the top by one more error, you see that the error is thrown by your code in optimize(), where it calls rnn_forward().

Then going back one error further, you’re calling optimize() from model().

So, I’d say there’s a problem in your model() function where it computes one of the parameters that is passed to optimize() - that’s either X, Y, or a_prev.

1 Like

Thank you.

x is none + single example ix, none in square brackets
y is just the end of y appended to the new line. so since y = x+1 Im just doing x[1:] plus [ixnewline] which is just the \n.
the problem could be a_prev, I’m initializing it to zeros shape na,1 but then I dont touch it again before it enters the optimization loop and is used by optimize…