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.
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 is defined by two distinct parts, each active depending on the true label. When the true label is 1, the cost is calculated as , where is the predicted probability of the positive class. In this case, since , the term simplifies to . Conversely, when the true label is 0, the cost is calculated as . Here, since , the term simplifies to . This design ensures that only the relevant term contributes to the loss for any given instance.
The Log Loss for a single instance, given a true label and a predicted probability , is:
This combined formula elegantly handles both cases for binary classification. When , the term becomes zero, leaving only . When , the term becomes zero, leaving . 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.
Calculating Log Loss in Practice
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.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, , 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