Skip to main content
Loss Functions

Loss Functions & Objectives

The Central Role of Loss Functions

A neural network learns by minimizing a loss function (also called objective function, cost function, or criterion). The loss function answers: “How wrong is my prediction?” Think of the loss function as the coach’s scoring rubric. Two different coaches might evaluate the same performance differently — one penalizes big mistakes harshly (MSE), another treats all mistakes equally (MAE). The rubric you choose shapes what the athlete (model) optimizes for. Choose the wrong rubric and you get a model that is technically “optimizing” but optimizing the wrong thing. This is why loss function selection is one of the most consequential design decisions in deep learning. Training=minimizeθ  L(fθ(X),Y)\text{Training} = \underset{\theta}{\text{minimize}} \; \mathcal{L}(f_\theta(X), Y) Where:
  • θ\theta = model parameters (weights and biases)
  • fθ(X)f_\theta(X) = model predictions
  • YY = true labels
  • L\mathcal{L} = loss function
Design Choice: Choosing the right loss function is a design decision that depends on:
  1. The type of problem (regression, classification, ranking)
  2. The output distribution you expect
  3. What kind of errors you care about most

Regression Loss Functions

Mean Squared Error (MSE)

LMSE=1ni=1n(yiy^i)2\mathcal{L}_{\text{MSE}} = \frac{1}{n}\sum_{i=1}^{n}(y_i - \hat{y}_i)^2
Properties: When to use:
  • Regression with Gaussian noise assumption (MSE is the maximum likelihood estimator when errors are normally distributed)
  • When you want to penalize large errors heavily — a prediction off by 10 is penalized 100x more than one off by 1
Mathematical intuition: Minimizing MSE is equivalent to maximizing the likelihood of the data under a Gaussian noise model. This is not just a convenient formula — there is a deep statistical justification. If your errors truly follow a bell curve, MSE is the optimal loss.

Mean Absolute Error (MAE / L1 Loss)

LMAE=1ni=1nyiy^i\mathcal{L}_{\text{MAE}} = \frac{1}{n}\sum_{i=1}^{n}|y_i - \hat{y}_i|
Properties: When to use:
  • When outliers are expected
  • When you care about median prediction

Huber Loss (Smooth L1)

