Skip to content
cd /learn/tiny-language-model

Step 1: How text becomes numbers

Inside a tiny language model · Step 1 of 4

How my Shakespeare model turns letters into numbers it can do math on, shown with real values from the model I trained.

The big picture

The model has exactly one job: look at some text and guess the next character. Writing a whole passage is just that guess repeated, one character at a time. The model only does arithmetic, so first every character has to become numbers. That conversion is Step 1.

Swipe sideways to see the rest of this figure.

Step 1 covers the blue boxes. Later steps take the rows of numbers from there.

My model is based on Andrej Karpathy's nanoGPT (MIT license). The training text is the Tiny Shakespeare dataset from Karpathy's char-rnn.

Words to know

Token

One unit of text the model reads. In my model a token is one character.

Vocabulary

The full list of tokens the model knows. Mine has 65.

ID

A token's position in that list. It's just a name tag, like a row number.

Embedding

A row of 384 numbers for each token. The model adjusts these numbers while it learns.

Loss

A single number that says how wrong the next-character guess was.

Text to ID numbers

Swipe sideways to see the rest of this figure.

The real vocabulary: every character that appears in the 1,115,394-character Shakespeare file. ↵ is a line break and ␣ is a space.

Before training, every different character in the file was listed, sorted, and numbered 0 to 64. Then the whole file was swapped for those IDs. The last 10% was set aside, so the model could be checked on text it never trained on.

Because the IDs come from sorting, they carry no meaning. The model only does arithmetic. If it did math on the IDs themselves, it would treat 'z' (64) as bigger than 'a' (39). It would also treat 'a' (39) and 'b' (40) as near-twins, just because they sit next to each other in the list. Neither of those is true about letters.

Here's my test sentence, using the same table:

Swipe sideways to see the rest of this figure.

"To be, or not to be:" becomes 20 ID numbers, one per character, spaces included.

Big models do this with more IDs, and each ID is a chunk of a word instead of a single letter. They typically have about 50,000 to 200,000 of them, so a word like fuzzing might be split into "fuzz" and "ing". That takes two steps instead of seven, and each piece already carries part of a word.

The 256-character window is from my run. fuzzing is an example word, not from the Shakespeare file.

With only 65 characters, my table is tiny. The cost: writing a word takes many more steps. The model can only look at 256 characters at a time (about 50 words). And it has to learn spelling before it can learn anything else.

ID numbers to embeddings

An ID is only a pointer. The model keeps a table with one row per character (65 rows by 384 columns), and each ID picks out its row. That row, a list of 384 numbers, is the character's embedding. It's what the rest of the model actually works with.

One number can carry only one trait. A row of 384 numbers can carry many traits at once, such as whether a character is a vowel, whether it's a capital, or whether it often comes before a space. 384 is a size I chose, not something the data forced on me. More numbers per character give the model more room to store differences, but they cost more computing and need more text to fill in well. I also picked 384 because in a later step the model looks at the text in 6 parallel ways, and 384 splits evenly into 6 groups of 64. GPT-2 small, an early public language model, used 768 numbers per token. Today's large models use several thousand.

The trait names are illustrations. Nobody labels the 384 numbers. The "large models" bar is drawn at 4,096 to stand for "several thousand", not one specific model.

Here are the first 16 of the 384 numbers in the row for 'e', before and after training:

Swipe sideways to see the rest of this figure.

Real values. Blue is positive, amber is negative, and a stronger color means further from zero.

At step 0, the numbers are random noise: small values close to zero. Nobody tells the model what 'e' means. By step 5,000 the values have grown (for 'e', their typical size went from 0.019 to 0.065; across the whole table, from 0.020 to 0.077). Yet the row still looks like its starting version: 90% of its 384 numbers kept their plus or minus sign. Training adjusted the random start; it didn't replace it. The next section explains the loop that made those changes.

The model also adds a second learned row for each position (1st character, 2nd character, and so on), so it knows the order. That matters in later steps.

How training changed the numbers

The model never went online and never browsed the internet. It read one file I supplied: 1,115,394 characters of Shakespeare. Then it repeated this loop 5,000 times:

A guess is a percentage for each of the 65 characters, and those percentages add up to 100%. After my test sentence, "To be, or not to be:", the trained model at step 5,000 was 56.12% sure the next character was a line break and 43.78% sure it was a space. The other 63 characters shared the remaining 0.10%.

Swipe sideways to see the rest of this figure.

Real percentages from the model at step 5,000, after the test sentence.

The model then measures how wrong that guess was (the loss). It nudges every number, including the 384-number rows, a tiny bit in the direction that would have made the guess less wrong. Nobody told it to group anything. Characters that get used in similar spots got similar nudges, so their rows slowly became more alike. The model is not counting how often characters sit next to each other and then assigning a probability.

That's why a and A ended up linked. They show up in similar spots, as in "and" and "And", or "all" and "All", and the same characters often follow both. After a, the next character is n 18% of the time; after A, it's n 27% of the time. When the model is nudged to guess n next, both rows get pushed the same way. At step 0, the rows for a and A were unrelated (−0.007). At step 5,000 they scored 0.328.

Counts and next-character rates are from the 1,115,394-character Shakespeare file. Similarity uses all 384 numbers for a and A.

Characters that barely appear get few nudges. $ shows up once in the whole text and 3 shows up 27 times, so they stay out at the edges of the plot below.

What training did to the table

Each character's 384 numbers squashed into 2 with PCA, a method that keeps the directions where the points spread out most. Each plot is squashed on its own, so the axes of the two plots don't line up. Leader lines join a moved label to its dot.

What moved was the 384 numbers in each character's row. At step 0, the characters are scattered randomly and the groupings below are missing: the similarity scores sit near zero. At step 5,000, the same characters have formed groups:

  • Uppercase letters gathered in their own area, separate from lowercase.
  • Lowercase letters formed a second big group, and the vowels a, e, i, o, u sit together inside it.
  • Punctuation clustered together: the closest neighbors of , . ? : are all other punctuation marks.
  • Rare characters drifted to the edges: X, Z, Q, x, q, z, j, 3, $, &. The $ shows up once in the whole text and 3 shows up 27 times, so they got little training.

To check this beyond the 2D picture, the table below scores similarity using all 384 numbers. 1 means two rows point the same way, 0 means unrelated, and a negative score means opposite.

Pair of charactersStep 0Step 5,000
Each letter and its own capital (a/A, t/T…)−0.0060.342
Uppercase with uppercase−0.0020.224
Punctuation and spacing with each other−0.0130.165
Lowercase vowel with vowel0.0250.112
Lowercase consonant with consonant−0.0050.055
Lowercase vowel with punctuation−0.006−0.085

The strongest evidence is the first row: each letter became similar to its own capital (0.342). That link wasn't there at step 0 (−0.006).

At this step, each character has one fixed row of numbers. That row doesn't carry the meaning of a word, so this step can't tell a fuzzy kitten from fuzzing as a security practice. Telling two uses of a word apart comes from the surrounding text, in the later layers, which is Step 2. And my model has only read Shakespeare, so it has never seen security fuzzing at all.

Next: Step 2, how the model uses context.