Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 4 — Word Embeddings: Word2Vec

Lesson 32 of 39 in the free Natural Language Processing notes on Siksha Sarovar, written by Rohit Jangra.

Word Embeddings — Word2Vec

Recall from Unit 3 that Bag-of-Words / TF-IDF treats every word as an independent dimension — "good" and "great" share no similarity in that representation, even though they mean almost the same thing. Word embeddings solve this by representing each word as a dense, low-dimensional vector (typically 100–300 dimensions) where semantically similar words end up close together in vector space.

The Distributional Hypothesis

Word2Vec is built on a core linguistic idea: "a word is characterized by the company it keeps" (J.R. Firth) — words that appear in similar contexts tend to have similar meanings.

"The cat sat on the mat"
"The dog sat on the mat"
-> "cat" and "dog" appear in the same context -> Word2Vec learns similar vectors for them

Two Word2Vec Architectures

ArchitecturePredictsBest for
CBOW (Continuous Bag of Words)The target word, given its surrounding context wordsFaster training, works well with frequent words
Skip-gramThe surrounding context words, given the target wordWorks better with rare words, small datasets

Training Word2Vec with Gensim

from gensim.models import Word2Vec

sentences = [
    ["the", "cat", "sat", "on", "the", "mat"],
    ["the", "dog", "sat", "on", "the", "mat"],
    ["cats", "and", "dogs", "are", "great", "pets"],
    ["the", "king", "loves", "the", "queen"],
    ["the", "queen", "loves", "the", "king"],
]

model = Word2Vec(sentences, vector_size=50, window=2, min_count=1, sg=1)  # sg=1 -> skip-gram

vector = model.wv["cat"]
print(vector.shape)   # (50,)  -- a dense 50-dimensional vector

similar = model.wv.most_similar("cat", topn=3)
print(similar)
# [('dog', 0.98...), ('mat', 0.75...), ('sat', 0.60...)]  (scores illustrative on tiny toy corpus)

The Famous Vector Arithmetic Property

Word2Vec embeddings capture analogies through simple vector arithmetic — this only works well on models trained on very large corpora (e.g. Google News, 100 billion+ words):

# king - man + woman ≈ queen   (illustrative; requires a large pre-trained model)
result = model.wv.most_similar(positive=['king', 'woman'], negative=['man'], topn=1)
print(result)
# [('queen', 0.73)]

Using Pre-trained Word2Vec Embeddings

Training from scratch needs huge corpora; in practice, most projects load pre-trained embeddings:

import gensim.downloader as api

wv = api.load("word2vec-google-news-300")   # 300-dim vectors trained on ~100B words
print(wv.similarity("good", "great"))    # 0.72 (high -- semantically close)
print(wv.similarity("good", "bad"))      # 0.19 (low -- semantically distant, despite being frequent co-occurring antonyms)
print(wv.most_similar("python"))
# Mix of programming-related and snake-related words -- WSD-style ambiguity (Unit 2) still applies!

Word2Vec vs BoW/TF-IDF

BoW / TF-IDF (Unit 3)Word2Vec
DimensionalityHigh (= vocabulary size)Low (typically 100-300)
SparsitySparse (mostly zeros)Dense
Captures word orderNoPartially (via context window)
Captures semantic similarityNoYes
Out-of-vocabulary wordsSimply absent (0 count)Cannot be represented (fixed vocabulary) — solved by later methods like FastText and subword tokenization

Other Embedding Techniques (Brief Mention)

MethodKey Idea
GloVeLearns embeddings from global word co-occurrence statistics across the whole corpus
FastTextExtends Word2Vec using character n-grams — can represent out-of-vocabulary/misspelled words
Contextual embeddings (BERT, etc.)A word gets a different vector depending on its sentence context (unlike Word2Vec's one fixed vector per word) — covered next, as the foundation of Transformers

Word2Vec was a landmark 2013 breakthrough — but it still assigns one fixed vector per word, regardless of context (so "bank" the river and "bank" the financial institution get the same vector). Solving this limitation is exactly what motivates the Transformer architecture, covered in the next two lessons.