Reshape and reduce tensors
PyTorch stores numbers in tensors. A tensor is a block of numbers with a shape: a list of 6 numbers has the shape 6, and a table of 2 rows and 3 columns has the shape 2 by 3. The same 6 numbers can be viewed either way without copying or reordering them. Most of the work in PyTorch code is keeping track of shapes, so this lesson practises the two moves you will use most: changing a shape, and adding along one direction.
About this lesson
Words to know
- tensor
- PyTorch's block of numbers. It can be one number, a list, a table, or have more dimensions.
- shape
- The size of a tensor along each dimension. A table of 2 rows and 3 columns has shape
[2, 3]. - dimension
- One direction of a tensor. In a table, dimension 0 runs down the rows and dimension 1 across the columns.
Why it matters
Every later PyTorch lesson is a chain of shape changes like this one. Reading a shape off each line is the fastest way to find a bug in model code.
With a year
How it works, a worked example, the exercise with its tests, three hints and the explained solution are in the app, with a year of Algo150 for $19. The first chapter, Math foundations, is free for everyone: start with Follow a gradient.