Course 1 week 4 exercise 9 ...tuple index out of range

IndexError

Traceback (most recent call last) in 1 t_AL, t_Y_assess, t_caches = L_model_backward_test_case()

----> 2grads = L_model_backward(t_AL, t_Y_assess, t_caches)

3

4 print("dA0 = "+ str(grads[‘dA0’]))

5 print("dA1 = "+ str(grads[‘dA1’]))

in L_model_backward(AL, Y, caches)

39# grads[“db” + str(L)] = …

40# YOUR CODE STARTS HERE

—> 41current_cache = caches[L]

42 dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads[“dAL”+ str(L )], current_cache,“sigmoid”)

43 grads[“dAL”+ str(L-1)]= dA_prev_temp

The IndexError and your definition of current_cache tell you what’s going on. An IndexError is most often raised when the index exceeds the number of objects in the “container” object. Here the object is the tuple caches. Higher up in the function, L is set with L = len(caches). Also, remember that python is “zero-indexed”. That is the first object in a tuple, list, dictionary, etc., is indexed by 0. To grab that one, I would use my_tuple[0], not my_tuple[1]. With this in mind, please reconsider your choice of index value in line 40.

KeyError Traceback (most recent call last)
in
1 t_AL, t_Y_assess, t_caches = L_model_backward_test_case()
----> 2 grads = L_model_backward(t_AL, t_Y_assess, t_caches)
3
4 print("dA0 = " + str(grads[‘dA0’]))
5 print("dA1 = " + str(grads[‘dA1’]))

in L_model_backward(AL, Y, caches)
40 # YOUR CODE STARTS HERE
41 current_cache = caches[0]
—> 42 dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads[“dA” + str(L+1 )], current_cache, “sigmoid”)
43 grads[“dA” + str(L-1)] = dA_prev_temp
44 grads[“dW” + str(L)] = dW_temp

KeyError: ‘dA3’

Getting this error now

KeyError Traceback (most recent call last)
in
1 t_AL, t_Y_assess, t_caches = L_model_backward_test_case()
----> 2 grads = L_model_backward(t_AL, t_Y_assess, t_caches)
3
4 print("dA0 = " + str(grads[‘dA0’]))
5 print("dA1 = " + str(grads[‘dA1’]))

in L_model_backward(AL, Y, caches)
40 # YOUR CODE STARTS HERE
41 current_cache = caches[0]
—> 42 dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads[“dA” + str(L)], current_cache, “sigmoid”)
43 grads[“dA” + str(L-1)] = dA_prev_temp
44 grads[“dW” + str(L)] = dW_temp

Found discussions on this topic but unable to figure out the solution

@kenb also name grads isn’t defined is shown when printing the keys

Well, where is grads defined? It’s a python dictionary, right? You initialize it by setting it to be an empty dictionary and then add elements to it.

I think you misunderstood Ken’s previous point about how zero-based indexing works in python. If I have a list called myList that has 3 elements in it, the last one is myList[2].

it is in the notebook above in the cell

Ok, have you looked at that logic? And has that cell actually been run since the last time you opened the notebook? Also you need to look at every following line that modifies grads. You need to figure out why grads is the wrong type. The first step would be to print the type right before the line that “throws”:

print(f"type(grads) = {type(grads}")

What does that show? Note that it may be correct the first time and the fail later.

This is how debugging works: you start by understanding the evidence at the point of failure. Then you need to work backwards to figure out where the actual error is. It is frequently nowhere near the point that the error is actually thrown.