3.5.11 · D2Sequence Models

Visual walkthrough — Word embeddings (Word2Vec, GloVe)

2,275 words10 min readBack to topic

This page rebuilds ONE result from nothing: the negative sampling training rule that Word2Vec's Skip-gram actually uses. The parent note gave you the final loss. Here we earn every symbol, one picture at a time, until that loss stops looking like magic and starts looking obvious.


Step 1 — A word is a labelled slot, nothing more

WHAT. Before meaning, a computer only knows identity. If our vocabulary has distinct words, we give each word one slot. "cat" might be slot 3; "dog" slot 7. We write a word as a column of numbers that is all except a single in that word's slot. This is One-hot encoding.

WHY. We need some number-form to start. One-hot is the most honest one: it claims nothing except "this is word number ". No word is closer to any other — every pair is equally far apart. That emptiness is exactly the problem embeddings will fix.

PICTURE. Look at the tall thin column in the figure: one amber , a stack of white s. Every word gets its own column, and geometrically they sit on perpendicular axes — no two share a direction.

Figure — Word embeddings (Word2Vec, GloVe)

Step 2 — Two lookup tables turn a slot into a location

WHAT. We introduce two matrices. Call them (the input/word table) and (the output/context table). Each has rows and columns, where is a small number like . Row of is the word's own vector ; row of is the word's context vector .

WHY. We want each word to live at a point in -dimensional space so that nearness can mean "similar meaning". Multiplying a one-hot vector by just selects one row — so is nothing but a lookup. We keep two vectors per word (one as "I am the centre word", one as "I am a neighbour") because a word playing centre and the same word playing neighbour should be allowed to behave differently; it makes the maths and the gradients cleaner.

PICTURE. The one-hot spike acts like a finger pointing at exactly one row of the grid ; that row slides out as the dense vector on the right.

Figure — Word embeddings (Word2Vec, GloVe)

Step 3 — The learning signal: real neighbours in the text

WHAT. Slide a small window across the corpus. At each position we have a centre word and its context words (the words within steps to the left and right, ). Each pair that actually occurs is a positive example.

WHY. Skip-gram's whole bet, from the parent note, is: a word is known by the company it keeps. If we can make each centre word good at predicting its true neighbours, its vector must absorb what those neighbours have in common — its meaning. So the raw material for learning is simply: which words really sat next to which.

PICTURE. The amber box is the centre word "sat"; the cyan boxes inside the window are its true context. Each arrow "sat → the", "sat → cat", "sat → on", "sat → mat" is one positive training pair.

Figure — Word embeddings (Word2Vec, GloVe)

Notice the payoff the parent mentioned: one centre word spawns several pairs (one per neighbour), so even a rare word gets multiple updates.


Step 4 — Scoring a pair with a dot product

WHAT. Given a centre vector and a candidate context vector , we measure how well they match with a dot product:

WHY this tool. We need one number saying "do these two vectors point the same way?" The dot product is the natural answer: it is large and positive when two vectors point together, near zero when perpendicular, and negative when opposed. It is the un-normalised cousin of Cosine similarity — same sign behaviour, without dividing by lengths. We use the raw dot product (not full cosine) because it is cheap and the network can absorb the scaling into the vectors themselves.

PICTURE. Two arrows from the origin: when the angle between them is small the projection (the amber drop-line) is long → big score; when they are at right angles the projection collapses to zero.

Figure — Word embeddings (Word2Vec, GloVe)

Step 5 — Squashing a score into a probability with the sigmoid

WHAT. A score can be any real number (from to ). We want a probability — a number in answering "is this a real pair?". The sigmoid does exactly this:

