What is f_compute_gradient and f_compute_cost are doing?

  • Week # 1
  • Link to the classroom item: link here
    As plt_gradients is called in the cell 7 of the jupyter notebook, I took a look inside the lab_utils_uni.py file to see what the function was doing behind the scene.

On line 355, inside the function plt_gradients(), we are passing f_compute_cost and f_compute_gradient functions but I could not find where those functions are defined in the code - I have checked inside lab_utils_uni.py and lab_utils_coomon_py but they are not there.

Can somebody point me where I can find them so I can understand what the code is doing?

TKS

1 Like

The functions are specified when plt_gradients() is called from the notebook:
image

1 Like

Yes, but how the values x, y, w and b are being passed to compute_gradient(x, y, w, b) ?

=

1 Like

image

1 Like

Note that “f_compute_cost()” is a pointer to some function. It doesn’t have to include the function arguments, because those are specified by that function’s definition.

The names of the variables don’t have to match up, because what matters is the order of the parameters in the function call - not their specific names.

That’s a basic software design rule.

It is still not clear to me, is there anywhere in the code that makes compute_cost = f_compute_cost ?

I totally got the parameters passed, my remaining question is still where f_compute_cost was defined and how.

TKS

Yes. This happens via the function definition, when the function is called. It’s automatically part of the python language.

All programming languages have this feature. Variables and objects (such as function pointers) are passed between different execution scopes via being passed as function parameters.