W4_A1_Ex-9_local variable 'dA_prev' referenced before assignment

Hi everyone, I am facing an issue with the exercise 9 of the 1st assignment of week 4 L_model_backward() function

Here is a screenshot of the error message,
Appreciate the support

Hi @Alex_Bleriot_KASHIE

Looks like you try to access a variable that wasn’t declare/assigned before.

make sure that the code cell related to that variable was executed and try again.

best regards

Hi Elirod, thanks for your response
Unfortunately I still don’t get it. Could you please be more explicit?
Don’t understand why is says dA_prev referenced before assignment.

Look at how the logic works in linear_activation_backward: there are two if statements that choose which set of logic to run based on what the activation function is, right? But the way you called the function, the value you passed doesn’t match either of the choices, which is why dA_prev does not get a value. So how could that happen?

This is how debugging works: you start with the error message. What does it mean? Notice that the exception is thrown in code that is actually correct, but the point is that a perfectly correct function can still throw errors if you pass it bad arguments. But you have to start at the point of the error and understand how that code works in order to work backwards and figure out where the real problem is.

1 Like

Hi Paul,
Really appreciate your time and precious advices.
Your explanations of the if statements made me understood I didn’t them the right way.
However I am still struggling to do it the right way.
Could you please elaborate better (with generic case if possible) how to call such functions.
I am a newbie in the field,
Really appreciate your understanding and support, thanks.

Hi @Alex_Bleriot_KASHIE ,

The linear_activation_backward() function deals with 2 cases of activation function, sigmoid, and relu. To determine which activation function to use, the code check the input argument activation against a string which is either “sigmoid”, or “relu”. Only when a match is found, the code under that if statement is executed, and the local variable dA_prev together with dW, db are created.
The error message reported here is dA_prev is referenced before assignment, so there could be two possibilities that dA_prev wasn’t created:

  1. none of the if statements is executed - due to mismatch of string when checking the input argument activation against the string “sigmoid” or “relu”
  2. linear_backward() failed to return any values - which is very unlikely

Please check the code for linear_activation_backward() to see if there is any mismatch of string.

1 Like

Did you actually read your code with the clues that Kic and I gave earlier in mind? You can see the bug in the exception trace that you showed in your original post on this thread. Look at what you passed as the “activation” argument: it is “sigmoig”. That is misspelled, so of course it doesn’t match either “sigmoid” or “relu”.

Every character matters in programming. “Close enough” is not “a thing”. It needs to be exactly correct. The difference between “,” and “;” can ruin everything. :nerd_face:

1 Like

Woow, I was really looking elsewhere… Thank you Mentor for this brilliant lesson, really appreciate it.
Would you by any chance have some room for another mentee?

Hi, Alex.

It’s great to hear that the info on this thread was helpful to you. We have a dedicated group of mentors who are here to help. Note that you got replies from at least 3 different people to your questions on this thread. Please feel free to continue asking whatever questions you have here on the forums and we’ll do our best to help.


1 Like