Course 1, week 2, exercise 6 optimization, Error: local variable referenced before assignment

The error I get is:

This must mean that the program thinks I need to assign values to dw (and probably db) before they are used in the gradient descent step. But in fact, they are assigned before hand as (default code):

    # Retrieve derivatives from grads
    dw = grads["dw"]
    db = grads["db"]

where they are being referenced from the previously defined propogate function from exercise 5.

Hello @Nicklaus_Millican
Check the below post

Please read the error message again more carefully. It is telling you that the grads dictionary is undefined. Where is that defined in your optimize code? It should be one of the return values from propagate, right? So either you neglected to call propagate or perhaps you called that return value something different than grads.

I believe that I’ve covered at least the call propagate issue, calling it before the optimize function. Also, grads is defined in the propagate function.

Here are the relevant bits; only the parts between the # YOUR CODE STARTS/ENDS HERE are my contributions:

From the propagate function:

From the optimize functiion:

{moderator edit - solution code removed}

You have called the propagate function, but you did not store the return values of the function, so they were simply discarded. The grads variable that is within the local scope of propagate has nothing to do with the local scope of optimize, right? This course is not a beginning python course. If you are not already familiar with the concept of the “scope” of a variable, you should seriously consider putting this course on hold and taking a python course first. If you have only programmed in a language like Basic in which all variables are “global”, python is quite a bit more complicated than that.

If you don’t have time for a full python course, at least have a look at some tutorials about functions and variable scope. Googling “python function” and “python variable scope” will find plenty of good information.

Ah, thank you, Paul. I’m an R person and so I’m familiar with the concepts but not implementation. Nevertheless, I got it figured; did not know that storing the output of propagate as grads was necessary.

Understanding function call semantics and variable scope is pretty fundamental. I would suggest you try the searches I listed above and invest an hour in understanding those concepts in python. If you don’t, it is very likely that you will waste far more time than an hour as we go forward here.