Week 1 assignment 2 exercise 4 model

Not sure what I am doing wrong here, can someone point me in the right direction?

Getting the following error:

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, 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/utils.py 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

What we want to do in here is;

  1. get a “single_example” like turiasaurus
  2. break into a list of single character like [‘t’,‘u’,‘r’,‘i’,…]
  3. convert a list of single characters into a list of integer like [20, 21,18, 9,…]
  4. add “None” at the beginning of the list for the use in "rnn_forward(). The result should be like [None, 20, 21, 18, 9, …]

So, you did not use the right variable to set the range at the step 3. It needs to be a list of single characters, not a word.
In addition, X is a list of integers with None in front. Looks like you are adding a list of characters.

Correcting above two should let you going forward. Hope this helps.

By the way, pasting your code in here is not recommended in this community. Please remove those.


Thanks Nobu_Asai, I created X wrong as you’ve pointed out. I’ve removed the code.