Error in week 1 assignment 2 last exercise

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] 

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-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-15-ad9b5cfee8e5> in model(data_x, ix_to_char, char_to_ix, num_iterations, n_a, dino_names, vocab_size, verbose)
     82         if j % 2000 == 0:
     83 
---> 84             print('Iteration: %d, Loss: %f' % (j, loss) + '\n')
     85 
     86             # The number of dinosaur names to print

TypeError: only size-1 arrays can be converted to Python scalars

Sir this is the error I am facing

Hi @Pulkit ,

Here is my output:
j = 0 idx = 0
single_example = turiasaurus
single_example_chars [‘t’, ‘u’, ‘r’, ‘i’, ‘a’, ‘s’, ‘a’, ‘u’, ‘r’, ‘u’, ‘s’]
single_example_ix [None, 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]

as you can see my X and Y are different from yours, just a list. But your X and Y are made into list of list, and that is where the problem lie.

I need help. I am stuck in the last exercise for an hour now. Don’t know what is wrong. Previous step ‘optimize’ passed the grader test. Following is the 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)
60 # Perform one optimization step: Forward-prop → Backward-prop → Clip → Update parameters
61 # Choose a learning rate of 0.01
—> 62 curr_loss, gradients, a_prev = optimize(X, Y, a_prev, parameters, learning_rate=0.01)
63
64 ### END CODE HERE ###

in optimize(X, Y, a_prev, parameters, learning_rate)
31 ### START CODE HERE ###
32 # Forward propagate through time (≈1 line)
—> 33 loss, cache = rnn_forward(X, Y, a_prev, parameters)
34
35 # 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: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

++++++++++++++++++++++++++++++++++++++++++++++++++++++

Finally solved the issue sir thanks for your support

Hi @Pulkit ,

Great to hear you have got it sorted. Thanks

Thank you. I solved the problem. I restarted and cleared the output. Then ran again and it was apparent that the X and Y were not right. Fixed it and it ran successfully.

Hello,

I have the same output as yours but still getting the “Wrong expected output” error message.

And here is my final output:


Hi @farmakis

Two lines of code are incorrect:

  1. calculation of idx should be done by len(examples) because idx is used to get one examples from the list of examples. Please see the implementation notes.
  2. single_example_ix is wrong, it should be getting the ix from char_to_ix if the char is in single_example_chars
1 Like
TypeError                                 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-b96cdaae36a4> 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)
     64 
     65         ### END CODE HERE ###

<ipython-input-9-bdbaca42588b> 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)
    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)

TypeError: 'NoneType' object is not subscriptable

type or paste code here

I keep getting this error. Everything upto optimize is running

Most likely, in your model() function, there is an error in your X, Y, or a_prev values.

a_prev = np.zeros((n_a, 1))
X = [None]+[single_example_ix]
Y = single_example_ix.append(ix_newline)
these are my values. is something wrong?

Hi @Bhavika_Rakesh ,

To help debugging, move the block of code that is already provided for you to print out the content of X and Y. That block of debugging code is just below the call for optimize() in the model() function, move it before the call for optimize().

I did that, X,Y are : [None, 20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19] [20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19, 0]

Iteration: 22000, Loss: 14.642888

Omaqosaurus
Omala
Otarailosaurus
Omaldor
Tods
Ogantor
Ssagonelesirosaurus


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-20-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!")

AssertionError: Wrong expected output

The code is working but I am getting the wrong values, is the initialization of a_prev to np.zeros correct?

Hi @Bhavika_Rakesh ,

It appears that the problem is in the model() function where the idx is calculated. This idx is index to the samples, so it should be calculated by using the number of samples. However, you have hardcoded it to 27. Hence, the single example that passed to optimize() is different form what it should be.

Your Y values are not correct.
They should not be identical to the X values.

Yup. sorted. Thank you

I think I got the code right, but I am not getting the expected result. Previous tests passed. Could it be something wrong in previous functions?

        ### START CODE HERE ###
        
{mentor edit: code removed}
        
        ### END CODE HERE ###

Iteration: 22000, Loss: 20.408705

Lutuson
Guia
Hyutolonnopus
Locaesten
Yussaodon
Elacosaurus
Wurephosaurus


AssertionError Traceback (most recent call last)
Cell In[242], line 3
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!”)

AssertionError: Wrong expected output

Welcome to the community.

First, in the future, please do not share your code on the forum. That’s not allowed by the Code of Conduct. Using a screen capture image, just post your test results, or any error messages you see. If a mentor needs to see your code, we’ll contact you with instructions.

Tip: Your 2nd line of code uses “data_x”. That should be “examples”.

Sure, I will not share my code again. Apologies, I did not know. Thanks for helping!

1 Like