Questions about Week 1 assignment 2

I am struggling in Exercise 4 - model in Week 1 assignment 2. Does anyone know what’s wrong with my code? The error shows TypeError: ‘NoneType’ object is not subscriptable

{mentor edit: code removed}

1 Like

In “idx = …” , don’t use j + 1. Just use “j % len(examples)”

Not working. Should be something else. I have passed Q1-Q3, and the issues should happen in Q4. Do you have any ideas?

The following are the error message:

TypeError 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)
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 ###

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

That doesn’t look right. It’s supposed to multiply y-hat and y, but seems to be missing a multiplication operator.

Update: Well, that’s not the problem, because that’s from one of the provided utility functions (rnn_forward). I don’t get how it works though.

This one line code is in the rnn_forward function which is provided by the assignment. Do you mean there is something wrong with the prebuilt function?

But that’s the line of code that is causing the error. It’s in rnn_forward(), which is called from your optimize() function.

So maybe in optimize(), you’re passing an incorrect argument to rnn_forward().

Could you check the optimize for me, please? I cannot identify the error since I have passed the test for optimize.

{mentor edit: code removed}

return loss, gradients, a[len(X)-1]

{mentor edit - text removed}

I don’t see any problems there.

Back in the model() function, maybe it doesn’t like your code for setting the Y value.
Try this instead:
Y = X[1:]
Y.append(ix_newline)

They should be the same, but maybe they aren’t.

1 Like

It works. Thanks so much.