Skip to content

2.4 MLE, MAP and loss functions

ready≈ 8.5 hoursNeeds: 2.3, 1.5Unlocks: 3.8
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Derivation stepper)
  4. 4 Do
  5. 5 Check

So far each loss has arrived as a choice: squared error for regression, log-loss for classification. They are one recipe (UDL §5.1–5.2). Have the model output the parameters of a distribution over yy. Then choose θ\boldsymbol{\theta}parametersAll learnable parameters of a model, collected into one vector.Introduced in 1.6 Optimization. UDL uses φ (phi) for model parameters; CS229 and most papers use θ. to make the observed targets as probable as possible, which is maximum likelihood from module 1.4. Products of probabilities underflow, and the log is monotone, so minimise the negative log likelihood, L(θ)=−∑ilog⁡p(yi∣xi;θ)L(\boldsymbol\theta) = -\sum_i \log p(y_i \mid \mathbf{x}_i; \boldsymbol\theta).

The distribution you pick decides the loss:

  • a Gaussian with mean f(x)f(\mathbf{x}) and fixed variance gives squared error (plus a constant);
  • a Bernoulli with probability σ(f(x))\sigma(f(\mathbf{x})) gives binary cross-entropy;
  • a categorical with probabilities softmax(f(x))\mathrm{softmax}(f(\mathbf{x})) gives softmax cross-entropy, the loss of every classifier and language model in this course (UDL §5.3–5.5).

The model’s raw outputs, called logits, go straight into the loss. Computing log⁡(softmax(z))\log(\mathrm{softmax}(\mathbf{z})) in one step, with the maximum subtracted first, never takes the log of an underflowed zero (the log-sum-exp trick; UDL §5.5 for softmax on logits; PyTorch, CrossEntropyLoss, which takes logits for exactly this reason). The gradient is again prediction minus target, as derived below.

MAP. Maximum likelihood trusts the data alone. Put a prior p(θ)p(\boldsymbol\theta) on the weights and maximise the posterior instead: −log⁡p(θ∣data)=NLL−log⁡p(θ)+const-\log p(\boldsymbol\theta\mid\text{data}) = \text{NLL} - \log p(\boldsymbol\theta) + \text{const}. A zero-mean Gaussian prior adds 12τ2∥w∥2\frac{1}{2\tau^2}\lVert\mathbf{w}\rVert^2, which is L2 regularization, with λ=σ2/τ2\lambda = \sigma^2/\tau^2. A tighter prior or noisier data means stronger shrinkage (MML §8.3.2; UDL §9.1). The bias is usually left out of the penalty, because shrinking the overall level of yy toward 0 has no justification.

Softmax cross-entropy gradient is softmax(z) − onehot(y)step 1 of 4
  1. ℓ(z)=−log⁡ezy∑jezj=−zy+log⁡∑jezj\ell(\mathbf{z}) = -\log\frac{e^{z_y}}{\sum_j e^{z_j}} = -z_y + \log\sum_j e^{z_j}

Step through the three derivations below. Each line’s “why?” names the rule used, and each derivation is confirmed numerically in references/derivations.

Derivation stepper

Deriving by hand

Static view on small screens. Open this page on a laptop to interact.

Gaussian noise ⇒ mean squared errorstep 1 of 4
  1. p(yi∣xi)=12πσ2exp⁡ ⁣(−(yi−f(xi))22σ2)p(y_i\mid\mathbf{x}_i) = \frac{1}{\sqrt{2\pi\sigma^2}}\exp\!\left(-\frac{(y_i - f(\mathbf{x}_i))^2}{2\sigma^2}\right)
Bernoulli ⇒ binary cross-entropystep 1 of 3
  1. p(y∣x)=p y(1−p)1−y,p=σ(f(x)),  y∈{0,1}p(y\mid\mathbf{x}) = p^{\,y}(1-p)^{1-y}, \quad p = \sigma(f(\mathbf{x})), \; y\in\{0,1\}
Gaussian prior ⇒ L2 regularization (MAP)step 1 of 4
  1. p(w∣D)∝p(D∣w) p(w),p(w)=N(0,τ2I)p(\mathbf{w}\mid\mathcal{D}) \propto p(\mathcal{D}\mid\mathbf{w})\,p(\mathbf{w}), \qquad p(\mathbf{w}) = \mathcal{N}(\mathbf{0}, \tau^2\mathbf{I})
Three derivations revealed one line at a time: a Gaussian likelihood gives the mean squared error, a Bernoulli likelihood gives binary cross-entropy, and a Gaussian prior on the weights gives ridge regression. Keyboard: Tab to Next and Why buttons; Enter reveals the next line.

Write each loss from its likelihood, with the numerical care each needs, and solve for MAP weights. The tests feed in logits of 10⁴ and probabilities of exactly 0 and 1.

Losses from likelihoods stubs/m2_4_losses.py

Loading starter file…

Prefer your own editor? Edit exercises/stubs/m2_4_losses.py and run mlc check 2.4. The tests are identical; import the results on the progress page.

Hints · log_softmax 0/3 revealed
Hints · softmax_ce_and_grad 0/3 revealed
Hints · map_ridge 0/3 revealed

Locally: mlc check 2.4.

1. Your targets are counts (0, 1, 2, …) and you model them as Poisson with rate e^{f(x)}. What do you minimise?
2. Maximum likelihood with Gaussian noise of fixed variance is equivalent to minimising…
3. A Gaussian prior with variance τ² on the weights, and noise variance σ². The ridge strength λ is…
4. Why pass logits rather than probabilities to a cross-entropy loss?
5. What is the gradient of the mean softmax cross-entropy with respect to the logits of one example?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.