3.5.11 · D3Sequence Models

Worked examples — Word embeddings (Word2Vec, GloVe)

2,876 words13 min readBack to topic

This page takes the machinery from Word embeddings (Word2Vec, GloVe) and runs the numbers — by hand, on tiny vocabularies you can hold in your head. The parent gave you the formulas. Here we plug in real numbers and check every corner case: the friendly ones, the degenerate ones (zeros, ties), and the exam-trap ones.

Before we start, one promise: every symbol below is re-explained the first time it appears, and every number is verified at the bottom.


What the symbols mean (built from zero)

We will need three helper functions repeatedly:

Two more formulas, stated now so no example uses them cold:


The scenario matrix

Every problem this topic can throw at you falls into one of these cells. The examples below are tagged with the cell(s) they cover.

Cell Case class The tricky bit
A CBOW forward pass, ordinary inputs averaging context, softmax normalises
B Skip-gram forward pass one target → many context probs, no averaging
C Negative-sampling loss, real pair near 1 → loss near 0
D Negative-sampling loss, fake pair uses ; sign flip matters
E Degenerate: zero score () , uniform softmax
F Degenerate: tie / identical arrows softmax gives equal probs; cosine
G GloVe target for a pair , and the trap
H GloVe weighting across all 3 regimes , ,
I Limiting behaviour huge/tiny scores → softmax saturates
J Word-analogy word problem king − man + woman, cosine ranking
K Exam twist negative-sampling gradient sign

Nine worked examples below hit all eleven cells.


Example 1 — CBOW forward pass (Cell A)

Forecast: Guess — will "sat" get more or less than a uniform ? Jot it down.

  1. Average the context arrows to get the hidden vector . Why this step? CBOW's hidden layer is literally the average of the context embeddings — it blends the surrounding words into one "context mood" arrow. See the figure below — the two chalk-blue context arrows and their yellow average.

Figure — Word embeddings (Word2Vec, GloVe)
Figure 1 — CBOW hidden vector (yellow) is the average of the two context arrows (blue). It lands exactly midway between and .

  1. Score every vocabulary word with . \text{cat}&: (1,1)^T(0.5,0.5)=0.5+0.5=1.0\\ \text{sat}&: (2,0)^T(0.5,0.5)=1.0+0=1.0\\ \text{on}&: (0,2)^T(0.5,0.5)=0+1.0=1.0\\ \text{mat}&: (1,-1)^T(0.5,0.5)=0.5-0.5=0.0 \end{aligned}$$ *Why this step?* The dot product asks "how aligned is each output arrow with the context mood?" Higher = more likely to be the missing word.
  2. Softmax the scores .

Verify: All four probabilities must sum to 1: . ✓ And "sat" beats uniform because its score (1.0) is above the low scorer "mat" (0.0). Did your forecast agree?


Example 2 — Skip-gram forward pass (Cell B)

Forecast: Skip-gram uses no averaging — the hidden vector is just . Which context word wins, cat or mat?

  1. Hidden vector = the target arrow, untouched: . Why? Skip-gram feeds one word in, so there is nothing to average — this is the whole architectural difference from CBOW.

  2. Score all words : Why this step? Just like CBOW, the dot product measures how aligned each output arrow is with the target's hidden vector — "how likely is to appear near sat?" Higher score = more likely a context word.

  3. Softmax, scores : denominator . Why softmax? We need the scores turned into a probability distribution over the whole vocabulary (they must sum to 1) so we can read off "probability that this word is a context word." Softmax is the tool that normalises raw scores into probabilities.

Verify: cat (0.319) mat (0.043), and cat/sat/on tie at 0.319 each. Sum check: . ✓ Cat wins, as forecast.


Example 3 — Negative sampling, the real pair (Cell C)

Forecast: A big positive score means the model is confident this real pair is real. Will the loss be near 0 or near ?

  1. Sigmoid of the score: . Why ? Negative sampling reframes training as "is this pair real? — a yes/no question." turns the raw score into .

Figure — Word embeddings (Word2Vec, GloVe)
Figure 2 — the sigmoid curve. The blue dot marks the real pair (, ); the pink dot marks a fake pair scored through (Example 4); the yellow crosshair marks (Example 5).

  1. Loss: .

Verify: is close to 1, so the penalty is tiny — exactly what "the model got it right" should look like. ✓


Example 4 — Negative sampling, a fake pair (Cell D)

Forecast: The score is positive (+2) — but this pair is supposed to be fake. Note the minus sign inside . Will the loss be big (model is fooled) or small?

  1. Flip the sign, then sigmoid: . Why the minus? . We want fake pairs judged fake, so we score .

  2. Loss: .

