Lensa ML
Lensa ML

Word2Vec & Embeddings

Step 1 of 6

Distributional Hypothesis

A word is known by the company it keeps

Context window size2
"You shall know a word by the company it keeps" — Firth, 1957thekingsatonthegoldenthroneandthequeenwatchedwindow = ±2Context words for "king" (window=2):thesatonWords appearing in similar contexts get similar embeddings.
CENTER
king
WINDOW
±2
CONTEXT
3 words

Window ±2: 'the', 'sat', 'on' are context words. Since 'queen' also appears near 'throne' and 'sat', it will develop a similar embedding to 'king'.

Step 1 of 6: Distributional Hypothesis

A word is known by the company it keeps

Context window size2
"You shall know a word by the company it keeps" — Firth, 1957thekingsatonthegoldenthroneandthequeenwatchedwindow = ±2Context words for "king" (window=2):thesatonWords appearing in similar contexts get similar embeddings.
CENTER
king
WINDOW
±2
CONTEXT
3 words

Window ±2: 'the', 'sat', 'on' are context words. Since 'queen' also appears near 'throne' and 'sat', it will develop a similar embedding to 'king'.

Step 2 of 6: Skip-Gram Architecture

Predict context words from a center word

kingInput (center)thronecrownroyalqueenPredict context wordsW_embed (king) → hidden → W_context → P(context|king)
CENTER
king
CONTEXTS
4
OBJECTIVE
Skip-Gram

Skip-Gram: given "king", predict its context words [throne, crown, royal, queen]. The model learns that "king" and "throne" often co-occur. The embedding matrix W_embed is the learned representation — that's the word2vec output!

Step 3 of 6: Training Objective

Watch embeddings cluster as training progresses

Training step0
kingqueenmanwomanprinceprincessdogcatpuppykittencartruck2D embedding spaceroyaltygenderanimalvehicle
STEP
0
PROGRESS
0%

Before training: words are randomly positioned in the embedding space. No structure — 'king' and 'queen' are just as far apart as 'king' and 'dog'.

Step 4 of 6: Vector Arithmetic

king - man + woman ≈ queen

kingqueenmanwomanprinceprincessdogcatpuppykittencartruckkingmanwomanqueenking−man+woman
KING
(2, 2.5)
- MAN
(2.2, 0.8)
+ WOMAN
(1.2, 0.6)
≈ RESULT
queen

king−man+woman: the vector from "man" to "king" (pink dashed) captures a relationship. Adding that same offset to "woman" (teal dashed) lands near "queen". This works because Word2Vec encodes semantic relationships as geometric directions!

Step 5 of 6: Cosine Similarity

Measuring word relatedness by the angle between vectors

Query vector angle (°)30
woman (1.00)prince (0.99)man (0.98)princess king queen car truck puppy kitten cat dog query
#1 NEAREST
woman
SIM
0.998
#2
prince
#3
man

Rotating the query vector to 30° — nearest neighbor is "woman" (cosine sim = 0.998). Cosine similarity measures the angle between vectors, ignoring magnitude. Words in the same direction as the query are most similar.

Step 6 of 6: Beyond Word2Vec

From static vectors to contextual embeddings

Embedding dimensionality50
"bank" (river bank)"bank" (bank account)Word2Vec2013GloVe2014ELMo2018BERT2019GPT2020Static embeddings: one vector per word
DIMENSIONS
50
TYPE
Static
SEPARATION
0.00

Static embeddings (Word2Vec, GloVe): each word gets ONE vector regardless of context. 'Bank' (river) and 'bank' (money) share the same embedding — a fundamental limitation.