C5W1Assignment2(Dinosaurus Island Character level language model)[Exercise4:IndexError]

Hi!
In exercise4 where we are expected to implement the model by calculating and feeding the values of X,Y,a_prev,parameters and learning rate in the optimize function, I got this error:

---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
in
----> 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!")**

in model(data_x, ix_to_char, char_to_ix, num_iterations, n_a, dino_names, vocab_size, verbose)
** 64 # Perform one optimization step: Forward-prop → Backward-prop → Clip → Update parameters**
** 65 # Choose a learning rate of 0.01**
—> 66 curr_loss, gradients, a_prev = optimize(X,Y,a_prev,parameters,0.01)
** 67 **
** 68 ### END CODE HERE ###**

in optimize(X, Y, a_prev, parameters, learning_rate)
** 32 **
** 33 # Forward propagate through time (≈1 line)**
—> 34 loss, cache = rnn_forward(X, Y, a_prev, parameters)
** 35 **
** 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
** 97 **
** 98 # Run one step forward of the RNN**

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

I did not understood what to do so I just printed out the values of X and Y, as that’s the only calculation that I was supposed to perform and I got these values when I printed them out.

X : [None, ‘t’, ‘u’, ‘r’, ‘i’, ‘a’, ‘s’, ‘a’, ‘u’, ‘r’, ‘u’, ‘s’]
Y : [21, 18, 9, 1, 19, 1, 21, 18, 21, 19, 0]

Now, I am not able to figure out where is the problem in my code as I am able to perform the required calculation and the error is being thrown from a predefined function, so can somebody please help me with this.
Thanks in advance

Your Y vector is one value short - it’s missing the first index value (for the letter ‘t’).

Hii @TMosh,

Thank you for your kind reply. I was thinking that we are not supposed to include that value but anyways, I included that value but still getting the same error.

Please post your X and Y values again, and indicate exactly where you’re printing them from.

Here are the values

X : [None, ‘t’, ‘u’, ‘r’, ‘i’, ‘a’, ‘s’, ‘a’, ‘u’, ‘r’, ‘u’, ‘s’]
Y : [21, 18, 9, 1, 19, 1, 21, 18, 21, 19, 20, 0]

And this is what I’m actually doing:

At every iteration:

  1. Taking on example
  2. Converting it into list of characters
  3. Converting list of characters into list of integers (using char_to_ix)
  4. X = [None] + list of characters
  5. Y = list of integers
  6. Y.append[index_of_newline]
  7. print(X)
  8. print(Y)

I think you are misinterpreting the instructions. Both X and Y are lists of indices, not characters. Here is a sample of the output printed by my code:

j =  0 idx =  0
single_example = aachenosaurus
single_example_chars ['a', 'a', 'c', 'h', 'e', 'n', 'o', 's', 'a', 'u', 'r', 'u', 's']
single_example_ix [1, 1, 3, 8, 5, 14, 15, 19, 1, 21, 18, 21, 19]
 X =  [None, 1, 1, 3, 8, 5, 14, 15, 19, 1, 21, 18, 21, 19] 
 Y =        [1, 1, 3, 8, 5, 14, 15, 19, 1, 21, 18, 21, 19, 0]
1 Like

I have trouble with # UNQ_C4
In Dinosaurus_Island_Character_level_language_model, char_to_ix is all lower case. But in
assert last_name == ‘Trodonosaurus\n’, “Wrong expected output”

  1. last_name is expecting “T”. how is it possible? char_to_ix is all lower case vocabulary

  2. Below is my first example, but I am getting different last_name
    Training size: 1536
    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

  1. I am getting different output name and my loss is 0.07 higher

Iteration: 22000, Loss: 22.794515

Onyvus
Llecakosaurus
Mustodonosaurus
Ola
Yusodon
Eiadosaurus
Trodontorus

Please advise

For the question about capitalization, notice that all the 7 printed names that are shown every 2000 iterations have their first letter capitalized. If you examine the logic, those strings are the return value from the provided utility function get_sample. You can examine the source for that by clicking “File → Open” and then opening the file utils.py. If you do that, you’ll understand why the first letters are capitalized. :nerd_face:

On the question of why your results are different, here’s my output for the end of the training:

