Exercise: Using Gradient Descent to Minimize a Cost Function

Learn how to use the gradient descent method to minimize the cost function.

Approach to minimize the cost function

In this exercise, our task is to find the best set of parameters in order to minimize the following hypothetical cost function: y=f(x)=x22xy = f(x) = x^2 – 2x. To do this, we will employ gradient descent, which was described in the preceding lesson. Perform the following steps to complete the exercise:

  1. Create a function that returns the value of the cost function and look at the value of the cost function over a range of parameters. You can use the following code to do this:

    X_poly = np.linspace(-3,5,81)
    print(X_poly[:5], '...', X_poly[-5:]) 
    def cost_function(X): 
        return X * (X-2) 
    y_poly = cost_function(X_poly) 
    plt.plot(X_poly, y_poly) 
    plt.xlabel('Parameter value') 
    plt.ylabel('Cost function') 
    plt.title('Error surface')
    

    You will obtain the following plot of the cost function:

Get hands-on with 1200+ tech skills courses.