LABS · 05  /  EMBEDDINGS

The Meaning Map.

Turn every word into a point in space, arranged so that closeness means similar meaning. Click a word to find its neighbours, then do the impossible-sounding math: king − man + woman lands right on top of queen.

THE GIST · 20 SECONDS

An embedding is a word as a vector. Trained on context, words used alike end up pointing the same way. Similarity is the cosine of the angle between two vectors — 1 = same direction, 0 = unrelated. Because concepts become consistent directions, you can add and subtract meaning: king − man + woman ≈ queen. Geometry becomes semantics.

  • Embeddinga word as a vector
  • Cosineangle = similarity
  • Neighbourclosest by cosine
  • Analogyadd & subtract meaning

Click any word to see its nearest neighbours — or hit an analogy button on the right and watch the arithmetic land.

UNDER THE HOOD

What you just played, written down

Meaning becomes direction. Once every word is a vector, similarity is an angle and analogies are arithmetic — the two ideas that power semantic search and RAG.

How embeddings work — four ideas

  1. A word is a vector. Each word maps to a list of numbers, learned so words in similar contexts get similar vectors.
  2. Similarity is cosine. Compare two words by the angle between their vectors, not their distance — direction carries the meaning.
  3. Neighbours are meaning. The nearest vectors to a word are its closest relatives: cat → kitten, dog; tokyo → japan.
  4. Analogies are arithmetic. Concepts become consistent directions, so king − man + woman shifts along the gender axis and lands on queen.
WHY COSINE, NOT DISTANCE?

A word's vector can be long or short for reasons unrelated to meaning (frequency, magnitude). Cosine ignores length and compares only direction, so "cat" and "kitten" score high even if their raw magnitudes differ.

The whole thing in code

import numpy as np

def cosine(a, b):
    return a @ b / (np.linalg.norm(a) * np.linalg.norm(b))

def nearest(word, embeds, k=3):
    v = embeds[word]
    scored = [(w, cosine(v, u)) for w, u in embeds.items()
              if w != word]
    return sorted(scored, key=lambda t: -t[1])[:k]

def analogy(a, b, c, embeds):        # a - b + c
    target = embeds[a] - embeds[b] + embeds[c]
    scored = [(w, cosine(target, u)) for w, u in embeds.items()
              if w not in (a, b, c)]
    return max(scored, key=lambda t: t[1])   # → queen
COSINE−1 … 11 = identical direction
REAL DIMS100s–1000s2D here is a projection
THE 2D MAP IS A SHADOW

Real embeddings live in hundreds of dimensions. Tools like PCA or t-SNE flatten them to 2D so you can see the clusters — but the neighbours and analogies here are computed on the full vectors, not the flattened dots.

⚠ They inherit bias

Because embeddings learn from human text, they absorb its biases — analogies can surface stereotypes. Auditing and debiasing embeddings is an active area of fairness research.

↔ Static vs contextual

word2vec/GloVe give each word one fixed vector. Modern contextual embeddings (from Transformers) give a word a different vector per sentence, so "bank" differs by context.

★ Where it's used

Semantic search & RAG, recommendations, clustering, classification — and the very first layer of every Transformer, which embeds its input tokens.

QUICK CHECK

Did it stick?

FAQ

Embeddings, answered

What is a word embedding?

A word represented as a vector of numbers, learned so words used in similar contexts get similar vectors. Closeness in the space means closeness in meaning.

What is cosine similarity?

The angle between two vectors — dot product over the product of magnitudes. It runs from 1 (same direction) through 0 (unrelated) to −1 (opposite), and ignores vector length.

How does king − man + woman ≈ queen work?

Concepts become consistent directions. The man→woman direction is a gender shift; adding it to king lands nearest to queen. The analogy is literal vector arithmetic.

Why show them in 2D?

Real embeddings have hundreds of dimensions. PCA/t-SNE project them to 2D so clusters are visible; the math here still uses the full vectors.

Where are embeddings used?

Semantic search and RAG, recommendations, clustering, classification, and as the first layer of every Transformer.

RESULT

Next → rank a whole document store by cosine to a query embedding and you've built the retrieval half of RAG.

Finished this one? 0 / 12 Labs done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Labs