Subject 32

Loss Functions

A loss function measures how far a model's prediction is from the ground truth. The training process minimizes this value. Choosing the wrong loss function can make a model learn the wrong thing entirely, even if everything else is correct.

Quick Comparison

Loss Function Description Task Output type Key property
MSE (Mean Squared Error) RNNs for time series, CNNs for depth/bounding box regression, any continuous output Regression Continuous value Penalizes large errors heavily; sensitive to outliers
MAE (Mean Absolute Error) Forecasting models (price, demand) where outliers exist and shouldn't dominate Regression Continuous value Treats all errors equally; robust to outliers
Huber Object detection (bounding box regression in YOLO, Faster R-CNN) Regression Continuous value MSE near zero, MAE for large errors; best of both
Binary Cross-Entropy Text classification (spam/not spam), sentiment analysis, CNNs for binary image tasks Binary classification Sigmoid probability Heavily penalizes confident wrong predictions
Categorical Cross-Entropy LLMs (next token prediction), CNNs for image classification, multi-class NLP tasks Multi-class classification Softmax probabilities Only cares about probability given to the correct class
Hinge SVMs, classical text classification before deep learning era Binary classification (SVM) Raw score Zero once margin is satisfied; creates decision boundary margin
KL Divergence VAEs, knowledge distillation (teacher→student LLMs), RLHF in LLMs Distribution matching Probability distributions Not symmetric; zero only when distributions are identical
Contrastive Siamese networks, early sentence embedding models, face verification Metric learning (pairs) Embedding vectors Pulls similar pairs together, pushes dissimilar beyond margin
Triplet FaceNet, sentence transformers, recommendation system embeddings Metric learning (triplets) Embedding vectors Enforces relative ordering: anchor closer to positive than negative

What loss function do LLMs use? β€” Categorical Cross-Entropy (next-token prediction). At each position the model outputs a probability distribution over the entire vocabulary via softmax, and cross-entropy measures how much probability was assigned to the correct next token. Minimizing this over billions of tokens is what drives pre-training.

Batch Size and Loss Computation

Loss functions and batch size are tightly coupled β€” the batch determines how many examples contribute to each loss value and therefore how noisy or stable the gradient signal is.

How loss is computed over a batch

For a batch of N examples, the model computes a per-example loss and then averages (or sometimes sums) them into a single scalar. This scalar is what backpropagation uses.

import torch
import torch.nn.functional as F

# Batch of 4 examples: model predictions vs true labels
logits = torch.tensor([[2.0, 0.5], [0.3, 1.8], [1.5, 0.2], [0.1, 2.5]])
labels = torch.tensor([0, 1, 0, 1])

# Per-example losses
per_example = F.cross_entropy(logits, labels, reduction='none')
print(per_example)  # tensor([0.1269, 0.1269, 0.2769, 0.0821])

# Batch loss = mean of per-example losses (default)
batch_loss = per_example.mean()
print(batch_loss)   # tensor(0.1532)

Impact on gradient quality

Batch size Loss behavior Gradient quality
1 (SGD) Loss is a single example β€” highly variable between steps Very noisy; can escape local minima but converges erratically
Small (8–64) Loss averages a few examples β€” moderate variance Good balance: noisy enough to regularise, stable enough to converge
Large (256+) Loss is a smooth average β€” low variance between steps Accurate gradient estimate; may need higher learning rate
Full dataset Exact loss β€” deterministic True gradient; impractical for large datasets (too slow, too much memory)

Why this matters for loss functions specifically: some losses (like triplet loss or contrastive loss) are very sensitive to batch composition. A batch that happens to contain no hard negatives produces near-zero gradients β€” wasting the entire step. For these losses, batch size and hard-example mining strategy are inseparable design decisions.

Common Interview Questions