Lesson 4.3core2 h⚡ live demo

Loss functions catalog

Every loss is a negative log-likelihood in disguise — pick the right disguise

The loss is the only thing a network ever sees of your goals. Choosing it well is choosing the probabilistic model you believe in: MSE says "Gaussian noise", cross-entropy says "categorical outcomes", InfoNCE says "find the matching pair". The same catalog reappears unchanged in vision (5.5 Object detection, 5.6 Segmentation), language modeling (7.6 Decoder-only: GPT and autoregressive generation), embeddings (9.2 Embeddings deep dive) and alignment (8.3 Alignment: RLHF, PPO, DPO, GRPO and Constitutional AI), and the log-sum-exp trick you learn here is why your training never produces NaN.

You'll be able to

  • Derive MSE, MAE and cross-entropy as negative log-likelihoods under Gaussian, Laplace and Bernoulli/categorical models
  • Implement cross-entropy from logits stably with the log-sum-exp trick and explain BCE-with-logits
  • Choose a loss from the type of output and the data's quirks (outliers, imbalance, label noise)
  • Explain label smoothing, focal loss, class weighting, KL divergence and contrastive losses and when each applies
  • Compute cross-entropy and its gradient with respect to the logits for a small example

Key ideas

  1. Loss = negative log-likelihood under an assumed output distribution: Gaussian → MSE, Laplace → MAE, Bernoulli → BCE, categorical → cross-entropy.
  2. Cross-entropy from logits: CE = −z_y + LSE(z) with LSE(z) = m + log Σ e^{z_j − m}; never compute log(softmax) by hand.
  3. The gradient of softmax + cross-entropy with respect to the logits is p − y.
Show 4 more key ideas
  1. Huber is quadratic near zero and linear far away — the robust default for regression and value functions.
  2. Label smoothing (ε ≈ 0.1) and focal loss (γ ≈ 2) fight over-confidence and class imbalance respectively; class weights rebalance skewed datasets.
  3. KL divergence matches distributions (distillation, VAEs, RLHF); InfoNCE is cross-entropy over a batch of candidates and trains all embedding models.
  4. Optimize a differentiable surrogate, report the real metric, and check they agree on validation data.

Suggested path through this lesson