Cost Function (Log Loss)

Why Squared Error Falls Short for Classification

Why does using squared error for a binary classifier lead to unstable training and poor performance? This occurs because squared error assumes a linear relationship between input and output, which fundamentally clashes with the non-linear, probabilistic output of a sigmoid function in logistic regression.

Squared Error vs. Predicted Probability
This chart shows the squared error for a single prediction against the predicted probability. The 'True Label = 1' trace plots (1−p)2(1-p)^2, while the 'True Label = 0' trace plots p2p^2. Both curves are convex individually, but when combined with the sigmoid function's output, the overall loss surface for model parameters becomes non-convex, leading to multiple local minima and flat gradients that hinder optimization.
Loading chart...
Key Insight: Squared error, while convex for a single prediction, creates a non-convex loss surface when combined with the sigmoid function, making gradient descent unstable and prone to local minima.

The non-convex nature of squared error, when applied to the sigmoid output, means that gradient descent can get stuck in local minima, failing to find the optimal model parameters. This instability necessitates a different approach for binary classification. A suitable cost function must offer a smooth, convex error surface, ensuring that gradient-based optimization methods reliably converge to a global minimum. It also needs to heavily penalize predictions that are confidently wrong, while gently rewarding those that are confidently correct.

Log Loss: Penalizing Uncertainty

Log Loss, also known as binary cross-entropy, is the standard cost function for logistic regression. It is specifically designed to handle probabilistic outputs from binary classifiers. Log Loss assigns a very high penalty when a model predicts a probability that is confidently incorrect. For example, if the true label is 1, but the model predicts a probability of 0.01, the penalty will be substantial. Conversely, if the model predicts 0.99 for a true label of 1, the penalty is very small, approaching zero.

Log Loss vs. Predicted Probability
This chart illustrates the Log Loss for a single prediction against the predicted probability. The 'True Label = 1' trace plots −log⁡(p)-\log(p), while the 'True Label = 0' trace plots −log⁡(1−p)-\log(1-p). Notice how the cost approaches infinity as the predicted probability diverges from the true label, and approaches zero as it converges. The y-axis is clipped for readability.
Loading chart...
Key Insight: Log Loss heavily penalizes confident incorrect predictions, with the cost approaching infinity as a prediction diverges from the true label, ensuring strong gradients for optimization.

Log Loss is defined by two distinct parts, each active depending on the true label. When the true label yy is 1, the cost is calculated as −ylog⁡(p)-y \log(p), where pp is the predicted probability of the positive class. In this case, since y=1y=1, the term simplifies to −log⁡(p)-\log(p). Conversely, when the true label yy is 0, the cost is calculated as −(1−y)log⁡(1−p)-(1-y) \log(1-p). Here, since y=0y=0, the term simplifies to −log⁡(1−p)-\log(1-p). This design ensures that only the relevant term contributes to the loss for any given instance.

📐 Log Loss Formula (Single Instance)

The Log Loss for a single instance, given a true label yy and a predicted probability pp, is:

Cost=−[ylog⁡(p)+(1−y)log⁡(1−p)]\text{Cost} = -[y \log(p) + (1-y) \log(1-p)]

This combined formula elegantly handles both cases for binary classification. When y=1y=1, the (1−y)log⁡(1−p)(1-y)\log(1-p) term becomes zero, leaving only −ylog⁡(p)-y\log(p). When y=0y=0, the ylog⁡(p)y\log(p) term becomes zero, leaving −(1−y)log⁡(1−p)-(1-y)\log(1-p). This structure results in a convex and differentiable function, which is ideal for optimization algorithms like gradient descent. The convexity guarantees that gradient descent will converge to the global minimum, providing a stable and effective training process for logistic regression models.

Log Loss (Binary Cross-Entropy)
A cost function used in binary classification to quantify the uncertainty of a model's probabilistic predictions, penalizing confident incorrect predictions more heavily than uncertain or correct ones.
Example: If a model predicts a 90% chance (0.9) that an email is spam, but it's actually not spam (true label 0), Log Loss will assign a high penalty. If it predicts 10% (0.1) for a non-spam email, the penalty is low.

Calculating Log Loss in Practice

pythonCalculating Log Loss with NumPy
Try It Yourself
Modify the y_pred_2 array in the code example to make the model confidently correct for the first instance (y_true=1). Observe how the Log Loss changes compared to the original confidently wrong prediction.
python
Check Your Understanding
What happens to Log Loss if a model predicts a probability of 0.99 for a true label of 0?
Key Takeaways
  • Squared error is unsuitable for binary classification because it creates a non-convex loss surface when combined with the sigmoid function, hindering stable optimization.

  • Log Loss (Binary Cross-Entropy) is designed for probabilistic binary classification outputs, providing a convex and differentiable loss function.

  • It heavily penalizes predictions that are confidently wrong (e.g., predicting 0.99 for a true label of 0), with the cost approaching infinity.

  • The Log Loss formula, −[ylog⁡(p)+(1−y)log⁡(1−p)]-[y \log(p) + (1-y) \log(1-p)], elegantly handles both positive and negative true labels.

  • Log Loss ensures that gradient descent can reliably converge to a global minimum, leading to stable and accurate logistic regression models.

Try it yourself

Logistic Regression Playground

Move a decision boundary and watch log-loss respond. See what the sigmoid turns distance into, and why a confident mistake costs so much.

Open the lab

← All lessons in Logistic Regression

Ready to keep this from fading?

Bitelrn turns lessons like this into a full course — quizzes, a knowledge map, and spaced review.

Get started free