Natural Language Processing

⚡ Key Takeaways

Natural Language Processing (NLP) is the computational study of human language, blending linguistics, statistics, and machine learning to enable machines to understand, generate, and manipulate text and speech. Key result in words: language can be modeled probabilistically as sequences of tokens, and modern neural architectures (especially the Transformer) learn distributed representations that capture syntax and semantics from raw data. Where it applies: machine translation, search, dialogue systems, information extraction, summarization, sentiment analysis. Who/when: roots in 1950s machine translation and Shannon's information theory; revolutionized by statistical methods (1990s) and deep learning / Transformers (2013–present).

💡 The Core Insight

Language is modeled as a probability distribution P(w1,w2,…,wn)P(w_1, w_2, \dots, w_n) over token sequences. By the chain rule of probability, this factorizes into a product of conditional next-token distributions. The entire enterprise of modern NLP reduces to estimating these conditionals well — from nn-gram counts to recurrent networks to self-attention — and then using the learned distribution either to score, generate, or extract structured information. The distributional hypothesis ("you shall know a word by the company it keeps," Firth 1957) provides the semantic justification: words appearing in similar contexts have similar meanings, so context-prediction objectives implicitly learn semantics.

Formal Statement

DefinitionLanguage Model

A language model is a probability distribution over sequences of tokens drawn from a finite vocabulary VV. Given a sequence w1:n=(w1,…,wn)w_{1:n} = (w_1, \dots, w_n) with each wi∈Vw_i \in V, the joint probability factorizes by the chain rule:

1
P(w1:n)=∏i=1nP(wi∣w1:i−1)\quad P(w_{1:n}) = \prod_{i=1}^{n} P(w_i \mid w_{1:i-1})

Derivation hint: Repeatedly apply the definition of conditional probability P(A,B)=P(A)P(B∣A)P(A,B)=P(A)P(B\mid A) along the sequence. Here w1:i−1w_{1:i-1} is the context (history), P(wi∣w1:i−1)P(w_i \mid w_{1:i-1}) is the next-token conditional, and the product runs left to right. The vocabulary VV may consist of words, characters, or subword units (e.g., Byte-Pair Encoding tokens).

Definitionn-gram Approximation

Under the Markov assumption of order n−1n-1, the full history is truncated to the previous n−1n-1 tokens:

2
P(wi∣w1:i−1)≈P(wi∣wi−n+1:i−1)\quad P(w_i \mid w_{1:i-1}) \approx P(w_i \mid w_{i-n+1:i-1})

Derivation hint: Assume conditional independence of wiw_i from all but the most recent n−1n-1 tokens; the maximum-likelihood estimate is the ratio of co-occurrence counts. The maximum-likelihood estimate for a bigram (n=2n=2) is

3
P^(wi∣wi−1)=C(wi−1,wi)C(wi−1)\quad \hat{P}(w_i \mid w_{i-1}) = \frac{C(w_{i-1}, w_i)}{C(w_{i-1})}

Derivation hint: Maximize the multinomial log-likelihood subject to ∑wiP(wi∣wi−1)=1\sum_{w_i} P(w_i\mid w_{i-1})=1; the Lagrangian yields normalized counts. where C(⋅)C(\cdot) denotes corpus counts.

DefinitionWord Embeddings

A word embedding is a map E:V→RdE: V \to \mathbb{R}^d assigning each token a dense vector. The skip-gram objective (word2vec) maximizes the log-probability of context words wi+jw_{i+j} given a center word wiw_i over a window of size mm:

4
L=1T∑i=1T∑−m≤j≤m, j≠0log⁡P(wi+j∣wi),P(wO∣wI)=exp⁡(uwO⊤vwI)∑w∈Vexp⁡(uw⊤vwI)\quad \mathcal{L} = \frac{1}{T}\sum_{i=1}^{T}\sum_{-m \le j \le m,\, j\neq 0} \log P(w_{i+j}\mid w_i),\quad P(w_O\mid w_I)=\frac{\exp(u_{w_O}^\top v_{w_I})}{\sum_{w\in V}\exp(u_{w}^\top v_{w_I})}

