βˆ‘GateAndNet
PlannerSign Up
INTERMEDIATEFrequently Tested 4 min read 3 views

Lexical & Vector Semantics: TF-IDF, BM25, Word2Vec (Skip-Gram / CBOW) & GloVe

Sparse Bag-of-Words vs TF-IDF & Okapi BM25 saturation, Cosine Similarity invariance, Word2Vec CBOW vs Skip-Gram with Negative Sampling (SGNS), and vector analogy arithmetic.

1. Sparse Representations: BoW, TF-IDF & Okapi BM25

  • Bag-of-Words (BoW): Document vector in R∣V∣\mathbb{R}^{|V|}; loses word order, high-dimensional (∣V∣∼105|V| \sim 10^5), orthogonal one-hot word vectors (cos⁑(wi,wj)=0\cos(w_i, w_j) = 0 even for "hotel" and "motel"!).
  • TF-IDF: Multiplies local Term Frequency (often sublinear 1+log⁑10ft,d1 + \log_{10} f_{t,d}) by global Inverse Document Frequency log⁑(N/dft)\log(N / \text{df}_t) so rare discriminative terms outweigh ubiquitous words.
  • Okapi BM25 (Modern IR & RAG Lexical Baseline): Adds term-frequency saturation (k1β‰ˆ1.2k_1 \approx 1.2: seeing a query word 1010 times isn't 10Γ—10\times more relevant than 33 times) and document-length normalization (bβ‰ˆ0.75b \approx 0.75).

2. Dense Static Word Embeddings: Word2Vec & GloVe

Grounded in the Distributional Hypothesis ("You shall know a word by the company it keeps" β€” Firth, 1957), Word2Vec learns dense low-dimensional embeddings d∈[100,300]d \in [100, 300] via self-supervised sliding windows:

Model VariantPrediction TaskSpeed & Strengths
CBOW (Continuous Bag-of-Words)Predicts center word wtw_t from averaged surrounding window (wtβˆ’m,…,wt+m)(w_{t-m}, \dots, w_{t+m})Faster to train; slightly better on frequent words
Skip-GramPredicts each context word wt+jw_{t+j} (βˆ’m≀j≀m,jβ‰ 0-m \le j \le m, j \neq 0) given center word wtw_tSuperior representations for rare words & morphological analogies
FastTextRepresents each word as sum of character nn-grams (e.g. <wh, whe, her, ere, re>)Handles Out-of-Vocabulary (OOV) words & rich morphology!
GloVeFactorizes log global word-word co-occurrence matrix wiTw~j+bi+b~jβ‰ˆlog⁑Xijw_i^T \tilde{w}_j + b_i + \tilde{b}_j \approx \log X_{ij}Combines global matrix statistics + local context geometry
Common Exam Mistake

Static vs Contextual Embeddings (Polysemy Limitation): Word2Vec and GloVe assign one single static vector per word type β€” "river bank" and "savings bank" receive the exact same vector! Contextual encoders (ELMo, BERT, GPT) compute dynamic token vectors ht=f(w1,…,wn)h_t = f(w_1, \dots, w_n) conditioned on the entire sentence.

Key Formulas Summary
TF-IDF Term Weight & Cosine Similarity
tfidf⁑(t,d,D)=tf⁑(t,d)β‹…log⁑ ⁣(Ndf⁑(t)),cos⁑(ΞΈ)=uβ‹…vβˆ₯uβˆ₯2 βˆ₯vβˆ₯2\operatorname{tfidf}(t, d, D) = \operatorname{tf}(t, d) \cdot \log\!\left(\frac{N}{\operatorname{df}(t)}\right), \qquad \cos(\theta) = \frac{u \cdot v}{\|u\|_2 \, \|v\|_2}
Note: If a stopword appears in all N documents (df(t) = N), log(N/N) = log(1) = 0!
Word2Vec Skip-Gram Softmax vs Negative Sampling (SGNS) Objective
LSGNS=log⁑σ(ucTvw)+βˆ‘k=1KEnk∼Pn ⁣[log⁑σ(βˆ’unkTvw)]\mathcal{L}_{\text{SGNS}} = \log \sigma(u_c^T v_w) + \sum_{k=1}^{K} \mathbb{E}_{n_k \sim P_n}\!\big[\log \sigma(-u_{n_k}^T v_w)\big]
Note: Reduces per-step denominator cost from O(|V|) full softmax to O(K) with K = 5..20 negative samples!
#NLP#TF-IDF#BM25#Word2Vec#Skip-Gram#CBOW#Cosine Similarity#Embeddings

Practice Questions on This Chapter (2)

Q1GATE DA NLPMCQEASY
+1M β€’ -0.33

In a collection of N=10,000N = 10,000 documents, the word "the" appears at least once in every single document (df=10,000\text{df} = 10,000). Using standard idf⁑(t)=log⁑10(N/dft)\operatorname{idf}(t) = \log_{10}(N / \text{df}_t) without smoothing, what is the TF-IDF weight of "the" in document dd?

⚑ Click an option above to verify instantly
Q2GATE DA NLPMCQMEDIUM
+2M β€’ -0.67

Which embedding architecture represents each word as a bag of character nn-grams, enabling it to generate meaningful vectors for unseen Out-of-Vocabulary (OOV) words at inference time?

⚑ Click an option above to verify instantly
Previous TopicN-Gram Language Models, Laplace/Kneser-Ney Smoothing & PerplexityNext Topic Sequence Labeling (HMM Viterbi) & Transformers (Self-Attention, BERT vs GPT)