📝 Chapter 10: NLP — Quick-Revision Notes
A compact cheatsheet covering every major topic in the Natural Language Processing chapter. Use this for last-minute revision before GATE DA.
1. Text Preprocessing
| Technique | Purpose | Key Detail |
|---|---|---|
| Tokenization | Split text into units | Word-level, sub-word (BPE, WordPiece), character-level |
| Lowercasing | Normalize case | Reduces vocabulary; may lose proper noun info |
| Stop-word removal | Remove frequent words | “the”, “is”, “and” — task-dependent |
| Stemming | Strip suffixes | Porter Stemmer: “running” → “run”; crude, fast |
| Lemmatization | Dictionary lookup | “better” → “good”; needs POS; slower, accurate |
Bag of Words (BoW): Document = vector of word counts. Ignores word order. Sparse & high-dimensional.
TF-IDF: TF-IDF(t, d) = TF(t, d) × log(N / DF(t)). Upweights rare, informative terms.
N-grams: Contiguous sequences of n tokens. Count of n-grams in string of length L = L − n + 1.
2. Word Embeddings
| Model | Method | Key Property |
|---|---|---|
| Word2Vec CBOW | Predict center word from context window | Faster training; better for frequent words |
| Word2Vec Skip-gram | Predict context words from center word | Better for rare words; slower |
| GloVe | Factorize word co-occurrence matrix | Global statistics; good analogies |
Key formulas:
- Cosine similarity: cos(θ) = (A · B) / (‖A‖ × ‖B‖)
- Vector arithmetic: vec(“king”) − vec(“man”) + vec(“woman”) ≈ vec(“queen”)
- Negative sampling loss: log σ(vw′ · vc) + ∑k log σ(−vwk′ · vc)
Limitation: Static embeddings — one vector per word regardless of context (“bank” = same vector in all sentences).
3. Language Models
Chain rule: P(w1...wn) = ∏i=1n P(wi | w1...wi−1)
N-gram approximation (Markov): P(wi | w1...wi−1) ≈ P(wi | wi−n+1...wi−1)
| Model | Formula | Notes |
|---|---|---|
| Unigram | P(w) = count(w) / N | No context |
| Bigram | P(wi|wi−1) = C(wi−1,wi) / C(wi−1) | 1 word context |
| Trigram | P(wi|wi−2,wi−1) = C(wi−2,wi−1,wi) / C(wi−2,wi−1) | 2 word context |
Perplexity: PP(W) = P(w1...wN)−1/N — lower is better.
Smoothing techniques:
- Laplace (add-1): P(wi|wi−1) = (C(wi−1,wi) + 1) / (C(wi−1) + V)
- Kneser-Ney: Discounts counts by fixed d; redistributes using continuation probability
4. Transformer Architecture
Self-Attention: Attention(Q, K, V) = softmax(QKT / √dk) · V
Multi-Head Attention: Concatenate h attention heads, then project: MultiHead = Concat(head1, ..., headh) · WO
Transformer block: Multi-Head Attention → Add & LayerNorm → FFN → Add & LayerNorm
FFN: FFN(x) = max(0, xW1 + b1)W2 + b2
Positional Encoding (sinusoidal):
- PE(pos, 2i) = sin(pos / 100002i/d)
- PE(pos, 2i+1) = cos(pos / 100002i/d)
5. GPT vs BERT at a Glance
| GPT (Decoder) | BERT (Encoder) | |
|---|---|---|
| Direction | Left-to-right (causal mask) | Bidirectional (full attention) |
| Pretraining | Next-token prediction | MLM (15% masked) + NSP |
| Strength | Text generation | Text understanding / classification |
| Tokenizer | BPE | WordPiece |
| Usage mode | Prompting (zero/few-shot) | Fine-tuning with task head |
6. BERT Variants Quick Reference
| Variant | Key Change |
|---|---|
| RoBERTa | No NSP; dynamic masking; more data & longer training |
| ALBERT | Cross-layer weight sharing; factorized embeddings; SOP replaces NSP |
| DistilBERT | 6-layer student via knowledge distillation; 40% smaller, 97% performance |
| ELECTRA | Replaced-token detection (generator + discriminator); more sample-efficient |
7. Large Language Model Concepts
- Scaling laws: Loss ∝ N−α (Kaplan); Chinchilla: tokens ≈ 20 × parameters
- Emergent abilities: Capabilities that appear only at sufficient scale (e.g., CoT reasoning)
- In-Context Learning (ICL): Learning from examples in the prompt without weight updates
- RLHF: SFT → Train reward model → PPO optimization
- LoRA: Low-Rank Adaptation — freeze model; add small trainable matrices
8. Prompt Engineering Cheatsheet
| Technique | When to Use |
|---|---|
| Zero-shot | Simple, well-defined tasks with capable models |
| Few-shot (k=3–8) | Need specific output format or domain adaptation |
| Chain-of-Thought | Math, logic, multi-step reasoning |
| Self-Consistency | High-stakes reasoning; majority vote over N samples |
| ReAct | Tasks needing external tools (search, calculate) |
| RAG | Knowledge-intensive QA; reduce hallucination |
9. Formula Quick Reference
| Concept | Formula |
|---|---|
| TF-IDF | TF(t,d) × log(N / DF(t)) |
| N-gram count | L − n + 1 |
| Cosine similarity | (A · B) / (‖A‖ · ‖B‖) |
| Bigram probability | C(wi−1, wi) / C(wi−1) |
| Perplexity | P(W)−1/N |
| Self-Attention | softmax(QKT / √dk) · V |
| Laplace smoothing | (C(wi−1,wi)+1) / (C(wi−1)+V) |
10. GATE DA Quick Tips
- ⚡ TF-IDF and BoW are high-relevance topics for GATE DA — practice numerical problems.
- ⚡ Know the bigram/trigram probability formula cold — expect plug-and-compute questions.
- ⚡ Perplexity: lower = better. If PP = V (vocabulary size), the model is random.
- ⚡ Stemming is rule-based (faster, cruder); lemmatization is dictionary-based (slower, accurate).
- ⚡ Self-attention complexity is O(n² · d) where n = sequence length, d = dimension.
- ⚡ BERT = encoder (bidirectional, understanding); GPT = decoder (left-to-right, generation).
- ⚡ For GATE: focus on TF-IDF calculations, n-gram probabilities, perplexity, and BoW representations.