Derivation hint: Model context prediction as a softmax over the vocabulary using separate input (vv) and output (uu) embeddings; the softmax is intractable, motivating negative sampling or hierarchical softmax.

TheoremSelf-Attention as Weighted Information Retrieval

The Transformer's scaled dot-product attention computes, for query matrix Q∈Rn×dkQ\in\mathbb{R}^{n\times d_k}, key matrix K∈Rn×dkK\in\mathbb{R}^{n\times d_k}, and value matrix V∈Rn×dvV\in\mathbb{R}^{n\times d_v}:

5
Attention(Q,K,V)=softmax ⁣(QK⊤dk)V\quad \mathrm{Attention}(Q,K,V) = \mathrm{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V

Derivation hint: The softmax row-normalizes pairwise compatibility scores into a convex combination over value vectors; the dk\sqrt{d_k} divisor keeps the dot-product variance ≈1\approx 1 to avoid saturated softmax gradients. This allows every position to aggregate information from all other positions in O(n2d)O(n^2 d) time with O(1)O(1) sequential dependency, enabling full parallelization across the sequence — the key advantage over recurrent models.

Why It Holds / Derivation

ProofFrom Cross-Entropy to Perplexity

We show that minimizing the standard training loss (cross-entropy) is equivalent to maximizing data likelihood, and that perplexity is its natural exponential form.

Step 1. Training maximizes the log-likelihood of the corpus under the model parameters θ\theta:

6
θ∗=arg⁡max⁡θ∑i=1Nlog⁡Pθ(wi∣w1:i−1)\quad \theta^* = \arg\max_\theta \sum_{i=1}^{N} \log P_\theta(w_i \mid w_{1:i-1})

Derivation hint: Take the log of the chain-rule factorization in Equation [1] and apply it across the corpus.

Step 2. Equivalently, minimize the average negative log-likelihood, the cross-entropy HH:

7
H=−1N∑i=1Nlog⁡2Pθ(wi∣w1:i−1)\quad H = -\frac{1}{N}\sum_{i=1}^{N} \log_2 P_\theta(w_i \mid w_{1:i-1})

Derivation hint: Negate and normalize Equation [6]; this approximates the cross-entropy between the true data distribution and the model.

Step 3. Define perplexity as the exponentiated cross-entropy:

8
PP=2H=Pθ(w1:N)−1/N\quad \mathrm{PP} = 2^{H} = P_\theta(w_{1:N})^{-1/N}

Derivation hint: Substitute Equation [7] into 2H2^H and use 2log⁡2x=x2^{\log_2 x}=x; the geometric mean of inverse probabilities emerges.

Step 4. Interpretation: perplexity is the effective branching factor — the average number of equally likely next tokens. A perfect model on a deterministic sequence has PP=1\mathrm{PP}=1; a uniform model over ∣V∣|V| tokens has PP=∣V∣\mathrm{PP}=|V|. Lower perplexity means better predictive compression of the language. ■\blacksquare

∎
CS
Claude Shannon
1916–2001

Though an electrical engineer and mathematician rather than a linguist, Shannon laid NLP's quantitative foundation. In A Mathematical Theory of Communication (1948) and his 1951 paper Prediction and Entropy of Printed English, he treated English as a stochastic source, estimated its entropy via human prediction experiments (~1.1 bits/character), and introduced the very notions of entropy, redundancy, and nn-gram models of text. His framing of language as an information source emitting symbols with measurable uncertainty is the conceptual seed from which perplexity, cross-entropy training, and the entire probabilistic-language-modeling program grew.

Building Intuition

🧠 Intuition

Imagine reading a sentence and covering the last word: "I drove my car to the ___." You don't assign equal probability to every English word; "garage," "store," or "office" feel far more likely than "elephant." A language model formalizes precisely this intuition as a probability distribution. Early models (nn-grams) only peeked at the last few words — like predicting from a tiny window — and were brittle: unseen combinations got zero probability. Word embeddings then realized that "car" and "automobile" should share statistical strength because they appear in similar contexts, packing meaning into geometry where direction and distance encode semantic relationships. The Transformer's attention is best imagined as each word querying every other word and asking "how relevant are you to my meaning here?" — then blending in their information weighted by that relevance. "Bank" attends to "river" or "money" depending on context, resolving ambiguity dynamically. The deep stacking of these attention layers builds, layer by layer, from surface tokens to abstract meaning.

Figure
Transformer self-attention: each query attends over all keys/values to form a context-weighted output
Scaled Dot-Product Attention the river bank Linear Q, K, V QKᵀ / √dₖ softmax Σ wᵢ Vᵢ context vector attention weights for query "bank" the river bank

Worked Examples

ExampleBigram Probability and Perplexity

Given a tiny corpus (with sentence boundaries `` and ``): ` I am Sam Sam I am I do not like green eggs `

Task: Estimate P(am∣I)P(\text{am}\mid\text{I}) and the perplexity of the test sentence ` I am Sam `.

Step 1 — Counts. C(I)=3C(\text{I}) = 3 (positions: "I am", "I am", "I do"). C(I, am)=2C(\text{I, am}) = 2.

Step 2 — Bigram MLE (Equation [3]):

P^(am∣I)=C(I, am)C(I)=23≈0.667\hat P(\text{am}\mid\text{I}) = \frac{C(\text{I, am})}{C(\text{I})} = \frac{2}{3} \approx 0.667

Step 3 — Other needed bigrams (approximate from corpus): P(I∣⟨s⟩)=2/3P(\text{I}\mid\langle s\rangle)=2/3, P(Sam∣am)=1/2P(\text{Sam}\mid\text{am})=1/2, P(⟨/s⟩∣Sam)=1/2P(\langle/s\rangle\mid\text{Sam})=1/2.

Step 4 — Test sentence probability:

P=23⋅23⋅12⋅12=436=0.1111P = \tfrac{2}{3}\cdot\tfrac{2}{3}\cdot\tfrac{1}{2}\cdot\tfrac{1}{2} = \tfrac{4}{36}=0.1111

Step 5 — Perplexity over N=4N=4 transitions (Equation [8]):

PP=P−1/N=0.1111−1/4=(9)1/4≈1.732\mathrm{PP} = P^{-1/N} = 0.1111^{-1/4} = (9)^{1/4} \approx 1.732
P^(am∣I)=0.667,PP≈1.73 (branching factor, dimensionless)\boxed{\hat P(\text{am}\mid\text{I}) = 0.667,\qquad \mathrm{PP}\approx 1.73\ \text{(branching factor, dimensionless)}}
ExampleAdd-One (Laplace) Smoothing

Given: Same corpus, vocabulary size ∣V∣=8|V| = 8 (I, am, Sam, do, not, like, green, eggs). We need P(Sam∣am)P(\text{Sam}\mid\text{am}) but want to avoid zero probabilities for unseen bigrams.

Step 1 — Raw counts. C(am)=2C(\text{am})=2, C(am, Sam)=1C(\text{am, Sam})=1.

Step 2 — Laplace-smoothed estimate:

9
PLaplace(wi∣wi−1)=C(wi−1,wi)+1C(wi−1)+∣V∣\quad P_{\text{Laplace}}(w_i\mid w_{i-1}) = \frac{C(w_{i-1},w_i)+1}{C(w_{i-1})+|V|}

Derivation hint: Add a pseudocount of 1 to every possible bigram; equivalent to a uniform Dirichlet prior with concentration 1 on the multinomial.

Step 3 — Plug in:

PLaplace(Sam∣am)=1+12+8=210=0.20P_{\text{Laplace}}(\text{Sam}\mid\text{am}) = \frac{1+1}{2+8} = \frac{2}{10} = 0.20

Step 4 — Compare to unsmoothed 1/2=0.501/2 = 0.50. Smoothing redistributes probability mass to unseen events, lowering the spike on observed bigrams.

PLaplace(Sam∣am)=0.20 (dimensionless probability)\boxed{P_{\text{Laplace}}(\text{Sam}\mid\text{am}) = 0.20\ \text{(dimensionless probability)}}

Common Misconceptions

⚠️ Common mistake

"A language model understands meaning." A language model estimates P(wi∣context)P(w_i\mid \text{context}) — it is a statistical sequence predictor, not a grounded reasoner. High fluency does not imply truthfulness, comprehension, or symbolic reasoning. Large models exhibit emergent abilities, but they can also "hallucinate" plausible-sounding falsehoods because the training objective rewards likely continuations, not factual correctness. Distinguish fluency (well-formed output) from faithfulness (correctness relative to a source or the world).

⚠️ Common mistake

"More nn in nn-grams is always better." As nn grows, the number of possible contexts grows as ∣V∣n−1|V|^{n-1}, so most are never observed — the data sparsity problem. Without smoothing or backoff, large-nn models assign zero probability to most test sequences (infinite perplexity). The bias–variance trade-off applies: higher nn reduces bias but explodes variance. Neural models circumvent this by sharing parameters across contexts via continuous representations.

Counterexample

The softmax does not preserve relative attention under length. One might assume attention weights for a fixed query stay stable as context grows. Consider a query with uniform compatibility scores over nn keys: each weight is 1/n1/n. Adding more keys dilutes every existing weight, so a salient token's relative contribution can vanish in very long sequences — a known weakness ("attention dilution") that motivates sparse, local, or retrieval-augmented attention variants. Thus the convex-combination structure of Equation [5] does not guarantee scale-invariant focus.

Connections

connection

NLP sits at the intersection of several mathematical fields. The probabilistic backbone connects to Information Theory (entropy, cross-entropy, perplexity) and Bayesian Inference (smoothing as priors, latent-variable topic models). Sequence labeling relies on Hidden Markov Models and Conditional Random Fields. Word embeddings and attention are grounded in Linear Algebra (dot products, matrix factorization — word2vec implicitly factorizes a shifted PMI matrix). Training rests on Stochastic Gradient Descent and Backpropagation. Parsing and grammar formalisms link to Formal Language Theory and Context-Free Grammars. The Transformer's representational power connects to Universal Approximation and the broader theory of Deep Learning.

Real-World Applications

🔬 Where this appears

Historical Development

⏳ History
- 1948–1951 — Claude Shannon models English as a stochastic source, introduces nn-gram entropy estimation, founding probabilistic language modeling.
- 1954 — Georgetown–IBM experiment demonstrates Russian–English machine translation (60 sentences), igniting MT optimism (and later disappointment via the 1966 ALPAC report).
- 1957 — Chomsky's Syntactic Structures establishes generative grammar and formal language hierarchies, dominating symbolic NLP for decades; Firth articulates the distributional hypothesis.
- 1970s–1980s — Rule-based and symbolic systems (SHRDLU, expert systems); Hidden Markov Models adopted for speech recognition.
- 1990s — Statistical revolution: IBM alignment models for MT, statistical parsing, the rise of corpus-driven, probabilistic methods over hand-written rules.
- 2003 — Bengio et al. introduce the neural probabilistic language model, learning distributed word representations.
- 2013 — Mikolov et al. release word2vec; efficient embeddings make distributed semantics mainstream. GloVe follows in 2014.
- 2014–2015 — Sequence-to-sequence learning (Sutskever et al.) and the attention mechanism (Bahdanau et al.) transform neural MT.
- 2017–2019 — "Attention Is All You Need" (Vaswani et al., 2017) introduces the Transformer; BERT (2018) and GPT (2018) usher in the pretraining/fine-tuning paradigm and the era of large language models.

Subtleties & Limits

open-problem

Grounding, reasoning, and scaling limits remain open. Despite remarkable fluency, current models struggle with (1) factual grounding — distinguishing learned associations from truth, leading to hallucination; (2) compositional generalization — systematically combining known parts in novel ways (the systematicity critique from Fodor & Pylyshyn); (3) long-range and quadratic cost — self-attention's O(n2)O(n^2) scaling limits context length, spurring linear-attention and state-space alternatives; (4) interpretability — we lack a theory mapping the geometry of learned representations to formal semantic structure; and (5) data and energy efficiency — humans acquire language from orders of magnitude less data than LLMs. Whether scaling alone yields robust reasoning, or whether explicit symbolic structure, world models, or grounding in perception/action are necessary, is a central and contested open question.

See Also

Information Theory · Hidden Markov Models · Conditional Random Fields · Word Embeddings · Transformer Architectures · Large Language Models · Context-Free Grammars · Bayesian Inference · Deep Learning