← Build a Tiny LLM Lesson 2 of 10

Text into numbers

The problem

I see letters everywhere, but they mean nothing to me. Can you turn them into numbers?

An LLM reads text, but tensors hold only numbers. How do we turn text into numbers?

Try it: do math on a letter.
print("h" + 5)
# press Run

First, find every different character in the text.

Find the letters

This is our text. Watch it find every different character. You can type your own too.
Each different character, sorted, with its number:

This list is the vocabulary. It's every character the model knows, like these 14.

The blank tile is the space. It's a character too.

Each character has a number now. Let's turn whole words into numbers.

Encode and decode

Vocabulary: the 14 different characters of our text.

Type a word. See it as numbers.
encode ↓
decode ↓

Encode: text → numbers. Decode: numbers → text.

Guess first, then try both in the box above. Which one crashes?

Try it on new text.

✏️ Practice 2.1

New text, same ideas.
Remember: the vocabulary is every different character, sorted, numbered from 0. The space sorts first, then “.”, then the letters.
encode swaps each character for its number.

Work each one out. Type the answer, press Check.
How many different characters are in "banana band"? The space counts.
In our vocabulary, what number does m get? (The characters are sorted: ␣ . a c d e g h l m n o s t.)
What is the sum of the numbers in encode("sat")?
A new text is "to go.". Its characters are sorted the same way. What number does g get?

Here's all of it in Python.

The code

Guess what each print shows. Then press ▶ Run.

import torch

text = "the cat sat on the mat. the dog sat on the log."

# Find every different character, sorted
unique_chars = sorted(set(text))
print(unique_chars)

# Give each one a number, and the reverse
char_to_number = {}
number_to_char = {}
for number, char in enumerate(unique_chars):
    char_to_number[char] = number
    number_to_char[number] = char

# Text -> numbers
def encode(some_text):
    numbers = []
    for char in some_text:
        numbers.append(char_to_number[char])
    return numbers

# Numbers -> text
def decode(numbers):
    result = ""
    for number in numbers:
        result = result + number_to_char[number]
    return result

print(encode("cat"))

print(decode([4, 11, 6]))

# The whole text as a tensor
print(torch.tensor(encode(text)))

Colab runs Python with PyTorch in your browser for free. You just need a Google account.

Quick check: did it all make sense?

Lesson 2 test

5 questions, one for each idea in this lesson. Answer them all right to pass. A wrong answer tells you why, and which page to look at again.

1. VocabularyHow many different characters are in "banana!"?
2. EncodeOur vocabulary is ␣ . a c d e g h l m n o s t, numbered 0 to 13. What is encode("cat")?
3. DecodeSame vocabulary. What is decode([4, 11, 6])?
4. Spot the problemWith the same vocabulary, what happens with encode("hat!")?
5. WhyWhy do we turn text into numbers at all?

What your model still can't do.

Guess the next letter

Now text is numbers: "cat" → [3, 2, 13].

An LLM's whole job is to guess what comes next:

t h e  c a ?

I can read text as numbers now! But how do I guess which letter comes next?

This leaves a question
An LLM's whole job is guessing the next character. Now that text is numbers, how do we make our first guess?
Lesson 3 answers it.