Combines the best of MSE and MAE: LHuber={12(yy^)2if yy^δδyy^12δ2otherwise\mathcal{L}_{\text{Huber}} = \begin{cases} \frac{1}{2}(y - \hat{y})^2 & \text{if } |y - \hat{y}| \leq \delta \\ \delta|y - \hat{y}| - \frac{1}{2}\delta^2 & \text{otherwise} \end{cases}
Regression Loss Functions

Classification Loss Functions

Binary Cross-Entropy (BCE)

For binary classification with output y^(0,1)\hat{y} \in (0, 1): LBCE=1ni=1n[yilog(y^i)+(1yi)log(1y^i)]\mathcal{L}_{\text{BCE}} = -\frac{1}{n}\sum_{i=1}^{n}\left[y_i \log(\hat{y}_i) + (1-y_i)\log(1-\hat{y}_i)\right]
Intuition: BCE measures the “surprise” the loss function experiences when it sees the true label given your prediction. If you predicted 0.99 for a positive example, there is almost no surprise — low loss. If you predicted 0.01 for a positive example, the loss function is shocked — the loss explodes toward infinity.
  • Confident and correct: Low loss (you predicted what happened)
  • Confident and wrong: Very high loss (the log penalty is brutal — this is by design)
  • Uncertain (0.5): Medium loss (you are admitting you do not know)
This asymmetric penalty structure is what makes cross-entropy so effective for classification: it ruthlessly punishes confident wrong answers, which forces the model to be calibrated rather than just accurate.

Categorical Cross-Entropy

For multi-class classification with KK classes: LCE=1ni=1nk=1Kyiklog(y^ik)\mathcal{L}_{\text{CE}} = -\frac{1}{n}\sum_{i=1}^{n}\sum_{k=1}^{K}y_{ik}\log(\hat{y}_{ik})

Cross-Entropy with Logits

In practice, we use the numerically stable version that combines softmax + cross-entropy. This is not just a convenience — it is a necessity.
Critical pitfall: Never compute softmax then log separately. When softmax outputs a value very close to 0, taking its log produces negative infinity. The combined LogSoftmax (or CrossEntropyLoss in PyTorch, which fuses both operations) uses the log-sum-exp trick to avoid this. In production, always use nn.CrossEntropyLoss(logits, targets) rather than nn.NLLLoss(F.log_softmax(logits), targets) — they are mathematically identical but the former is numerically safer.

Visualizing Loss Landscapes

Loss Landscape Visualization

Advanced Loss Functions

Focal Loss

Addresses class imbalance by down-weighting easy examples: Lfocal=αt(1pt)γlog(pt)\mathcal{L}_{\text{focal}} = -\alpha_t (1 - p_t)^\gamma \log(p_t)

Label Smoothing

Prevents overconfidence by softening targets: yk={1ϵ+ϵKif k=true classϵKotherwisey'_k = \begin{cases} 1 - \epsilon + \frac{\epsilon}{K} & \text{if } k = \text{true class} \\ \frac{\epsilon}{K} & \text{otherwise} \end{cases}
Practical tip: Label smoothing with epsilon=0.1 is nearly free to implement and consistently improves performance by 0.2-0.5% on classification tasks. It is especially valuable when your training labels might contain noise — which in real-world datasets, they almost always do. Think of it as teaching the model epistemic humility.

Contrastive Loss

For learning embeddings where similar items are close: Lcontrastive=(1y)12d2+y12max(0,md)2\mathcal{L}_{\text{contrastive}} = (1-y) \cdot \frac{1}{2}d^2 + y \cdot \frac{1}{2}\max(0, m - d)^2

Triplet Loss

For learning embeddings with anchor-positive-negative triplets: Ltriplet=max(0,d(a,p)d(a,n)+m)\mathcal{L}_{\text{triplet}} = \max(0, d(a, p) - d(a, n) + m)

Loss Functions in PyTorch

Custom Loss Functions


Choosing the Right Loss Function

Decision Guide

Common Mistakes

Debugging hint: If your loss suddenly goes to NaN during training, the most common culprits are: (1) log of zero or negative number, (2) division by zero in normalization, (3) learning rate too high causing weight explosion. Check your loss function’s edge cases first — add epsilon clipping to any log or division operation.

Exercises

Implement these losses from scratch and compare their gradients:
  1. Hinge loss: max(0,1yy^)\max(0, 1 - y \cdot \hat{y})
  2. Exponential loss: eyy^e^{-y \cdot \hat{y}}
  3. Logistic loss: log(1+eyy^)\log(1 + e^{-y \cdot \hat{y}})
Create a loss function for a model that simultaneously:
  1. Classifies images (cross-entropy)
  2. Predicts bounding boxes (smooth L1)
  3. Estimates uncertainty (KL divergence)
How do you weight the different losses?
Train a small network on a 2D classification task. Visualize the loss landscape:
  1. Along the line connecting initial and final weights
  2. In a random 2D plane around the minimum
What do you observe about the landscape shape?
On an imbalanced dataset (1:10 ratio):
  1. Train with BCE, Weighted BCE, and Focal Loss
  2. Tune the gamma parameter in Focal Loss
  3. Plot precision-recall curves for each
Which loss gives the best F1 score on the minority class?

Key Takeaways


What’s Next

We’ve covered the foundations! Now let’s move to powerful architectures:

Module 6: Convolutional Neural Networks

The architecture that revolutionized computer vision — convolutions, filters, and feature maps.

Interview Deep-Dive

Strong Answer:
  • Cross-entropy is the negative log-likelihood of the data under the model’s predicted distribution. For a classification model that outputs probabilities via softmax, minimizing cross-entropy is mathematically equivalent to maximizing the likelihood of the correct labels — it is the maximum likelihood estimator for categorical data.
  • MSE for classification has two fundamental problems: (1) the gradient L/z\partial L / \partial z for MSE with sigmoid output is (y^y)σ(z)(\hat{y} - y) \cdot \sigma'(z), which includes the sigmoid derivative σ(z)\sigma'(z). When the model makes a confident wrong prediction (sigmoid saturated), σ(z)0\sigma'(z) \approx 0, so the gradient vanishes precisely when it should be largest. Cross-entropy’s gradient is simply y^y\hat{y} - y — no sigmoid derivative factor — so confident wrong predictions produce the largest gradients.
  • (2) MSE’s loss landscape for classification has many flat regions (where sigmoid is saturated) and is non-convex with respect to the logits. Cross-entropy’s landscape is convex with respect to the logits (for a fixed linear model), leading to smoother optimization.
  • Probabilistic interpretation: cross-entropy measures the KL divergence (plus a constant) between the true label distribution and the predicted distribution. It directly quantifies how surprised the model is by the true labels. MSE has no such information-theoretic interpretation for categorical data.
Follow-up: When might you legitimately use MSE for classification?In knowledge distillation, where the “labels” are soft probability distributions from a teacher model rather than hard 0/1 labels. MSE between the student’s logits and the teacher’s logits can work well because both are continuous-valued and the sigmoid saturation problem is less severe when targets are not at the extremes. Some mean-teacher methods use MSE loss on the consistency between augmented views. Also, in multi-label classification with many labels, MSE on the probability vectors can be more stable than BCE when the label space is very large and sparse.
Strong Answer:
  • Focal loss was introduced in the RetinaNet paper (Lin et al., 2017) to address the extreme class imbalance in object detection. In a typical image, there might be 100,000 background anchor boxes and only 10 foreground objects. Standard cross-entropy treats all examples equally, so the model is overwhelmed by the massive number of easy negatives.
  • The formula: FL(pt)=(1pt)γlog(pt)FL(p_t) = -(1 - p_t)^\gamma \log(p_t), where ptp_t is the predicted probability for the true class. The key innovation is the modulating factor (1pt)γ(1 - p_t)^\gamma.
  • When the model predicts correctly with high confidence (pt0.99p_t \approx 0.99), the factor (10.99)2=0.0001(1 - 0.99)^2 = 0.0001 essentially eliminates this example’s contribution to the loss. The model no longer wastes gradient budget on examples it already classifies easily.
  • When the model is wrong (pt0.1p_t \approx 0.1), the factor (10.1)2=0.81(1 - 0.1)^2 = 0.81 keeps the loss nearly at its full value. Hard examples dominate the training signal.
  • The gamma parameter controls the degree of focus. At γ=0\gamma = 0, focal loss reduces to standard cross-entropy. At γ=2\gamma = 2 (the most common setting), easy examples are down-weighted by 100×100\times or more. At γ=5\gamma = 5, the focusing is extreme and can make training unstable because too few examples contribute meaningful gradients.
  • In practice, γ=2\gamma = 2 combined with α\alpha class weighting is the standard recipe for detection and any heavily imbalanced classification problem.
Follow-up: What is the relationship between focal loss and hard example mining? Why is focal loss generally preferred?Hard example mining (OHEM) explicitly selects the top-k hardest examples per batch and only computes loss on those. Focal loss achieves a similar effect implicitly by continuously reweighting all examples based on difficulty. Focal loss is preferred because: (1) it is differentiable and integrates smoothly into standard training loops, (2) it considers all examples rather than discarding easy ones entirely, which provides a small but non-zero learning signal from easy examples, and (3) it does not require the additional sorting step that makes OHEM 20-30% slower. The continuous reweighting also adapts dynamically as the model improves — an example that was hard at epoch 5 may become easy by epoch 50 and automatically receive less weight.
Strong Answer:
  • Multi-task learning requires a combined loss: Ltotal=λ1Lcls+λ2LboxL_{total} = \lambda_1 L_{cls} + \lambda_2 L_{box}, where LclsL_{cls} is cross-entropy for classification and LboxL_{box} is smooth L1 (Huber loss) for bounding box regression. The challenge is balancing these losses so neither dominates.
  • Why smooth L1 for bounding boxes: smooth L1 is quadratic for small errors (providing strong gradients near the optimum) and linear for large errors (preventing outlier boxes from dominating training). Pure MSE would cause one badly predicted box to overwhelm the gradients for all well-predicted boxes.
  • Balancing loss terms: naive fixed-weight approaches (λ1=1,λ2=1\lambda_1 = 1, \lambda_2 = 1) fail because the loss scales and gradient magnitudes differ. Three principled approaches:
    • Manual tuning: start with equal weights, observe which task dominates (monitor individual loss curves), and adjust. Simple but tedious.
    • Uncertainty-based weighting (Kendall et al., 2018): learn the weight for each task as λi=1/(2σi2)\lambda_i = 1/(2\sigma_i^2) where σi\sigma_i is a learned task uncertainty. Tasks with higher uncertainty receive lower weight. This is mathematically principled (derived from multi-task likelihood).
    • GradNorm: normalize the gradient norms from each loss so they are approximately equal, preventing one task from dominating the shared representations.
  • In modern object detectors (YOLO, Faster R-CNN), the standard approach uses fixed weights that are tuned on the validation set, typically with the classification loss weighted lower than the box regression loss because classification is easier to learn.
Follow-up: What happens if the two losses conflict — optimizing one hurts the other?This is called negative transfer, and it occurs when the optimal shared representations for one task are suboptimal for the other. For example, fine-grained classification benefits from texture features, while bounding box regression benefits from shape features. Signs: total loss decreases but one individual loss increases. Fixes: (1) use task-specific heads with a shared backbone but stop gradients from one task flowing into the other’s head, (2) use a multi-gate mixture-of-experts architecture where each task selects different experts from the shared backbone, or (3) simply train separate models if the tasks are truly at odds. In practice, classification and box regression are well-aligned and negative transfer is rare for this specific combination.
Strong Answer:
  • Label smoothing replaces hard targets [1,0,0,0][1, 0, 0, 0] with soft targets [0.925,0.025,0.025,0.025][0.925, 0.025, 0.025, 0.025] (for ϵ=0.1\epsilon = 0.1 and K=4K = 4 classes). Instead of driving the model to produce infinite logits for the correct class, it encourages the model to produce high but finite confidence.
  • Why it improves generalization: (1) It prevents the model from becoming overconfident, which is a form of overfitting to the training labels. A model trained with hard labels can learn to output logits of magnitude 20+, which means tiny input perturbations can flip predictions. Label smoothing keeps logits moderate, producing more robust decision boundaries. (2) It implicitly regularizes by adding a uniform distribution component to the target, which is equivalent to adding a small KL-divergence penalty toward the uniform distribution. (3) It improves calibration — the predicted probabilities more accurately reflect the true uncertainty.
  • When it can hurt: (1) In knowledge distillation, where the student should learn to match the teacher’s sharp predictions exactly. Smoothing the teacher’s targets reduces the information content. (2) When labels are genuinely certain and the data is clean — e.g., mathematical theorem proving where there is exactly one correct answer. (3) In metric learning or contrastive learning where you need the model to distinguish between semantically similar classes with high precision.
  • Standard practice: use ϵ=0.1\epsilon = 0.1 for image classification and ϵ=0.1\epsilon = 0.1 for machine translation. It is essentially free to implement (one line in PyTorch: nn.CrossEntropyLoss(label_smoothing=0.1)) and provides 0.2-0.5% accuracy improvement on most benchmarks.
Follow-up: Label smoothing assigns equal probability to all incorrect classes. Is that always the right assumption?No, and this is a limitation. A cat image should have some probability mass on “dog” and “tiger” but very little on “airplane.” Knowledge distillation implicitly provides this structure — the teacher’s soft predictions encode inter-class similarity. Some approaches use structured label smoothing based on a class hierarchy or embedding distance, assigning more probability to semantically similar incorrect classes. In practice, the uniform assumption works surprisingly well because the model learns to ignore the uninformative uniform noise and focus on the correct class signal. The benefit comes primarily from preventing infinite-logit overconfidence, not from the specific distribution over incorrect classes.