I am not sure why I am getting this error: tuple indices must be integers or slices, not str
Hello @Sherman_Sham,
I cannot tell which week this assignment belongs to from your post, but just from the error message, the operation of “indexing” is done by the square brackets, and cache
is the only variable that is being indexed if we look at the line that has an arrow.
cache
is usually a dict
but from the error message, it appears to be changed to a tuple
. You might want to start with looking at your code for when cache
is being assigned to a new value.
Cheers,
Raymond
Hi Raymond.
This is from Course 4 week 1, assignment 1 - Convolution_model_Step_by_Step_v1 - exercise 5 - conv_backward
The assignment asked that we retrieve information from cache
Retrieve information from “cache”
# (A_prev, W, b, hparameters) = None
cache was generated from the conv_forward function.
conv_forward(A_prev, W, b, parameters)
You’re working too hard here. You don’t need to specify each separate item.
If you look at where “cache” is defined in conv_forward(), it’s already a tuple, and it only contains those four values.
When you reference “cache” later, you don’t need to create a tuple of a tuple.
Your method is still too complicated. Try less code.
Thanks for sharing which assignment it is.
So it is indeed a tuple, and in this case, you cannot use square brackets to “identify” any element of that tuple.
cache['some_name']
is the way we use a dictionary. A dictionary is a collection of key-and-value pairs, meaning that each value has a corresponding key, and you can use the key to find the corresponding value. For cache['some_name']
, 'some_name'
is a key.
However, in tuple, there are no keys, but just values. Therefore, you cannot do cache['some_name']
.
You might want to google “python tuple unpacking” for what’s going on. It talks about how we are “unpacking” the values from a tuple to multiple variables.
Good luck!
Raymond