How does a language model decide what a word means?

A Large Language Model has never tasted food. It hasn’t learnt that being chased by a dog is a different experience from being chased by a puppy, and yet, when asked if “Queen” is closer to “King” or “Pizza”, it has no struggle identifying it. So, what is going on when a model “understands” a word?

It doesn’t know what a word means. Instead it works it out using maths. Today, I will go over that maths and try and explain it in the way that felt most clear and accessible to me. The numbers I will be using are from the Word Vector Lab, which I released with a friend to visualise this.

Words are just lists of numbers

Before a model can use a word, it creates a numeric representation of that word. Each word (technically, a token) is represented as a vector. They are called embeddings and are the heart of every modern LLM. This idea became mainstream, but how are the vectors calculated? People don’t sit down and calculate vectors manually. Rather, the model nudges them based on how each word is used in the training text, so words that often show up together or in the same context get nudged closer together. “Cat” and “kitten” often appear in the same context, so they get similar vectors. “Cat” and “bicycle” don’t, and hence the vectors are more spread out.

This is not limited to 3D space. A list of numbers is also a point in space. Two numbers is a point on a flat graph, three is a point in 3D, and so on. Draw an arrow from the origin to that point and you get a direction and a length. The trick is that words with similar meaning end up with arrows in similar directions. The model we used is all-MiniLM-L6-v2, which gives every word 384 numbers. A common pitfall is to assume that these vectors have meaning themselves. Instead, the meaning lives in the patterns across them.

Try it yourself

Now, most of us (me included) struggle to visualise even 4 dimensions, so 384 is a tiny bit difficult. For Word Vector Lab we squashed them down to 3 using PCA, keeping the three directions in which our 24 words differ the most. Now every word can be mapped as a vector on a 3D grid. The demo here will show you the vectors and the maths that follows.

Word Vector Lab: select two words and follow the maths. It runs entirely in your browser, and nothing you select is sent or saved. Open it in full size.

The words I am using are here, with their numbers the same as in the demo above:

WordVector
dog[2.75, 1.35, −1.09]
puppy[3.07, 1.30, −1.01]
cat[2.80, 0.94, −1.52]
kitten[3.26, 0.92, −1.03]
bread[−2.85, −0.37, −2.82]
man[0.41, −3.77, 1.95]
woman[0.01, −1.80, 1.02]
boy[0.95, −3.20, 1.34]

Which is the odd one out?

Since similar words point in similar directions, you can compare them by measuring how the arrows line up. This is cosine similarity:

cosine similarity(A,B)=A·B‖A‖×‖B‖

A·B is the dot product: multiply the corresponding numbers together, then add the results. ‖A‖ is the length of the vector; it’s essentially Pythagoras’ theorem in three dimensions. Dividing by the lengths cancels them out, leaving only the direction. A value of 1 indicates the same direction, 0 indicates no relationship between the vectors, and −1 indicates the opposite direction.

So, which of these doesn’t fit: dog, cat or bread? Let’s start with dog and cat:

dog·cat=(2.75×2.80)+(1.35×0.94)

+(−1.09×−1.52)

=7.7000+1.2690+1.6568

≈10.626

‖dog‖=2.752+1.352+(−1.09)2≈3.252

‖cat‖=2.802+0.942+(−1.52)2≈3.322

cos⁡(dog,cat)=10.6263.252×3.322≈0.984

0.984 is very close to the maximum of 1. And now, what about dog and bread?

dog·bread=(2.75×−2.85)+(1.35×−0.37)

+(−1.09×−2.82)

=−7.8375−0.4995+3.0738

≈−5.263

‖bread‖=(−2.85)2+(−0.37)2+(−2.82)2≈4.026

cos⁡(dog,bread)=−5.2633.252×4.026≈−0.402

When the same approach is applied to cat and bread, the result is −0.302. Dog and cat are separated by approximately 10°, while bread is more than 100° away from both of them. It’s clear that bread is the odd one out. Honestly, you probably already knew this. The point is that the model arrived at this result using only multiplication and addition.

Adding and subtracting meaning

And that’s where the fun begins. Since words are vectors, they can be added and subtracted from each other, and the results often make sense.

