Math Foundations — Lecture 1
Notation, sets, vectors, TF–IDF, BM25, cosine, SVD, softmax, cross-entropy.
Sets & notation
One-hot & bag-of-words vectors
One-hot: word \(w\) at index \(j\) in \(V\) → vector \(\mathbf{e}_w\in\{0,1\}^{|V|}\) with a single 1 at position \(j\).
BoW document vector (count variant): \(\mathbf{d}[j]=\text{count}(w_j,d)\). Binary variant uses OR instead of sum.
Orthogonality: \(\mathbf{e}_a\cdot\mathbf{e}_b=0\) for \(a\neq b\) — synonyms have zero overlap.
TF–IDF
Raw counts over-weight frequent but uninformative terms (e.g. the). TF–IDF re-weights:
Term frequency (log-scaled)
Inverse document frequency
If \(t\) appears in every document, \(\mathrm{idf}_t=\log_{10}1=0\) — term has no discriminative power.
Combined
BM25 (Okapi)
Information retrieval scoring; same spirit as TF–IDF with saturation and length normalisation.
Cosine similarity
Dot product favours longer vectors; cosine normalises by magnitude:
Range \([-1,1]\); 1 = same direction; 0 = orthogonal; −1 = opposite.
Analogy arithmetic
Check: \(\mathbf{v}_{w_1}-\mathbf{v}_{w_2}+\mathbf{v}_{w_3}\approx\mathbf{v}_{w_4}\) via nearest-neighbour in cosine. Example: Paris − France + Italy ≈ Rome.
SVD for distributional vectors
Co-occurrence matrix \(M\in\mathbb{R}^{|V|\times|V|}\) (or \(|V|\times C\) for context features). Truncated SVD:
Keep top-\(k\) singular values → each word gets a dense \(k\)-dimensional vector (row of \(U_k\Sigma_k\)). Captures latent semantic structure (Schütze 1993, LSA).
Softmax & cross-entropy (preview)
Maps raw scores (logits) \(z_1,\dots,z_C\) to a probability distribution over \(C\) classes:
Always positive; sums to 1. Sensitive to scale of \(z\).
Cross-entropy loss
Measures distance between predicted distribution \(\hat{\mathbf{p}}\) and one-hot target \(\mathbf{y}\):
where \(y^*\) is the true class. Minimising CE = maximising log-probability of the correct label.
Dense embeddings
Map each word to a learned vector \(\mathbf{e}_w\in\mathbb{R}^d\) with \(d\ll|V|\). Equivalent to a lookup in an embedding matrix \(E\in\mathbb{R}^{|V|\times d}\):
Properties (animacy, part-of-speech, etc.) encoded as directions, axes, or regions in embedding space — not always a single dimension.