Math Foundations — Lecture 4
Argmax, combinatorics, greedy vs exhaustive, softmax sampling, beam search, Viterbi DP.
Argmax & scoring
Given a model that assigns score \(s(\mathbf{x},\mathbf{y})\) to each (input, output) pair, inference seeks:
When \(|\mathcal{Y}|\) is small (e.g. class labels), exhaustive enumeration is fine. When \(\mathcal{Y}\) is structured (sequences, trees), search is needed.
Combinatorial explosion in tagging
Sequence of \(n\) tokens, tag set \(T\) with \(|T|\) tags. Exhaustive: score all \(|T|^n\) sequences.
Grows exponentially in \(n\) → intractable for long sentences.
Greedy decoding
At each step \(t\), pick the best tag independently:
Total cost: \(O(n\,|T|)\) — linear in sequence length. Not globally optimal; early mistakes propagate.
Softmax & sampling strategies
Turn scores into probabilities, then sample:
Top-\(k\)
Restrict to the \(k\) highest-scoring tokens, re-normalise, then sample. Fixed set size regardless of distribution shape.
Top-\(p\) (nucleus)
Keep the smallest set of tokens whose cumulative probability \(\ge p\). Adapts to distribution: sharp → few tokens; flat → many.
Beam search
Maintain a set of \(K\) partial hypotheses (the "beam"). At each step:
- Extend each hypothesis by every possible next token.
- Score all extensions.
- Keep only the top-\(K\).
Complexity per step: \(O(K\,|V|)\). Total: \(O(T\,K\,|V|)\) where \(T\) is max length.
Log-prob & length normalisation
Scores are usually log-probabilities (sums instead of products). Shorter sequences have fewer negative terms → bias toward short outputs. Fix:
\(\alpha=1\) → average log-prob per token; \(\alpha=0\) → no normalisation.
Viterbi algorithm
For chain-structured models (e.g. HMM, CRF): score decomposes into per-step and transition terms.
Dynamic programming recurrence:
Complexity: \(O(n\,|T|^2)\) — polynomial, not exponential. Back-pointers recover the best path.