Week 4, Exercise 5: Model_forward

The linear_activation_forward still works fine (at least, it passes the test).
Now, when I use it in my code for Exercise 5, I get the following error message:

#############################################################

UnboundLocalError Traceback (most recent call last)
in
1 t_X, t_parameters = L_model_forward_test_case_2hidden()
----> 2 t_AL, t_caches = L_model_forward(t_X, t_parameters)
3
4 print("AL = " + str(t_AL))
5

in L_model_forward(X, parameters)
30 #W=parameters[“W”+str(l)]
31 #b=parameters[“b”+str(l)]
—> 32 A,cache=linear_activation_forward(A_prev,parameters[“W”+str(l)],parameters[“b”+str(l)],relu)
33 caches.append(cache)
34 # YOUR CODE ENDS HERE

in linear_activation_forward(A_prev, W, b, activation)
40 A,activation_cache=relu(Z)
41 # YOUR CODE ENDS HERE
—> 42 cache = (linear_cache, activation_cache)
43 #print("A_neu: ",A)
44 return A, cache

UnboundLocalError: local variable ‘linear_cache’ referenced before assignment
################################################################

I tried to fix this by inserting the following into the code for linear_activation_forward:

linear_cache=
activation_cache=
A=A_prev

With these ingredients, the code runs, but now, the loop in model_forward with the lines

Moderator edit: code removed.

does no longer change A. (I had it print A, and it just remains the same in every iteration.)

I suppose the first error should not have happened in the first place, and my attempt to fix it does not work as it should. Any help would be appreciated…

1 Like

Hi, @Merlin_Carl. At some point in your coding life you are going to produce this error. Congratulation on getting to Week 4 before getting your first. The UnboundLocalError: local variable referenced before assignment is raised when you try to assign a value to a local variable (i.e. a variable within a function) prior to it being declared. I get these most often when I am working on a function that contains some complicated if-elif-else logic. When a try to return the value from the function, I find that my logic had not exhausted all of the pertinent cases.

I am ignoring your attempted fix in the linear_activation_forward function. If it passed its unit tests, I would hold the line on that, for now at least. Well, mostly ignoring. That last line, A = A_prev is concerning. If you tried that in the for loop of L_model_forward (i.e. had changed the provided code), that would be the source of such an exception.

Hi there, me again. Note that the activation argument in linear_activation_forward calls for a string. Please read the “docstring” (the function doumentation in triple quotes) to linear_activation_forward function carefully.

2 Likes

Der kenb,
the second post solved my problem, I just had to write >>activation=“relu”<< as the final argument of linear_activation_forward and everything worked fine. (What I do not understand, though, is why simply writing “relu” in quotation marks did not work either, but I guess I’ll find out after some reading.)
Thank you very much for your help!

1 Like