Week 4 programming assessment #2

I am having this error when trying to run the two_layer_model function:
UnboundLocalError: local variable ‘dA_prev’ referenced before assignment

It keeps giving me the error that dA_prev is referenced before assignment although it is a variable declared inside the function linear_activation_backward and I cannot change it or access it. I have attached an image of the error and the part of the code that keeps giving me that error.
code snippet

1 Like

HI, the activation function on that line is misspelled, that causes an error in the linear_activation_backward function.

@Mubsi, maybe it could be a good idea to add a check on the linear_activation_backward function to catch this type of problems i.e. adding an else statement and provide an error if someone uses an “unexpected” activation function as input.


The problem is that you misspelled “sigmoid” on the parameter list. As a result the activation parameter does not match either of the possible choices and you “fall through” without doing anything.

It is a general principle of debugging that a perfectly correct subroutine can still thrown an error if you pass it bad parameters. But the first step is you have to examine the failing code to understand how it works and then work your way backwards to find the actual error. Note that you should know how linear_activation_backward works, right? We built that function in the previous exercise. You can look at the source here by clicking “File → Open”.


Thank you for the suggestion @albertovilla. As always make this an issue so we don’t forget about it. Thanks.