Skip to content
E
Educatum

Log Loss

Curated by Educatum AI teamLast updated:
Basic Concepts
Direct Answer

Log loss, also known as binary cross-entropy or logistic loss, evaluates a binary classifier's predicted probabilities against true 0/1 labels. Lower log loss indicates better calibration and accuracy, while confident but wrong predictions incur an exponentially growing penalty that approaches infinity as the predicted probability of the true class approaches zero.

Study Card: Log Loss

Direct Answer

Log loss, also known as binary cross-entropy or logistic loss, is a strictly proper scoring rule and loss function for binary classification models outputting predicted probabilities p^i∈(0,1)\hat{p}_i \in (0, 1):

Llog(y,p^)=−1N∑i=1N[yilog⁡(p^i)+(1−yi)log⁡(1−p^i)]\mathcal{L}_{\text{log}}(y, \hat{p}) = -\frac{1}{N} \sum_{i=1}^{N} \Big[ y_i \log(\hat{p}_i) + (1 - y_i)\log(1 - \hat{p}_i) \Big]

where yi∈{0,1}y_i \in \{0, 1\}. Lower values indicate better probabilistic predictions; a perfect model achieves 0, while confident incorrect predictions (e.g., predicting p^i≈0\hat{p}_i \approx 0 when yi=1y_i = 1) are penalized severely because −log⁡(p^i)→∞-\log(\hat{p}_i) \to \infty as p^i→0\hat{p}_i \to 0. Practical applications include click-through rate (CTR) modeling, spam filtering, credit risk scoring, and medical diagnostics.

Key Terms

  • Loss Function: Quantifies prediction error during training and evaluation; training seeks parameters that minimize expected loss.
  • Binary Classification: Classification task with two mutually exclusive target outcomes (00 or 11).
  • Calibrated Probability: A predicted value in [0,1][0, 1] representing the true empirical likelihood of the positive outcome.
  • Cross-Entropy: Information-theoretic measure of divergence between two probability distributions; log loss is binary cross-entropy over empirical Bernoulli labels.
  • Strict Proper Scoring Rule: A loss minimized uniquely when predicted probabilities match the true conditional class probabilities P(Y=1∣X)P(Y=1 \mid X).

Example

Imagine predicting whether a user clicks an online ad (1 for click, 0 for no click). Using natural logarithms (ln⁡\ln):

  • True label 1, predicted probability 0.9: −ln⁡(0.9)≈0.105-\ln(0.9) \approx 0.105 (small penalty).
  • True label 0, predicted probability 0.1: −ln⁡(1−0.1)=−ln⁡(0.9)≈0.105-\ln(1 - 0.1) = -\ln(0.9) \approx 0.105.
  • True label 1, predicted probability 0.1: −ln⁡(0.1)≈2.303-\ln(0.1) \approx 2.303 (steep penalty for being 90% confident in the wrong class).

Code Implementation

import numpy as np
from sklearn.metrics import log_loss
import matplotlib.pyplot as plt

# Example predictions (probabilities) and true labels (0 or 1)
y_pred = np.array([0.1, 0.3, 0.7, 0.9, 0.2, 0.8])
y_true = np.array([0, 0, 1, 1, 0, 1])

# Calculate log loss
loss = log_loss(y_true, y_pred)
print(f"Log Loss: {loss:.4f}")

# Plotting to illustrate log loss behavior across predicted probabilities
y_prob = np.linspace(0.01, 0.99, 100)

plt.figure(figsize=(8, 5))
plt.plot(y_prob, -np.log(y_prob), label='True class = 1: -log(p)', color='blue')
plt.plot(y_prob, -np.log(1 - y_prob), label='True class = 0: -log(1 - p)', color='red')
plt.title('Binary Log Loss vs Predicted Probability')
plt.xlabel('Predicted Probability (p)')
plt.ylabel('Log Loss')
plt.legend()
plt.grid(True)

# Zoomed plot near p = 0 when true class is 0
plt.figure(figsize=(8, 5))
plt.plot(y_prob, -np.log(1 - y_prob), label='True class = 0: -log(1 - p)', color='red')
plt.title('-log(1 - p) for small predicted probabilities')
plt.xlim([0, 0.2])
plt.xlabel('Predicted Probability (p)')
plt.ylabel('Log Loss')
plt.legend()
plt.grid(True)

# Zoomed plot near p = 1 when true class is 1
plt.figure(figsize=(8, 5))
plt.plot(y_prob, -np.log(y_prob), label='True class = 1: -log(p)', color='blue')
plt.title('-log(p) for predicted probabilities close to 1')
plt.xlim([0.8, 1.0])
plt.xlabel('Predicted Probability (p)')
plt.ylabel('Log Loss')
plt.legend()
plt.grid(True)
plt.show()
  • AUC-ROC vs. Log Loss: AUC-ROC evaluates rank ordering across thresholds and ignores probability calibration, whereas log loss evaluates both discrimination and calibration directly.
  • Other Loss Functions: Hinge loss (used in SVMs) stops penalizing predictions beyond a margin of 1 and does not yield calibrated probabilities, whereas log loss remains smooth and strictly positive everywhere.
  • Regularization: L1 or L2 penalties are routinely added to log loss to prevent weights from growing toward infinity on linearly separable data.
  • Gradient Descent: With a sigmoid activation p^=σ(wTx+b)\hat{p} = \sigma(w^T x + b), the log loss gradient simplifies cleanly to 1NXT(p^−y)\frac{1}{N}X^T(\hat{p} - y).
  • Model Calibration: Techniques like Platt scaling and isotonic regression calibrate model outputs specifically to reduce log loss and produce trustworthy probabilities.
View all →

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.