Go to Map Courses Lab Glossary How it works Troubleshooting Start →
ITEN
← Course 05 · Understanding language (Lesson 05.01 · 16 minutes · No sensor · For everyone)

Words become numbers.

A neural network only knows how to multiply numbers. Here you'll see how a sentence becomes pieces, how the pieces become numbers, and why in the end "cat" lands close to "kitty" and far from "bolt".

What you learn
  1. What tokens are and why words get split
  2. What an embedding is and why similar words end up close together
  3. How to measure how close two sentences are
You need to know

If you don't remember one of these, the link takes you to the chapter that explains it.

How do you want to follow it
Preparing the scene…
  1. 01 The problem

    A model only knows numbers, not words.

    A neural network01.03 does multiplications. A photo is already made of numbers, the pixels, and so is a sound. A sentence isn't. First of all, the text has to be cut into pieces, and each piece has to be turned into numbers the model can use. How you do that decides how much the model will be able to understand.

    In practice. When a text model makes strange mistakes, first check how it cut up the sentence. That's often where the problem starts.

01 / 07
(Go deeper)

A few more numbers.


(01)

How a token vocabulary is built

The most common method is called byte pair encoding. You start from single letters and look, in a huge collection of texts, for the pair of pieces that most often shows up side by side. That pair becomes a new piece. You repeat this until the vocabulary reaches the size you want.

GPT-2, in 2019, had 50,257 tokens. Llama 3 has 128,256, and Gemma 3 about 262,000, to cover many languages better. A bigger vocabulary splits words less, but it makes the model heavier, because each token has its own embedding.

  • 50,257tokens in GPT-2
  • 128,256tokens in Llama 3
  • 262,000tokens in Gemma 3, roughly
(02)

King minus man plus woman

In 2013 Tomas Mikolov and colleagues at Google published word2vec, a fast way to learn word embeddings. People found that the directions had a meaning. The embedding of king, minus the one for man, plus the one for woman, landed close to queen.

In 2016 another group showed the other side. With the same math, programmer minus man plus woman landed close to homemaker. Embeddings also learn from texts the biases of the people who wrote them. A model that uses them carries those biases along, unless someone looks for them and fixes them.

(03)

Cosine similarity, in code

For two lists of numbers of the same length, you multiply the elements in pairs and add them up. Then you divide by the lengths of the two lists. The result is the cosine of the angle between them. One means same direction, zero means no link, and minus one means opposite directions.

With sentence embeddings, real values almost always fall between 0.2 and 0.9. That's why you don't pick the threshold between similar and different in advance. You test it on a few pairs of sentences where you already know the answer.

function cosine(a, b) {
  let ab = 0, aa = 0, bb = 0;
  for (let i = 0; i < a.length; i++) { ab += a[i] * b[i]; aa += a[i] * a[i]; bb += b[i] * b[i]; }
  return ab / Math.sqrt(aa * bb);
}
(Try it on your phone)

Text your phone understands on its own


Put your phone in airplane mode and try these features. The ones that still respond use text models that live inside the phone.

  1. (01) Type the start of a sentence and look at the words the keyboard suggests
  2. (02) Type a word wrong on purpose and see if it gets corrected
  3. (03) Search your photos for a word like beach, dog or cake
  4. (04) Select the text inside a photo and copy it
  5. (05) Translate a sentence with the translation app

Photo search is the most interesting case. It doesn't look for the word in the file name. It compares the meaning of the word with the meaning of the photos, like the embeddings in this lesson.

(Quiz)

4 questions to close the lesson.

When you have answered them all, the lesson counts as done and its stone on the map turns yellow. Answering is what counts, not the score.


  1. (01)

    Why does text have to be cut into tokens before it reaches the model?

  2. (02)

    "Cat" is token 18,402 and "cats" is 18,403. Does that mean they're close in meaning?

  3. (03)

    Why do "cat" and "kitty" have close embeddings?

  4. (04)

    You search your photos for beach and the sea shows up. What happened?

To close the lesson, answer the quiz

← Predicting a number Back to the map
(Next lesson · 05.02) Classifying and searching text on the phone