Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Logistic Regression Gradient Derivation

Hello! Welcome back to our series on the mathematical foundations of machine learning.

In our last lesson, we successfully derived the objective function for logistic regression: the average Binary Cross-Entropy Loss, . We established that minimizing this function is equivalent to finding the Maximum Likelihood Estimate for our model's parameters. Our cost function is:

where .

Today, we will tackle the next crucial step: deriving the gradient of this objective function, . The gradient is a vector of partial derivatives, , which points in the direction of the steepest ascent of the cost function. By moving in the opposite direction, we can iteratively update our parameters to find the minimum cost. This derivation is the mathematical core of the gradient descent optimization algorithm, which we will implement in the next lesson.

1. Essential Calculus: The Derivative of the Sigmoid Function

Before we derive the gradient of the entire cost function, let's derive a key intermediate result: the derivative of the sigmoid function, . This beautiful and compact result is a cornerstone of the derivations for many neural network models, not just logistic regression.

Recall the sigmoid function:

To find its derivative with respect to , we can write it as and apply the chain rule.

  1. Apply the power rule and chain rule:

  2. Perform an algebraic manipulation: Now, we'll use a clever trick to express this result in terms of itself.

    Notice that the first term is simply . Let's manipulate the second term:

  3. Combine the terms:

    This elegant result—that the derivative of the sigmoid function can be expressed in terms of the function's own output—is what makes the gradient calculation for logistic regression remarkably clean.

2. Deriving the Gradient with the Chain Rule

We are now ready to compute the partial derivative of the cost function with respect to a single parameter .
Because the derivative of a sum is the sum of derivatives, we can first focus on the cost contribution from a single training example and then sum the results. The cost for one example is:

We want to find . This is a perfect application for the chain rule, as is a function of , which is a function of , which in turn is a function of .

The following video provides a clear, step-by-step walkthrough of this exact process. It uses slightly different notation (e.g., L for loss, a for activation/hypothesis, w for weights), but the mathematical steps are identical.

Derivative of Cost function for Logistic Regression | Machine Learning

The video 'Derivative of Cost function for Logistic Regression' by Learn With Jay methodically breaks down the gradient calculation into manageable pieces using the chain rule. This will guide you through each step of the derivation.

Watch from 01:53 to 06:48. Follow the calculation of the three components of the chain rule: ∂L/∂a, ∂a/∂z, and ∂z/∂w, and see how they are combined to get the final result for a single observation.

Let's re-trace those steps here using our notation to solidify the derivation.

  1. Compute :

  2. Compute :
    This is the sigmoid derivative we derived earlier. Since , we have:

  3. Compute :
    The linear component is . The partial derivative with respect to is simply the coefficient of :

  4. Combine the components:

    The terms cancel out beautifully, leaving us with a very simple result:

This is the gradient of the cost for a single example. To get the gradient of the total average cost function , we sum over all examples and divide by :

For a formal written account of this derivation, the following resource is excellent.

Logistic Regression

The Stanford CS109 handout 'Logistic Regression' provides a concise and rigorous derivation of this same gradient. Seeing the derivation in a formal document will reinforce your understanding.

Read the section 'Derivations' on page 4 of the PDF. Focus on the section 'Derivatives Without Tears' which uses the same chain rule decomposition we just performed. This will help you map the conceptual steps to a formal proof.

Test your understanding!

Given a single training example with features (where is the bias term) and true label . Your model's current parameters are .

First, calculate the linear component and the hypothesis .
Then, using the final formula we derived, calculate the partial derivative of the cost for this single example with respect to , which is .

Show answer
  1. Calculate and :

  2. Calculate the partial derivative:
    The formula is .
    We need the derivative with respect to , so we use .

    This negative value indicates that to decrease the cost, we should increase the value of .

3. A Surprising and Elegant Connection

Let's look at our final gradient expression again:

This is the average of the prediction error for each data point, multiplied by its corresponding feature value. This form is remarkably intuitive.

What is more remarkable is its similarity to the gradient for linear regression. The gradient descent update rule for both models takes the form:

If you recall from your work with linear regression, its gradient also has the form . The update rules look identical!

Locally Weighted & Logistic Regression | Stanford CS229: Machine Learning - Lecture 3 (Autumn 2018)

In this segment from his famous CS229 course, Andrew Ng highlights this elegant similarity between the linear and logistic regression update rules. He explains why this is not a coincidence.

Watch from 1:02:18 to 1:04:15. Pay close attention to the comparison he makes and the crucial difference he points out: the definition of the hypothesis function, h(x).

The key insight is that while the form of the update rule is the same, the definition of the hypothesis is different:

  • Linear Regression:
  • Logistic Regression:

This elegant unification occurs because both linear and logistic regression are special cases of a broader class of algorithms called Generalized Linear Models (GLMs). This is a topic for another time, but it's a profound result that demonstrates a deep connection between seemingly different models.

Conclusion

Today, we successfully derived the gradient for the logistic regression cost function, a pivotal step in understanding how the model learns.

Key Takeaways:

  • The derivative of the sigmoid function is , a result which simplifies the gradient derivation.
  • Using the chain rule, we decomposed the complex derivative of the cost function into three simpler parts.
  • The partial derivative of the average cross-entropy cost function with respect to a parameter is:
  • The logistic regression gradient update rule has the same form as the linear regression rule, differing only in the definition of the hypothesis function .

Preview of the Next Lesson:
With the cost function and its gradient in hand, we have all the components needed for optimization. In our next lesson, we will apply the gradient descent algorithm to iteratively update the parameters and train a logistic regression model on a dataset.

Can't find a good explanation? Sign up and we'll make it for you

Sign up