WHY the sigmoid and not softmax. Softmax (used in the parent's plain Skip-gram) needs the scores of all words in its denominator — brutally expensive. The trick of negative sampling is to stop asking "which of the 50,000 words is the neighbour?" and instead ask a much cheaper yes/no question about one pair at a time: "real or fake?" A yes/no question needs a squasher that maps one score to one probability — that is the sigmoid. It hands us for large positive , for large negative , and for "no idea".

PICTURE. The S-curve: horizontal axis is the score , vertical axis is probability. Watch it hug on the right, on the left, pass through at the origin.

Figure — Word embeddings (Word2Vec, GloVe)

Step 6 — Faking neighbours: negative sampling

WHAT. For each real pair we invent fake pairs by drawing random words from a noise distribution . These are almost certainly not real neighbours of .

WHY. The sigmoid only knows "real vs fake". If we only ever showed it real pairs, it could cheat by calling everything real (predict always). We must show it counter-examples so it learns to say "fake" too. Drawing them at random is cheap, and with around it is enough. The noise distribution is the smoothed unigram where is word 's raw frequency. The exponent pulls very common words down and lifts rare words up, so negatives aren't only "the", "of", "a".

PICTURE. Around the true arrow "sat → cat", four amber dashed arrows fly off to random words "banana", "of", "planet", "the" — the sampled negatives we will push away.

Figure — Word embeddings (Word2Vec, GloVe)

Step 7 — Assemble the objective, term by term

WHAT. We want, for each centre word :

  • the real neighbour to score high near ;
  • each fake neighbour to score low → its fake probability near .

Maximising both means maximising their product. Taking (turns a product into a sum and turns "maximise" into "minimise a loss") gives the objective the parent stated:

WHY this shape. is when the probability is and blows up toward as it drops to . So the loss is small exactly when we got both jobs right, and painful when we didn't. That is precisely what an optimiser wants to push downhill.

PICTURE. Two stacked bars: the real term shrinks its loss by raising toward the top; each fake term shrinks its loss by raising — i.e. driving its score down. Green = "pull together", amber = "push apart".

Figure — Word embeddings (Word2Vec, GloVe)

Step 8 — What one gradient step actually does to the arrows

WHAT. Backpropagation on this loss gives beautifully simple updates. Using , the gradient w.r.t. the centre vector is

WHY it's intuitive. For the real pair, is negative (since ), and gradient descent steps opposite the gradient — so we move toward : pull real neighbours together. For each fake pair, is positive, so we move away from : push fakes apart. The size of each nudge is the error : pairs already correct barely move, wrong ones move a lot.

PICTURE. The centre arrow "sat" slides a short green step toward "cat" and short amber steps away from the four random negatives — the physical meaning of one update.

Figure — Word embeddings (Word2Vec, GloVe)

The one-picture summary

Everything above compresses to a single tug-of-war around the centre word: green ropes pull the true neighbour in, amber ropes shove the random negatives out, and the sigmoid decides how hard to pull. Repeat over the whole corpus and the geometry that survives is meaning.

Figure — Word embeddings (Word2Vec, GloVe)
Recall Feynman retelling — say it back in plain words

We started with words as dumb labels — one in a long stack of s, every word equally far from every other. We handed each word two secret coordinates lists (a "me" vector and a "neighbour" vector). To score whether two words belong together, we point their arrows and take a dot product — long shadow means good match. We squashed that score through the S-curve so it reads as a probability: "is this a real neighbour, yes or no?" To stop the model from lazily shouting "yes" to everything, we invented fake neighbours by picking random words (leaning slightly toward rarer ones via the power). Then the loss is just: make real pairs read near 1, make fake pairs read near 0, and punishes us whenever we don't. One gradient step literally slides the centre word's arrow toward its true neighbour and away from the fakes, by an amount equal to how wrong we were. Do this millions of times and words that keep the same company drift into the same neighbourhood — that neighbourhood is the embedding space.

Recall Quick self-test

Why sigmoid instead of softmax here? ::: Softmax's denominator sums over all words (expensive); negative sampling replaces the "which word?" question with cheap yes/no "real or fake?" questions, each answered by one sigmoid. What does represent? ::: The probability the pair is fake, using the identity . Which direction does move for a real pair? ::: Toward the true context vector , because the factor is negative and descent steps opposite the gradient. Why exponent in the noise distribution? ::: It down-weights very frequent words and up-weights rare ones, so negatives are informative rather than all being "the/of/a". What breaks if ? ::: Nothing stops all vectors collapsing together — negatives are what prevent trivial collapse.

Related paths onward: Cosine similarity (how we read out the learned space), t-SNE (how we see it in 2-D), Transfer learning and Attention mechanism / BERT (what these static vectors grow into).