Programming assignment 4 struggling with current_cache in L_model_backward function

This is the link for the assignment: Coursera | Online Courses & Credentials From Top Educators. Join for Free | Coursera

In the function “L_model_backward” there is an object “current_cache” we need to define. I did it like this:
current_cache = caches

but it leads to the error:
TypeError: bad operand type for unary -: ‘tuple’

in the called function “sigmoid_backward(dA, cache)” defined above on the line:
s = 1/(1+np.exp(-Z))

Not really sure what the current_cache requires. I also tried “current_cache.append(caches)” but that doesn’t work because current_cache is undefined. I could use a little more guidance on what current_cache is supposed to be.

The point is that caches is a list of cache entries: one for each layer. It was created during forward propagation. You can go back and look at that code to remind yourself what it looks like.

Now in back propagation, you are processing one layer at a time and you need to select (index) the appropriate entry from the caches list for the layer you are currently handling. So how do you index a list in python? The same way you index an array, right? The first element has index 0, the second has index 1 and so forth.

@paulinpaloalto is right! The list of caches created during forward propagation contains the necessary information for each layer. During backpropagation, you need to index into this list to get the relevant cache for the current layer. The error you’re encountering (TypeError: bad operand type for unary -: ‘tuple’) suggests that you passed a tuple where the function expects a single numeric value (like Z). Ensure that current_cache is not the entire list of caches but only the cache for the specific layer.

1 Like

Thanks guys,

I will try that and see how it goes. I had already started experimenting with that, as I suspected that was the issue.

That worked.

2 Likes