W4,Exercise 5 - L_model_forward reopened

My problems are related to topic taken in a closed discussion W4,Exercise 5 - L_model_forward, but I am having another error:

in linear_activation_forward(A_prev, W, b, activation)
33 A, activation_cache = relu(Z)
—> 35 cache = (linear_cache, activation_cache)
37 return A, cache
UnboundLocalError: local variable ‘linear_cache’ referenced before assignment

My L_model_forward() implements [LINEAR → RELU]*(L-1) passing to linear_activation_forward parameters W & b indexed by l, then implements LINEAR->SIGMOID passing the same but indexed by L. Test output from all previous funcs are correct.
I have no idea what’s happening.

Hi @Andrzej_Krynski ,

linear_cache is a local variable, It holds the output returned from calling the function linear_forward(), but where is it being declared?

It is being declared inside linear_activation_forward() when counting Z, so one line earlier as shown in the error snapshot, exactly twice: one for relu and ones for sigmoid. It seems to be ok…

Hi @Andrzej_Krynski ,

If linear_cache has been declared, then, it is possible the execution environment is out of sync. So just refresh the kernel and rerun the code from start.
form the menu at the top, select Kernel → restart and clear all output
Cell → run from start

still the same… It does not helped any bit.

can it be caused by inproper params to linear_activation_forward() func?

Hi @Andrzej_Krynski ,

Send me a direct message with that function, I will have a look for you so see what might be the problem.

1 Like

Look carefully at the logic in linear_activation_forward: how can you get to the line that “throws” without setting linear_cache? If you didn’t take either of the branches because the value you passed for the activation parameter does not match either of the possible choices. Note that it is comparing against string names, not object references.

Yes, I think that’s the right theory to pursue. A correct function can still throw errors if you pass it incorrect parameters.

But there are not so many options to put incorrect values. parameters[“W” + str(l)], … l+1, l-1 eventually. And no value of it changes the error.
Beside, I am sure, it shouul be l used for relu and L for sigma. Or am I wrong?
I am currently at work :shushing_face: and have no possibility to review the code now, but tommorrow (in ~12 hours) I’ll try to looknat it again.

Read carefully what I said in my previous reply. In python "relu" and relu are not the same thing: the former is a string and the latter is an “object reference”.

1 Like

Indeed, I didn’t thought that. Another silly fault. Thank You @paulinpaloalto