← Build a Tiny LLM Lesson 1 of 10

Tensors

The problem

Hi, I'm your model. You're going to build me! I want to write stories. But I only understand numbers. Lots and lots of them.

A model is just numbers. Millions of them.

We need to do math on all of them at once.

Try it: add 5 to every number in a normal Python list.
numbers = [10, 20, 30]
print(numbers + 5)
# press Run

A tensor does math on every number at once. Let's meet one.

Meet the tensor

Type a number and press Enter. Then press a button.
numbers

The row of numbers you typed, kept together as one thing, is a tensor.

One line of math changes every number inside it.

A tensor holds many numbers. How do you take out just one?

Pick one number

Click a number to take it out.
# click a number

The small number above each item is its position. Counting starts at 0.

numbers = torch.tensor([10, 20, 30])
What is numbers[0]?

Try it on new numbers.

✏️ Practice 1.1

New numbers, same ideas.
Remember: numbers[0] is the first number. Positions start at 0.
numbers * 2 - 10 changes every number: first × 2, then − 10.

Work each one out. Type the answer, press Check.
numbers = torch.tensor([2, 8, 5]). At which position is the 8?
numbers = torch.tensor([4, 6, 10]). What is numbers[1] + numbers[2]?
numbers = torch.tensor([10, 20, 30]). After numbers * 2 - 10, what is the number at position 2?

A model needs more than one line of numbers. Let's make a table.

Tables of numbers

A tensor can also be a table. Rows and columns. Like this one.

Click a number, or click a row name.
# click something

Try it on new numbers.

✏️ Practice 1.2

A table needs two positions: first the row, then the column. Both start at 0.
Remember: table[row, column]
A table holds rows × columns numbers.

Work each one out. Type the answer, press Check.
table = torch.tensor([[2, 4], [6, 8], [10, 12]]). What is table[2, 1]?
In the same table, the 8 is at table[?, 1]. What is the ?
A table with 14 rows and 14 columns holds how many numbers?

Here's all of it in Python.

The code

Guess what each print shows. Then press ▶ Run.

import torch

# Make a tensor
numbers = torch.tensor([10, 20, 30])
print(numbers)

# Take one number out
print(numbers[0])

# Math on every number at once
print(numbers + 5)

print(numbers * 2)

# A table
table = torch.tensor([[1, 2, 3],
                      [4, 5, 6],
                      [7, 8, 9]])
print(table[1])

print(table[1, 2])

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

Quick check: did it all make sense?

Lesson 1 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. Predictnumbers = torch.tensor([10, 20, 30]). What does numbers * 2 give?
2. PositionSame tensor [10, 20, 30]. What is numbers[2]?
3. TableWhat is table[2, 0]?
table = torch.tensor([[1, 2, 3],
                      [4, 5, 6],
                      [7, 8, 9]])
4. Why tensorsWhy does a model use tensors instead of normal Python lists?
5. ConnectIn Lessons 3 and 4, your whole model is one tensor. What shape is it?

What your model still can't do.

What about text?

Your model can do math on lots of numbers at once.

But stories are made of letters, like "cat", and a tensor only holds numbers.

Now I can hold lots of numbers at once! But… how do I read a word like “cat”?

This leaves a question
An LLM reads text, but tensors hold only numbers. How do we turn text into numbers?
Lesson 2 answers it.