Suppose a dictionary assigns “workshop” the number 12 and “seminar” the number 13. Does their closeness mean the computer knows they are related? What if the dictionary assigns “banana” the number 14?
Before you begin: Know what a neural network takes as input. No vector algebra is required.
An ID names an item
Computers need a numerical representation of text. One step is to divide text into units called tokens and map each token to an ID. A token might be a word, part of a word, a punctuation mark, or another unit chosen by the tokenizer. We will examine tokenization more closely later.
A token ID acts like an entry number in a catalog. It identifies an item; arithmetic on IDs does not provide a meaningful measure of similarity. Renumbering the catalog should not change what the words mean.
An embedding adds a learned representation. It maps an item to a list of numbers, called a vector. Instead of looking at the distance between catalog numbers, a model works with the pattern across those coordinates.
Build a representation you can inspect
For a toy search engine, describe documents with two features: how much they discuss teaching and how much they discuss food. Give “workshop guide” the vector [3, 0], “class notes” [2, 0], and “lunch menu” [0, 3].
Those coordinates are designed by us. In a learned embedding, the coordinates usually do not have labels as tidy as “teaching” and “food.” Training adjusts them so that relationships useful for an objective can be expressed by their geometry.
Try the query vector [1, 0]. The first two documents point in the same direction as the query; the menu points elsewhere. Cosine similarity compares directions. It divides the dot product of two vectors by the product of their lengths. The dot product multiplies corresponding coordinates and adds the results.
For [1, 0] and [3, 0], the dot product is three and the lengths are one and three. Their cosine similarity is one. For [1, 0] and [0, 3], the dot product is zero, so similarity is zero. A zero vector has no direction, so cosine similarity needs a policy for that case.
Similar does not mean interchangeable
“Registrations are refundable” and “Registrations are not refundable” share a topic and much of their wording. They may be close in an embedding space while giving opposite answers to a learner's question. A search score indicates a relationship under a particular model; it does not certify truth, permission, or logical agreement.
Likewise, a vector near another vector is not a stored definition. Embeddings depend on the training data, objective, and model. Two unrelated embedding models generally produce different coordinate systems. You should not compare a query vector from one model with document vectors from another just because they have the same number of coordinates.
A word can change with its sentence
A simple embedding lookup gives a token the same starting vector wherever it appears. Later parts of a language model can update that representation using surrounding tokens. This creates a contextual representation. The token “bank” can contribute differently in a fishing story and a payment question.
This does not require a little dictionary definition to appear inside each coordinate. It means that the numerical state used by the model depends on the context. Keeping the distinction between initial lookup and later contextual processing will make attention easier to understand.
Try a similarity calculation
This complete Python 3 program implements cosine similarity for the toy vectors. The explicit error checks prevent silent comparisons of different dimensions or zero vectors.
# Runnable: Python 3, standard library.
import math
def cosine(a, b):
if len(a) != len(b) or not a:
raise ValueError('Vectors need equal, nonzero dimensions')
lengths = math.sqrt(sum(x*x for x in a))
lengths *= math.sqrt(sum(x*x for x in b))
if lengths == 0:
raise ValueError('A zero vector has no direction')
return sum(x*y for x, y in zip(a, b)) / lengths
assert cosine([1, 0], [3, 0]) == 1
assert cosine([1, 0], [0, 3]) == 0
print(round(cosine([1, 0], [1, 1]), 3))
Before running it, decide whether the last score should be above zero and below one. Then replace [1, 1] with [10, 10].
Explain what stays the same
Both vectors point in the same direction, so both scores are about 0.707. Cosine similarity ignores a positive rescaling of a vector. Other similarity measures may behave differently. Choose the metric recommended for the embedding model and evaluate retrieval on actual questions.
Next, we will ask how a model combines these representations across a sentence instead of interpreting each item alone.
Further reading
Google's embeddings module introduces representation learning. Sentence-BERT is a research example of learning sentence representations for similarity tasks.