The problem of representing language
8 min read
A neural network cannot be handed the word hello. At bottom, a neural network is a mathematical function that operates on vectors of real numbers. Its computations assume that the inputs belong to a space where notions like "distance", "direction" and "closeness" are well defined, and text, on its own, does not live in a space like that. Between apple and cat there is no intermediate point, no direction that leads from one word to the other, no step halfway between the two.
And yet language models work. That is the challenge in one line: to close the gap between a text and a function over real numbers. Solving it is what this block is for, because the decisions taken here shape everything that comes afterwards: tokenisation, the vocabulary, embeddings and, in the end, the architecture of the model itself.
So you need a translator: a function that turns text into real vectors, placed before the network, so that the network only ever sees numerical representations. The uncomfortable part is that this translator is not neutral. The moment you put words into a numerical space, that space starts making claims about them: which are close, which are far, which lie halfway between. You cannot avoid making those claims. You can only choose to make them on purpose or by accident.
This is what makes the problem interesting rather than a formality. Take any translator you like: numbering the words alphabetically, say. It produces perfectly valid numbers, and it destroys every notion of meaning.
What exactly you have to hand a network
Fix the vocabulary, the set of units the model recognises, and call it . For now "unit" means "word"; the lesson on tokenisation will show that this decision is more delicate than it looks. A text is then a finite sequence of elements of :
A network, by contrast, is a function between spaces of real numbers. We can write down its type without yet knowing anything about what it does inside: it takes a vector of a fixed size and returns another,
where is the dimension of the input space and that of the output space. What happens between input and output (the layers, the weights, the training) is the subject of the next block. Here, all that matters is that the network has to be handed vectors from .
The problem shows up at once: a text is a sequence of symbols, and what asks for is a vector in . Nothing turns an element of into an element of on its own. You need a function to close that gap:
and with it every word of the sequence becomes a vector, so the whole text turns into a matrix of numbers, one row per position and columns. The vectors in this course are columns, so stacking them lays each one on its side, as a row:
This whole block is about how to build . Before proposing one, settle what you are going to demand of it. Three things.
Totality. has to be defined for any word that arrives in production, not only for the ones you saw when you built it. The day somebody writes cryptocurrency and that word was not in , the function still has to return something. This is the demand that sounds bureaucratic and turns out to be the most expensive of the three: it is the problem of words outside the vocabulary (out-of-vocabulary, OOV), and further on it gets a whole lesson of its own, on the OOV problem.
Fixed dimension. , with the same for every . The network is designed to expect vectors of a definite size; a representation that sometimes returned a vector of five numbers and sometimes of nine would not fit through the front door.
Determinism. The same word always produces the same vector. If changed between two runs, what the model learned would stop matching what it receives.
The representation that looks like it works and doesn't
With those three conditions on the table, the shortest proposal is to sort the vocabulary and keep the index. With in alphabetical order:
Suppose for a moment that is closed, that no new word will ever arrive. With that concession, and only with it, is total; it also has fixed dimension and is deterministic. The concession is a big one: the moment somebody writes cryptocurrency, has no index to return, and that is exactly the fault line the lesson on the OOV problem runs along. We park it here on purpose, because what we want to see is what happens even when the three conditions hold. And what happens is that the representation is bad anyway, because satisfying them was never all it took. Putting the words into makes them inherit its structure, and that structure asserts things:
In words: apple is more like cat than like dog. It also says that cat lies between the other two, and that the exact midpoint of the road from apple to dog is cat. You made none of those three claims. They come out of the alphabet, which is an order invented for looking things up in a dictionary and knows nothing about meaning.
What matters is not that the claims are false, but that the network has no way of knowing they are. All it receives are the numbers; to the network, an invented geometry looks exactly like a true one, and it cannot tell them apart. You have handed it false information dressed up as good information.
And the flaw is not specific to this representation. You could rescale the indices by dividing by , and nothing would change: dividing by a constant preserves the order and preserves the ratios between distances, so cat would still lie between apple and dog. Any with carries the same underlying problem, because is totally ordered and words are not. Getting out of it means going up in dimension, and that is the argument that leads to the lesson on one-hot encoding.
How many dimensions it takes not to lie
Ask for the least you possibly can: that the representation assert nothing, that no word sit closer to one word than to any other. In other words, that every pairwise distance be equal.
In that is already impossible with three words. If , then : there is always one that ends up in the middle. In the plane it can be done, with an equilateral triangle, but only with three. Four equidistant points force you out into space, and in general mutually equidistant points need dimensions.
Check it yourself, on one sentence
Take the sentence the cat drinks milk and the alphabetically ordered vocabulary , with indices to . The sentence becomes the sequence , and with the matrix from the previous section has shape . Now answer three questions looking only at those numbers, the way the network looks at them:
- By the numbers, which two words of the sentence are most alike? Compare and the rest of the distances, and do not look for a single winner: three pairs are tied at distance (cat and drinks, drinks and milk, milk and the), and you have not miscounted. Does that tie match what you know about those words?
- Which word falls at the midpoint between cat and milk ? Does that question mean anything outside the numbers?
- Add bird to the vocabulary. Sort alphabetically again and reassign the indices. How many of the original words change number, even though the sentence has not changed at all?
The third is the most uncomfortable: a new word rewrites the representation of words that have nothing to do with it, and a model trained on the earlier is left useless. Notice that this is a different flaw from the other two. It is not the invented geometry, and it is not the OOV problem either: bird does end up with an index, and the vocabulary grows without difficulty. What fails is that the representation is not stable: it depends on the whole of at once, so touching one entry moves every other. Hold on to that observation. In the lesson on one-hot encoding it is what makes the jump to a far larger space look reasonable despite the cost.
Test your intuition
Three questions about what we have now set out: why the network is not handed text, what happens to a vocabulary numbered by order, and what exactly we ask of the function .
What is the underlying reason a network cannot be handed the string hello directly?
Numbering the vocabulary in alphabetical order (apple → 1, cat → 2, dog → 3) is a valid representation, because it assigns every word a number deterministically.
Which properties does a representation function have to satisfy in order to feed a network? Tick all the correct ones.
Select every correct option. This is graded all-or-nothing: there is no partial credit.
The problem the rest of the block lives on is now stated: we need a function that is total, of fixed dimension and deterministic, and that, on top of all that, carries a geometry which does not accidentally assert false things about words. We already know that will not do.
But there is an earlier question we have been treating as settled every time we write as though it were obvious: before deciding which vector belongs to unhappiness, you have to decide whether unhappiness is one unit of the vocabulary, or three (un, happi, ness), or eleven letters. That decision fixes the size of , defines what it means for a word to be "new" and, in the end, bounds what the model can learn. That is the next lesson, on tokenisation.