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.
numbers = [10, 20, 30]
print(numbers + 5)
A normal list can't do that. We need something better.
A tensor does math on every number at once. Let's meet one.
Meet the tensor
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
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.
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.
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.
table = torch.tensor([[2, 4], [6, 8], [10, 12]]). What is table[2, 1]?table[?, 1]. What is the ?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])
$ python lesson1.py
tensor([10, 20, 30])
tensor(10)
tensor([15, 25, 35])
tensor([20, 40, 60])
tensor([4, 5, 6])
tensor(6)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.
numbers = torch.tensor([10, 20, 30]). What does numbers * 2 give?[10, 20, 30]. What is numbers[2]?table[2, 0]?table = torch.tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])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”?