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 / ClaimCommon LossCheck
calibrated class probabilitycross-entropy, NLLevaluate NLL, Brier score, calibration, not only accuracy
hard classificationcross-entropy, focal loss, hinge lossthreshold and class prevalence must match deployment
numeric regressionMSE, MAE, Huber, Gaussian NLLunit, scale, censoring, and outlier policy define meaning
ranking or retrievalpairwise, listwise, contrastivesampled negatives must match candidate corpus
sequence generationtoken NLL, sequence loss, RL-style objectiveteacher forcing may not match free-running generation
molecule or structure generationdenoising, score, velocity, validity-filtered objectiveloss may not imply validity, novelty, or task utility
pose or coordinate predictioncoordinate loss, distance loss, equivariant lossatom 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

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?