AI Run Lab

All chapters

Embeddings

An embedding turns an ID into a vector a neural network can work with. Its values belong to the model and can change during training.

Open this chapter with experiments and audio

What does an embedding look up?

Suppose an illustrative vocabulary contains “room”, “meters” and “square”. Assign each word a row in the table below. An input containing the IDs for “square” and “meters” retrieves those two rows, in that order. We chose three features so you can inspect the operation. A model learns much longer rows from its training objective.

IDTokenVector · illustrative
0room[0.3, 0.7, −0.4]
1meters[0.8, −0.2, 0.5]
2square[0.7, 0.2, −0.6]

With 10,000 tokens and 256 features per token the table contains 2,560,000 trainable numbers. Doubling the number of times a text repeats “room” leaves that parameter count unchanged. The same saved row is read each time.

How could useful coordinates emerge?

At the start of training the vectors can be random. The rest of the network uses them to predict target tokens. When a prediction receives a large loss, backpropagation passes a gradient into the rows that were used on input. A small update can make the same situation slightly easier to predict next time. Across many examples, tokens used in related ways may develop related representations.

An individual coordinate usually has no convenient human label. It is the joint arrangement of many values, together with the way later layers read them, that gives the representation its function.

Can you measure how two vectors line up?

Cosine similarity divides the dot product by the product of the two vector lengths. For nonzero vectors a result of 1 means the same direction, 0 means perpendicular directions and −1 means opposite directions. Multiplying one vector by a positive constant leaves this score unchanged. That makes cosine a measure of direction.

Keep A fixed and rotate B to 90 degrees. Then adjust its length at the same angle. Compare this with stretching B at zero degrees.

At a right angle, both the dot product and cosine are zero for any positive length. Along the same direction, cosine stays at 1 while the dot product scales with B.

from math import hypot

def cosine(a, b):
    return sum(x*y for x, y in zip(a, b)) / (hypot(*a) * hypot(*b))

print(cosine([1, 0], [2, 0]))
print(cosine([1, 0], [0, 3]))

Run it with Python 3. The two outputs are 1 and 0. Similarity in a real embedding space depends on the training task. A high score by itself cannot establish that two statements mean the same thing or that either is true.

Does “bank” always have one vector?

For the same token ID, the input lookup gives the same row. After attention layers have incorporated context, its hidden state can differ substantially between “sit on the bank of the river” and “deposit money at the bank”. A token embedding, a contextual token state and a single embedding representing a whole document serve different purposes.

If a sentence is twice as long, does the embedding table double?

No. The sequence needs more looked-up vectors and temporary activations. The table keeps the same vocabulary and width.

Source · PyTorch · Embedding

What makes “bank” a poor search query?

A lookup gives the token “bank” the same starting vector in “bank account” and “river bank”. After attention, the representation at that position can differ. A retrieval model goes one step further. It compresses each query or passage into a fixed-length vector so two texts can be scored cheaply, even when they contain different words.

In a two-coordinate example, take a unit query vector [1, 0]. Passage A has unit vector [0.8, 0.6] and B has [0.6, 0.8]. Their cosine scores are 0.8 and 0.6. Rotate the query to [0, 1] and B wins. The coordinates here were chosen to illustrate the arithmetic. A production encoder learns its coordinates from examples, including pairs that should come close and pairs that should separate.

Can the highest cosine score establish that a passage is up to date?

The score orders vectors by direction. An expired policy can match the query perfectly. The retrieval system needs a date or version check to distinguish an old policy from its replacement.

Vary the directions before inspecting retrieval →

Sources · Reimers & Gurevych, 2019