Loss Function
A loss function converts prediction error into a scalar training signal. It defines what the optimizer tries to reduce, so it must match the task, target semantics, and evaluation metric.
Empirical risk minimization averages a loss over examples:
Mean squared error is common for regression:
See Mean squared error for the full regression loss and likelihood interpretation.
Cross-entropy is common for categorical prediction:
Here is the predicted probability for class , and is the target distribution or one-hot label.
See Cross-entropy loss and Negative log-likelihood for the probabilistic view.
Batch Reduction
Implementation details matter because the optimizer receives one scalar. For per-example losses :
Changing mean to sum changes gradient scale:
so the effective learning rate changes unless the update rule compensates.
For weighted data:
where may encode class imbalance, sampling correction, confidence, or task weighting.
Training Loss vs Reported Metric
The optimized loss and reported metric can differ:
This is normal when the loss is a smooth surrogate for a discrete or domain-specific metric. The important question is whether lower validation loss and better task metric move together.
Loss Decomposition
For paper notes, write the training objective as:
where is the sampled training unit, is the representation, is the target, is an optional weight, and is regularization. This makes hidden assumptions visible: sampling distribution, label semantics, representation, weighting, and regularization.
Choosing A Loss
| Target / Claim | Common Loss | Check |
|---|---|---|
| calibrated class probability | cross-entropy, NLL | evaluate NLL, Brier score, calibration, not only accuracy |
| hard classification | cross-entropy, focal loss, hinge loss | threshold and class prevalence must match deployment |
| numeric regression | MSE, MAE, Huber, Gaussian NLL | unit, scale, censoring, and outlier policy define meaning |
| ranking or retrieval | pairwise, listwise, contrastive | sampled negatives must match candidate corpus |
| sequence generation | token NLL, sequence loss, RL-style objective | teacher forcing may not match free-running generation |
| molecule or structure generation | denoising, score, velocity, validity-filtered objective | loss may not imply validity, novelty, or task utility |
| pose or coordinate prediction | coordinate loss, distance loss, equivariant loss | atom mapping, symmetry, and frame must be explicit |
Reduction Boundary
For variable-length examples, the denominator matters:
where is the valid token, residue, atom, edge, pixel, or candidate set for example . Averaging per token and averaging per example can optimize different populations.
Common Loss Families
- Cross-entropy loss for categorical labels and next-token prediction.
- Mean squared error for regression and reconstruction.
- Negative log-likelihood for explicit probabilistic models.
- Pairwise or listwise losses for Ranking.
- Contrastive losses for Contrastive learning.
Checks
- Does the loss match the evaluation metric?
- Are targets continuous, categorical, ordinal, structured, or pairwise?
- Is the loss reduced by mean, sum, token count, valid element count, or task-specific weights?
- Does gradient accumulation divide the loss at the right boundary?
- Does imbalance require weighting, sampling, or calibration?
- Is label noise large enough that a robust loss matters?
- Is the sampled unit an example, token, residue, atom, edge, pair, pose, or trajectory?
- Does the loss denominator match the population implied by the reported metric?
- Is the loss compatible with censored, missing, weak, or noisy labels?