Verify: Because the model gave this fake pair a high alignment (+2), it looks too real, so is low and the loss is large — a strong correcting push. That is the desired behaviour: the gradient will pull these arrows apart (this is the Backpropagation signal). ✓ Contrast with Example 3's tiny loss.


Example 5 — Degenerate: zero score (Cell E)

Forecast: A zero score means "no opinion — perpendicular arrows, no alignment." What probability does no opinion give?

  1. Sigmoid at 0: . Why it matters: a brand-new, untrained embedding often has near-zero dot products, so the model starts at "50/50 — I don't know." Training moves it off 0.5.

  2. Uniform softmax: if all four scores are 0, each probability is . Why? Equal scores → equal probabilities. This is the maximum-uncertainty state.

Verify: exactly, and . ✓ Perpendicular arrows carry zero preference — the geometric meaning of a zero dot product.


Example 6 — Degenerate: identical arrows / tie (Cell F)

Forecast: Identical arrows are the "perfect synonym" limit. Guess the cosine and the split.

  1. Cosine similarity = . Here , so numerator and denominator . Why cosine, not dot product, for similarity? Cosine ignores arrow length and compares direction only — the right tool when we care about "same meaning" regardless of how frequent (hence how long) a word's vector is.

  2. Softmax tie: equal scores over two words → each.

Verify: cosine (maximum possible — arrows point exactly the same way) and . ✓ Perfect synonyms sit on top of each other; t-SNE would draw them as one dot.


Example 7 — GloVe target, weighting, and the log(0) trap (Cells G, H)

Forecast: Which pairs does GloVe actually train on, and which one gets the flat cap of 1? Guess before computing.

  1. Target for ice–solid: the dot-plus-biases should reach . Why log? From the parent's derivation, embedding dot products should equal of the co-occurrence count — log because meaning-ratios became a homomorphism , whose inverse is .

  2. Weight , regime : . Why weight at all? Frequent pairs would otherwise dominate the loss; trims rare pairs while still trusting the solid counts.

  3. Weight , regime : since , exactly — the cap. Why cap? Without it, a hyper-frequent pair like ice–water would drown out everything; the flat 1 stops any single pair from dominating.

  4. ice–fashion: , so — undefined, would blow up the loss. But . The weight multiplies the whole squared-error term by zero, so this never-seen pair contributes nothing.

Verify: , , (cap regime), and — the zero weight is precisely what defuses the trap. ✓


Example 8 — Limiting behaviour: softmax saturation (Cell I)

Forecast: A huge lead in score — does softmax give "very likely" or "certainly"? Where does it stop?

  1. At : . Why this step? To see how fast softmax saturates — a lead of 10 already means 99.995%.

  2. At : dwarfs , so but never exactly reaches it (there is always the ). The limit is . Why it matters: huge scores cause numerical overflow in code and vanishing gradients (Backpropagation gets ~0 signal), which is why real implementations subtract the max score before exponentiating.

Verify: and the limit is exactly (approached, never hit). ✓


Example 9 — Word-analogy word problem + the exam twist (Cells J, K)

Forecast: Everyone "knows" the answer is queen — but prove it with cosine, and beat apple.

  1. Vector arithmetic: subtract man, add woman. Why subtract man, add woman? Subtracting removes the "male" direction; adding restores a "female" one, keeping the shared "royalty" part. This is the compositionality the parent promised.

Figure — Word embeddings (Word2Vec, GloVe)
Figure 3 — the analogy target (dashed pink) equals king − man + woman = (2,3). It lands exactly on queen (yellow) and points nearly opposite to apple.

  1. Cosine to queen . Since is identical to queen, by the Example 6 logic (numerator , denominator ).

  2. Cosine to apple : Why cosine, again? We rank by direction to the target; queen () crushes apple (, pointing nearly opposite), so the analogy returns queen.

  3. (K-twist) gradient sign. The real-pair loss is with score . Differentiate: using , Plug : , so . What the sign means: a negative gradient of the loss means gradient descent increases — it pushes the real pair's score up, pulling king/queen-style arrows together. Exactly right for a real pair.

Verify: ; ; ; queen wins. Gradient ⇒ score rises. ✓


Recall

Recall Why does CBOW average but Skip-gram not?

CBOW blends many context words into one hidden vector, so it averages. Skip-gram feeds one target word, so there is nothing to average — its hidden vector is the raw input embedding.

Recall What defuses GloVe's

problem? The weighting function multiplies the never-co-occurring pair's error by zero, so it never enters the loss.

Recall Real pair vs fake pair in negative sampling — what's the sign trick?

Real pair uses ; fake pair uses . The minus sign converts into .

Ratio of GloVe co-occurrence probabilities near 1 means
the two words are unrelated to that context word
equals
— perpendicular arrows, zero dot product, maximum uncertainty
Cosine similarity of two identical non-zero vectors