Week 4 Exercise 5

I have the following error message, any suggestion as how to fix it?

AL = [[0.03921668 0.58927854 0.00277513 0.04728177]]
Error: Wrong output for variable 0.
Error: Wrong output for variable 0.
Error: Wrong output for variable 1.
2 Tests passed
1 Tests failed

The expected AL is supposed to be:

AL = [[0.03921668 0.70498921 0.19734387 0.04728177]]

I found out the reason, given

J = caches[len(caches)-1][1]

The following won’t work

AL, cache = linear_activation_forward(J, parameters['W' + str(L)], parameters['b' + str(L)], activation = "sigmoid")

But this one works:

AL, cache = linear_activation_forward(A, parameters['W' + str(L)], parameters['b' + str(L)], activation = "sigmoid")

However, the following is true:

assert(A.any() == J.any())

Any one could explain?

Why are you using caches as input in forward propagation? The point of the caches is that they are used to pass values to backward propagation, right? In forward prop, you’ve already got what you need ready to hand.

But here is the structure of one cache entry:

((A, W, b), Z)

The caches variable is a python list of entries like that. Each one is a 2-tuple, the first element of which is a 3-tuple. So if you take index [1] of that you get Z, right? If you wanted the A value from the first element, that would be caches[-1][0][0]. But as we discussed above, you should already have the relevant A value from the previous layer sitting in a local variable anyway, so you’re just making it way more complicated to dig it out of the caches variable.

Also note that the normal notational convention here is to use J for the scalar cost value (the average of the loss over the samples in the current batch).

I think you are also probably misinterpreting what the numpy any() method does. What A.any() evaluates to is True (scalar) if any element of A is non-zero. So you are asserting that either both A and J each contain at least one non-zero element or both of them contain only False (0) values. As I hope you would agree, that doesn’t really tell you anything useful in this instance.