Machine LearningLogistic Regression and Loss Functions

Why Is Squared-Error Loss Usually Avoided When Training Logistic Regression?

Question 300 of 303 · Updated

On this page

Squared-error loss is not mathematically forbidden in logistic regression. You can train a sigmoid-based classifier with it. However, it is usually not the preferred objective because it is less naturally matched to binary probability estimation, can create a non-convex optimization problem, and may produce very weak gradients when the model is confidently wrong.

For standard logistic regression, binary cross-entropy (log loss) is usually preferred. It follows directly from a Bernoulli probability model, gives a convenient gradient, and produces a convex objective for a linear logit model.

1. What logistic regression predicts#

For binary classification, the target is . Logistic regression computes a linear score:

LaTeX

and converts it into a probability:

LaTeX

The model estimates . A threshold, often 0.5, can later convert that probability into a class label.

2. What happens if we use squared-error loss?#

A natural attempt is squared error between the target and predicted probability:

LaTeX

For examples:

LaTeX

This is a valid objective. It is also closely related to the Brier score used to evaluate probabilistic predictions. The issue is not that squared error cannot be calculated; it is that its combination with the sigmoid and a linear logit has disadvantages for training.

3. The sigmoid can make the gradient very small#

The sigmoid derivative is:

LaTeX

Using the chain rule, the derivative of squared error with respect to the logit is:

LaTeX

The factor becomes close to zero when the probability is near zero or one. That can be a problem when the prediction is confidently wrong.

Example: a positive example predicted as almost certainly negative

  • True label:
  • Predicted probability:
  • Probability error:
  • Sigmoid derivative:

Therefore:

LaTeX

The model is very wrong, but the gradient signal is tiny. Gradient-based training may make only a very small correction. The same issue appears for a negative example predicted with probability close to one. This is gradient saturation.

4. Cross-entropy avoids this particular gradient problem#

For a binary target, binary cross-entropy is:

LaTeX

Combining this loss with the sigmoid gives a simpler derivative with respect to the logit:

LaTeX

Compare the gradients:

LaTeX
LaTeX

For the same example, and , the squared-error gradient is approximately , while the cross-entropy gradient is . Cross-entropy retains a strong corrective signal for a confidently wrong prediction. This is one major reason it is preferred for training logistic regression.

5. Cross-entropy matches the Bernoulli probability model#

A binary target is naturally modeled using a Bernoulli distribution:

LaTeX

The negative log-likelihood for one observation is:

LaTeX

This is exactly binary cross-entropy. Minimizing average cross-entropy is equivalent to maximizing the likelihood of the observed binary labels under the model's Bernoulli assumptions.

Squared error is associated with a Gaussian-noise likelihood in the standard regression setting. That is a less natural likelihood model for a binary outcome. The distinction is about choosing a probability model that fits the target, not whether squared error is mathematically legal.

6. The squared-error objective can be non-convex#

For ordinary linear regression, squared error with a linear prediction is convex in the model parameters.

Logistic regression changes the prediction to a sigmoid of the linear score. With squared error on that probability, the composition generally produces a non-convex objective in the weights. Non-convexity means the optimization landscape can contain local minima or flat regions, so gradient-based optimization has weaker global guarantees.

With the usual linear logit and binary cross-entropy, the logistic-regression objective is convex in the weights (and remains convex when standard convex L1 or L2 regularization is added). Convexity does not mean every training configuration is automatically easy, but it gives a valuable guarantee: every local minimum is global.

This claim is specific to ordinary logistic regression with a linear score. Adding a nonlinear neural network before the sigmoid generally makes the overall parameter objective non-convex even when cross-entropy is used.

7. Cross-entropy penalizes confident mistakes more strongly#

Consider a positive example, so the true label is . If the model predicts , it is confidently wrong.

Squared error gives:

LaTeX

Cross-entropy gives:

LaTeX

As the predicted probability approaches zero for a positive example, cross-entropy grows without bound. Squared error on probabilities is bounded because both the target and prediction lie between zero and one.

This makes cross-entropy particularly sensitive to confidently incorrect probability estimates. Whether that behavior is desirable depends on the application, but it is a natural fit for likelihood-based probability estimation.

8. Is squared error ever appropriate for classification?#

Yes. It is important not to turn the usual recommendation into an absolute rule.

The Brier score is the mean squared difference between predicted probabilities and binary outcomes:

LaTeX

It is a proper scoring rule for probabilistic forecasts: in expectation, it is minimized by reporting the true event probability. It can be useful for evaluating probability forecasts and can also be used as a training objective in some settings.

However, when combined with the sigmoid parameterization of standard logistic regression, it generally loses the convenient convexity and stronger gradient behavior of cross-entropy. Thus it is less commonly selected as the primary training loss for ordinary logistic regression.

9. Practical implementation#

In scikit-learn, logistic regression uses regularized logistic loss by default. In deep-learning frameworks, prefer a numerically stable logits-based binary cross-entropy implementation rather than applying sigmoid yourself and then taking logarithms manually.

For example, in PyTorch, pass raw model logits and binary targets to the function torch.nn.BCEWithLogitsLoss. This combines the sigmoid and binary cross-entropy in a numerically stable calculation. Do not apply sigmoid before passing logits to this loss. If using an API that expects probabilities instead, use its stable implementation and handle extreme values safely.

10. Summary#

  • Statistical fit: Cross-entropy is the negative log-likelihood of a Bernoulli model for binary labels.
  • Gradient: Cross-entropy with sigmoid simplifies to ; squared error includes the additional sigmoid derivative factor .
  • Confident mistakes: Cross-entropy produces a strong penalty as the model becomes confidently wrong; squared error on probabilities is bounded.
  • Optimization: Cross-entropy gives a convex objective for standard linear logistic regression; squared error on sigmoid probabilities is generally non-convex.
  • Use cases: Cross-entropy is the usual training choice for logistic regression; squared probability error (Brier score) remains a valid scoring rule and can be useful in other contexts.

Interview-ready answer#

Squared-error loss is usually avoided in logistic regression because the sigmoid combined with squared error can create a non-convex objective and weak gradients for confidently wrong predictions. Its gradient contains the sigmoid derivative , which becomes very small near probabilities zero and one. Binary cross-entropy is better matched to binary labels because it is the negative log-likelihood of a Bernoulli model. Its gradient with respect to the logit simplifies to , and the objective is convex for standard linear logistic regression. Squared error is not invalid, however; the Brier score is a proper scoring rule for probabilities.

Related questions

Explore other topics:Deep LearningMLOps