02.02 · UNIT 01 · PyTorch in One Hour: foundations · Lesson
Tensors, shapes, and data types
A tensor is a grid of numbers. Rank counts the axes, and shape gives each axis length.
PLAIN-LANGUAGE INTRODUCTION
The idea
A tensor is a grid of numbers. Rank counts the axes, and shape gives each axis length.
A worked example
torch.tensor([[1,2,3],[4,5,6]]) has shape (2,3). Its transpose has shape (3,2).The input
Python numbers or nested Python lists.
The result
A tensor with a rank, a shape, and a data type.
Why you use it
Shape and data type decide which operations are valid.
A limit to remember
A scalar of shape () is not a one-item vector of shape (1,).
Check your understanding
- Rank 0 is a scalar, rank 1 is a vector, and rank 2 is a matrix. Higher ranks have no special name.
torch.tensorreads a Python number or nested list. Each extra list level adds one axis.- With the usual defaults, Python integers become
torch.int64. Python floats becometorch.float32. Setdtypeexplicitly when a particular type matters. - Reshape and transpose keep the number of values. Matrix multiply needs matching inner sizes:
(2,3)times(3,2)gives(2,2). - Common mistake: a reshape changes the view, not the meaning of each axis. Track what every axis represents.