Week 4 Assignment 1 Exercise 9 - L_model_backward

can’t figure out how local variable ‘dA_prev’ referenced before assignment


UnboundLocalError Traceback (most recent call last)
in
1 t_AL, t_Y_assess, t_caches = L_model_backward_test_case()
----> 2 grads = L_model_backward(t_AL, t_Y_assess, t_caches)
3
4 print("dA0 = " + str(grads[‘dA0’]))
5 print("dA1 = " + str(grads[‘dA1’]))

in L_model_backward(AL, Y, caches)
40 # YOUR CODE STARTS HERE
41 current_cache = caches[L-1]
—> 42 dA_prev_temp, dW_temp, db_temp = linear_activation_backward(dAL, current_cache, activation=sigmoid)
43 grads[“dA” + str(L-1)] = dA_prev_temp
44 grads[“dW” + str(L)] = dW_temp

in linear_activation_backward(dA, cache, activation)
37 # YOUR CODE ENDS HERE
38
—> 39 return dA_prev, dW, db

UnboundLocalError: local variable ‘dA_prev’ referenced before assignment

1 Like

Welcome to the community.

As you wrote, linear_activation_backward() supports two activation functions. And, it checks a “string” value to start a process. As you see, “relu” and “sigmoid” are supported.
On the other hand, a parameter that you passed is a variable, sigmoid, not a string “sigmoid”. In this case, linear_activation_backward() can not find an appropriate process, and just returns to a caller with “dA_prev” and others. Of course, those are not processed, i.e, “local variable ‘dA_prev’ referenced before assignment”
So, please ensure to use “string” to call linear_activation_backward().

problem solved, thanks a lot!