📝 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

TechniquePurposeKey Detail
TokenizationSplit text into unitsWord-level, sub-word (BPE, WordPiece), character-level
LowercasingNormalize caseReduces vocabulary; may lose proper noun info
Stop-word removalRemove frequent words“the”, “is”, “and” — task-dependent
StemmingStrip suffixesPorter Stemmer: “running” → “run”; crude, fast
LemmatizationDictionary 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

ModelMethodKey Property
Word2Vec CBOWPredict center word from context windowFaster training; better for frequent words
Word2Vec Skip-gramPredict context words from center wordBetter for rare words; slower
GloVeFactorize word co-occurrence matrixGlobal 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)

ModelFormulaNotes
UnigramP(w) = count(w) / NNo context
BigramP(wi|wi−1) = C(wi−1,wi) / C(wi−1)1 word context
TrigramP(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)
DirectionLeft-to-right (causal mask)Bidirectional (full attention)
PretrainingNext-token predictionMLM (15% masked) + NSP
StrengthText generationText understanding / classification
TokenizerBPEWordPiece
Usage modePrompting (zero/few-shot)Fine-tuning with task head

6. BERT Variants Quick Reference

VariantKey Change
RoBERTaNo NSP; dynamic masking; more data & longer training
ALBERTCross-layer weight sharing; factorized embeddings; SOP replaces NSP
DistilBERT6-layer student via knowledge distillation; 40% smaller, 97% performance
ELECTRAReplaced-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

TechniqueWhen to Use
Zero-shotSimple, well-defined tasks with capable models
Few-shot (k=3–8)Need specific output format or domain adaptation
Chain-of-ThoughtMath, logic, multi-step reasoning
Self-ConsistencyHigh-stakes reasoning; majority vote over N samples
ReActTasks needing external tools (search, calculate)
RAGKnowledge-intensive QA; reduce hallucination

9. Formula Quick Reference

ConceptFormula
TF-IDFTF(t,d) × log(N / DF(t))
N-gram countL − n + 1
Cosine similarity(A · B) / (‖A‖ · ‖B‖)
Bigram probabilityC(wi−1, wi) / C(wi−1)
PerplexityP(W)−1/N
Self-Attentionsoftmax(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.