Iteration: 22000, Loss: 22.728886

Onustreofkelus
Llecagosaurus
Mystolojmiaterltasaurus
Ola
Yuskeolongus
Eiacosaurus
Trodonosaurus


last_name Trodonosaurus

All tests passed!

Notice that the beginning few characters of your names are similar, but then they diverge. My guess is that the place to look for that issue is in your sample() function. Did your function pass the test cases? If that was successful, the other thing to check is to make sure you used the “scrambled” version of the original input list that they created for you in the template code here, rather than directly using the alphabetized list as the input.

That worked for me.

I literally misinterpreted the instructions.
@TMosh @paulinpaloalto Thanks for the support. :blush: :blush:

When you say use the scrambled version. Do you mean when it does “shuffle examples” because I use the examples from then on assuming that it is from the shuffled examples. My sample function also passed.

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]]


IndexError Traceback (most recent call last)
in
----> 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!”)

in model(data_x, ix_to_char, char_to_ix, num_iterations, n_a, dino_names, vocab_size, verbose)
64 # Perform one optimization step: Forward-prop → Backward-prop → Clip → Update parameters
65 # Choose a learning rate of 0.01
—> 66 curr_loss, gradients, a_prev = optimize(X, Y, a_prev, parameters, learning_rate = 0.01)
67
68 ### END CODE HERE ###

in optimize(X, Y, a_prev, parameters, learning_rate)
32
33 # Forward propagate through time (≈1 line)
—> 34 loss, cache = rnn_forward(X, Y, a_prev, parameters)
35
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
97
98 # Run one step forward of the RNN

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

Its also interesting to note that my [0] at the end of Y is in square brackets all itself…a clue?

Thanks

Yes, that is most likely what is triggering the error. You need to figure out why that is happening.

I was able to get the ‘0’ out of brackets by removing the brackets from around ix_new_char[‘n’]. pretty obvious since that was the only line seperating definition of ‘X’ and ‘Y’. However I still have my same error:

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]


IndexError Traceback (most recent call last)
in
----> 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!”)

in model(data_x, ix_to_char, char_to_ix, num_iterations, n_a, dino_names, vocab_size, verbose)
64 # Perform one optimization step: Forward-prop → Backward-prop → Clip → Update parameters
65 # Choose a learning rate of 0.01
—> 66 curr_loss, gradients, a_prev = optimize(X, Y, a_prev, parameters, learning_rate = 0.01)
67
68 ### END CODE HERE ###

in optimize(X, Y, a_prev, parameters, learning_rate)
32
33 # Forward propagate through time (≈1 line)
—> 34 loss, cache = rnn_forward(X, Y, a_prev, parameters)
35
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
97
98 # Run one step forward of the RNN

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

When it says what you’re allowed to run as an index: integers, slice, etc, it has a couple lines of code listed there like x[t] = np.zeros((something, 1)) and it also has X[t]X[t] so in those cases it looks like we’re using np.zeros as an index or x[t] itself as an index, which its saying is no good. except none of that code is my own. Its all from the utils package from RNN_forward that was given.

A perfectly correct function can still throw errors if you pass it bad arguments. Here’s what I get when I run the training:

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

So my X and Y values look the same as yours. Maybe your problem happens on iteration 1, not 0?

How can I determine what iteration I am getting my error on based on the above posted error message? Is there a certain print statement I can do to figure that out?

Print the iteration number at the beginning of the loop. It will be annoying and you will of course delete the print statement as soon as you figure this out, but that is how debugging works: one step at a time.

1 Like

It appears I’m not making it through one loop.
iteration J
0
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]

with a few print statements it would seem my program is stopping right at the optimize function. Which I’m applying the arguments x, y, aprev, parameters, and learning rate. This function previously passed the tests in the previous cell.

Oh, wait, your X value is not the same as mine. Sorry, I didn’t look closely enough. Note that the first element of yours is a python string, but in mine it is the token None. Those are not the same and with that realization, we can now see why that logic ‘throws’.

1 Like

Ah, yes, I now understand your comment “it will be annoying but you can delete the print statement”

Because therer are 22000 iterations haha.

Thank you Paul. I took the example for prepending none… [‘A’] + [‘B’] a little too literally.