Take kitten and subtract cat from it. What remains roughly indicates the direction corresponding to the concept of “young”:

young=kitten−cat

=[3.26,0.92,−1.03]

−[2.80,0.94,−1.52]

=[0.46,−0.02,0.49]

Now, add “young” to dog:

dog+young=[2.75,1.35,−1.09]

+[0.46,−0.02,0.49]

=[3.21,1.33,−0.60]

It’s not exactly a word from the list, and in practice, this almost never happens. Instead, you need to find the word whose arrow points most closely in the same direction (excluding the words used in the sum). The best match is puppy, with a cosine similarity of 0.992. Dog plus young equals puppy.

This mostly works in reverse as well. Subtracting young from puppy gives [2.61, 1.32, −1.50], and dog scores 0.991. Unfortunately, lion scores slightly higher, at 0.996, which is a good reminder that this is about the nearest match and is very unlikely to produce an exact answer.

Analogies

The same arithmetic can help answer questions like “a man is to a boy as a woman is to what?” Take boy, subtract man and add woman:

boy−man+woman=[0.95,−3.20,1.34]

−[0.41,−3.77,1.95]

+[0.01,−1.80,1.02]

=[0.55,−1.23,0.41]

The word with the closest meaning is girl, with a similarity score of 0.984. Subtracting man removes the meaning of “adult male”, adding woman brings back “female”, and the result is very close to girl.

The most well-known example is king − man + woman = queen. I was very interested to try it, and in our 3D version the result was [−0.45, 0.26, −0.11]. The closest word to this is… plane. Queen actually scores −0.474. The problem is that the arrow for man is more than twice as long as the arrow for woman (4.264 compared to 2.069), so subtracting it pushes the result past the origin and into the region of vehicles. Three numbers simply abstract the data so aggressively they remove a lot of the relationships.

The 3D picture lies a little

This brings us to the catch. Reducing the 384 numbers to 3 keeps the four groups of words (people, animals, food and vehicles) separate from each other. However, the differences between them are exaggerated. This is what the full model shows compared to the 3D picture:

WordsFull model (384 numbers)3D demo
king / queen0.6810.994
cat / dog0.6610.984
apple / banana0.4240.850
cat / truck0.448−0.376

In the 3D picture, words in the same group seem much closer to each other than the model really thinks they are. Words from different groups can even seem to point in opposite directions. I really like the last row: in 3D, cat and truck point in almost opposite directions. The full model, though, considers cat and truck to be slightly more similar to each other than apple and banana.

It seems wrong at first, until you remember what exactly the model is measuring. It doesn’t ask, “Are these things of the same type?” Instead, it asks, “Do these words appear in similar places?” The maths remains the same; it’s just that the full model uses 384 numbers instead of 3.

Words in context

So far, each word has been given one fixed vector, which is how Word2Vec and GloVe work. This approach has an obvious drawback: “bank” next to “river” has the same numbers as “bank” next to “loan”. A more modern solution is a technique called attention. It looks at each token in context and determines how much influence the tokens around it should have on it. The result is contextualised embeddings, similar to those used in the GPT family of models, and these form the basis of modern generative AI. Underneath, though, it’s still just lists of numbers and the angles between them.

What is this actually used for?

Comparing vectors is useful in many more situations than just party tricks with puppies:

  • Summarisation. Convert each sentence into a vector (often by averaging its word vectors). Then select the sentences that are most similar in meaning to the document as a whole.
  • Keyword extraction. Each word is compared with the entire document. Words that point most in the same direction as the document are likely to be the main topics.
  • Named entity recognition. Models can be fine-tuned so that people, organisations and places are grouped together. Then each token is checked in context to determine which group it belongs to.
  • Text classification. An entire document is represented as a single vector, which is then compared with an example vector for each category, such as positive or negative reviews. Documents that are similar to each other have similar vectors, so they end up in the same group.

So, does it know what a word means?

Sort of. It knows that dog is next to cat, that dog plus young is puppy, and that none of them is near bread. It doesn’t know that a dog barks, unless the words “dog” and “bark” appear together frequently enough for it to notice. Try the demo to see if you can find a pair that surprises you.