Why tensors — and how the lesson climbs
Lesson one said the network "sees" numbers. In this lesson you find out what shape those numbers have and how PyTorch works with them.
The whole course stands on one data structure: the tensor. A tensor is an n-dimensional array of numbers, and the only way it differs from a Python list is that PyTorch can do two important tricks with it:
- Compute every element at once. Instead of a loop you write one expression,
a * 2 + 1, and it applies to every number. - Compute the gradients for you. Remember "guess → compare → adjust"? To adjust, you need to know which way to nudge each weight. PyTorch works that out automatically — the machinery is called autograd.
A tensor, by shape
What tells tensors apart is their shape: a list of numbers saying how many elements each axis holds:
| what it is | shape | example |
|---|---|---|
| a single number | [] |
a loss: 9.0 |
| a row of numbers | [784] |
one "flattened" picture |
| a table | [28, 28] |
one picture |
| a stack of tables | [64, 28, 28] |
64 pictures at once |
When code "doesn't work", nine times out of ten the shape is to blame. So this whole lesson is really about one habit: look at the shape first.
What we learn on: FashionMNIST
From lesson 2 to lesson 8 we train a classifier on a ready-made set called FashionMNIST: 70,000 grayscale 28 × 28 pixel pictures of clothing in 10 categories (T-shirt, trouser, pullover, dress, coat, sandal, shirt, sneaker, bag, ankle boot).
Why not your own categories from lesson one? Because from the very first step you want a known, clean set whose accuracy you can compare with other people's. Your own image folders arrive in lesson 9, once you know what to do with them. In the homework you touch the road there.
The lesson climbs in steps
You never write anything from a blank page. Each idea comes in three steps:
- Watch. A small example, with the real printed values and shapes. You write nothing.
- Fill the holes. You get a working file with two lines missing. A test tells you whether you got them right.
- Write it yourself. Four short functions whose skeleton is already standing — and what they do, you have already seen.
After that: how it all works with real data, seven common PyTorch mistakes and their error messages, and drills.
Guess before reading on
Write down two answers:
- How many numbers are in one 28 × 28 picture?
- What shape do you get if you stack 64 such pictures into one tensor?
Both answers are in the next step — together with what those numbers really look like.