C1_W1_Lab04_Gradient_Descent_Soln - Functions

Dear all,
I have a potentially stupid question. In the optional lab C1_W1_Lab04_Gradient_Descent_Soln of the course " Supervised Machine Learning: Regression and Classification", there are two functions that have not been defined at all. These are “gradient_function” and “cost_function”. Both are used in the “gradient_descent” function.

I am wondering where those functions come from. Can anyone help?
Many thanks!

Hi, the name of those functions are arguments of the gradient_function which means that they are the names and you can pass to the function the inputs with any name you want.

From the docstring of the gradient function we get:

 """
    Args:
      x (ndarray (m,))  : Data, m examples 
      y (ndarray (m,))  : target values
      w_in,b_in (scalar): initial values of model parameters  
      alpha (float):     Learning rate
      num_iters (int):   number of iterations to run gradient descent
      cost_function:     function to call to produce cost
      gradient_function: function to call to produce gradient
"""

As you can see cost_function and gradient_function are arguments

When you run this function you pass compute_cost and compute_gradient from a previous step. So, in this case cost_function=compute_cost and gradient_function=compute_gradient

I hope this helps!

4 Likes

Thanks a lot!
I am completely new to all of this and it is fascinating. I just learned that in Python, one can pass other functions as arguments to another function.

I was confused, how Python knows that cost_function is referring to the compute_cost function :slight_smile:

Again many thanks!

1 Like