2.4 MLE, MAP and loss functions
1. Learn
Section titled “1. Learn”- Stanford CS229 main notes (Ng, Ma)§1.3 Probabilistic interpretation; chapter 3 Generalized linear models
- Understanding Deep Learning (Prince)chapter 5 Loss functions (pp. 56–76)
2. Bridge
Section titled “2. Bridge”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 . Then choose 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, .
The distribution you pick decides the loss:
- a Gaussian with mean and fixed variance gives squared error (plus a constant);
- a Bernoulli with probability gives binary cross-entropy;
- a categorical with probabilities 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 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 on the weights and maximise the posterior instead: . A zero-mean Gaussian prior adds , which is L2 regularization, with . 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 toward 0 has no justification.
3. Explore
Section titled “3. Explore”Step through the three derivations below. Each line’s “why?” names the rule used, and each derivation is confirmed numerically in references/derivations.
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.
stubs/m2_4_losses.pyLoading 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.
Locally: mlc check 2.4.
5. Check
Section titled “5. Check”- not yet: All tests pass (0/?)
- not yet: Checkpoint quiz ≥ 80%
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.