Unclear error message in Week 1, Assignment 2. Exercise 3

Dear course mentors,

As I am going through the exercise 3, I got an unclear KeyError notification as following:

KeyError Traceback (most recent call last)
in
28 print(“\033[92mAll tests passed!”)
29
—> 30 optimize_test(optimize)

in optimize_test(target)
9 Y = [4, 14, 11, 22, 25, 26]
10 old_parameters = copy.deepcopy(parameters)
—> 11 loss, gradients, a_last = target(X, Y, a_prev, parameters, learning_rate = 0.01)
12 print(“Loss =”, loss)
13 print(“gradients["dWaa"][1][2] =”, gradients[“dWaa”][1][2])

in optimize(X, Y, a_prev, parameters, learning_rate)
41
42 # Update parameters (≈1 line)
—> 43 parameters = update_parameters(gradients, parameters, lr = learning_rate)
44
45 ### END CODE HERE ###

~/work/W1A2/utils.py in update_parameters(parameters, gradients, lr)
71 def update_parameters(parameters, gradients, lr):
72
—> 73 parameters[‘Wax’] += -lr * gradients[‘dWax’]
74 parameters[‘Waa’] += -lr * gradients[‘dWaa’]
75 parameters[‘Wya’] += -lr * gradients[‘dWya’]

KeyError: ‘Wax’

I am not sure about what this indicates so turning for help in this forum, thanks for your support.

1 Like

Check the order of arguments to the update_parameters function.

Thanks for the correction !

i have got another unclear error, could you give me some hint, thanks?
This is from the same assignment with exercise 4:

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

There might be a couple of reasons for this. If you passed the previous test, optimize function, and I guess so, then double-check how you are implementing the code. Please read the instructions again:

# Set the index `idx` (see instructions above)
        idx = None
        
        # Set the input X (see instructions above)
        single_example = None
        single_example_chars = None
        single_example_ix = None
        X = None
        
        # Set the labels Y (see instructions above)
        ix_newline = None
        Y = None

Thanks for the hints.
After trying the codes several times, I found that it was the code for append ix_newline went wrong.
I changed list.append() method to + (list plus list) method and the error was solved.
Thanks again for the instructions.