Problem in course5 week1 Assignment2

I got exactly the same problem and output with yours! It is really confusing…

Hi there,

I got an error for exercise 4:

in model(data_x, ix_to_char, char_to_ix, num_iterations, n_a, dino_names, vocab_size, verbose)
80 if j % 2000 == 0:
—> 82 print(‘Iteration: %d, Loss: %f’ % (j,loss) + ‘\n’)
84 # The number of dinosaur names to print

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

anyone able to help?


Hi @Dalkhat ,

restart kernel and clear output, rerun all cells

Hi Ccharlenee86,

Restart kernel and clear output, rerun all cells.

Hi, Kic, thank you! Cheers

Thanks @Kic, error persists though…

Hi @Ccharlenee86,

Check the variables are aligned to the same order of the values returned by the optimize() function.
loss should be a scalar, but the TypeError message suggested otherwise. So your loss variable might be placed in the wrong order.

Hi Kic,


optimise code ends with: return loss, gradients, a[len(X)-1]

The function was indicated as:
curr_loss, gradients, a_prev = optimize(X, Y, a_prev, parameters, learning_rate = 0.01)

so i think theres no issue.

but when i print my loss function, its a matrix with repeated results…

Hi @Ccharlenee86 ,

Did your optimize() function pass the unit test?

If not, then, you need to check why it is returning a matrix.

The loss is obtained by calling the rnn_forward() which is a function provided for you. So there should not be any problem with the rnn_forward(). However, do check you have the right variables to catch the returned values, and you are passing the correct parameters to rnn_forward().

Hi @Kic,
I am also getting the same error. I restarted and cleared the output multiple times still am getting the same output.
Help me out with this.


Did you rerun the code cells after refreshing the kernel and clearing the output?

@Kic ,
Thanks for replying.
Yes, I did rerun the cells after refreshing the kernel and clearing the output. I am still facing the same issue.


post that section of the code to me on DM, I’ll have a look for you.

Hi Kin,
Thanks for your time. I have messaged you my query.

Pls mentor I have the same problem thanks sir

Your code for “single_example”, “single_example_chars”, and “single_example_ix” is incorrect.
I recommend you read the instructions again for “Set the input X”.


I have a problem with the last validation and I have no idea how to solve it, all the tests come out correct except the last one, these are the outputs to see if there is someone who can help me:

I already have
rerun the code cells after refreshing the kernel and clearing the output

Here are the output of the model function

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


Iteration: 20000, Loss: 22.965093

Trodolosaurus —> The most similar result is this, although it is not the same

Iteration: 22000, Loss: 22.741148


Hey @Kic, I’m getting the same error as SPRODEM. I restarted the kernel after clearing outputs, and reran all the cells, and am getting the same output.

Hi @Jozdien ,

Please check how the variable ‘idx’ is calculated. That was SPRODEM’s problem. Also, refer to the implementation notes for guidance if you are not sure.

Hi @Emmanuel ,

The assertion is triggered because your code is incorrect. Please check the code enclosed with the ‘START CODE HERE’ AND ‘END CODE HERE’ comments.

It is important idx is calculated correctly.