GGiriBioAI

/ module 04 · Foundations

Embeddings & Attention

After tokenizing, every token ID is swapped for a long list of numbers: its embedding. Words with similar meaning end up close together. Then attention lets each token look at the others and borrow the context it needs, which is how 'it' finds out it means 'the cat'.

By the end you can
  • Explain how embeddings turn meaning into distance and direction
  • Use cosine similarity and the king − man + woman example
  • Describe what one attention head does for a token like 'it'

Try this first: Click two dots on the map, then try a vector arithmetic button.

about 15 min hover ? for help

concept

Meaning becomes geometry

An embedding is a learned vector. During training, words used in similar contexts get pushed toward similar vectors, so distance and direction start to mean something. The famous example: the arrow from man to woman points the same way as king to queen.

Real models use hundreds or thousands of dimensions. The lab below squeezes a small vocabulary into two so you can see it.

embedding
A vector that captures meaning. Similar text → close vectors.
cosine similarity
How aligned two vectors are, from -1 (opposite) to 1 (same direction).
self-attention
Each token builds its new representation as a weighted mix of every other token.
Live lab · Embedding map
manwomanboygirlkingqueenprinceprincesscatdogkittenpuppyliontigerbluegreenyellowpurpleapplebananabreadriceDNAgeneproteincellenzyme
Compare two words
king vs queen
cosine
0.95
distance
2.00

Next click sets word A.

Vector arithmetic
king − man + woman ≈ queen
peopleanimalscoloursfoodbiology

mechanism

Attention: tokens talking to tokens

An embedding alone does not know its context: the vector for bank is the same in "river bank" and "bank account". Self-attention fixes this. Each token asks a question (query), every token offers a label (key), and the match scores, after a softmax, decide how much of each token's information (value) to mix in.

A model has many heads per layer and dozens of layers, and each head learns its own pattern. The three toy heads below are hand-written to mimic patterns researchers actually find.

Live lab · Attention heatmap
query ↓ key →Thecatsatonthematbecauseitwastired
The100
cat95
sat91
on87
the83
mat80
because77
it77
was72
tired69
Where does "it" look? (click a row to change)
Thecatsatonthematbecauseitwastired

Try: switch to Head 2 and read the "it" row. Then change "cat" to "dog" or replace "it" with "she". Turn off the causal mask to see what an encoder would allow.

attention head
One independent attention pattern. Models run many heads in parallel, each spotting different relations.
softmax
Turns raw scores into probabilities that sum to 1.

Check yourself

Tick each one you could explain to a friend. If you can't yet, scroll back up and replay the lab.

Finished exploring?
Progress is saved only in this browser.