An n-dimensional array, with two superpowers
Strip the deep-learning mystique away and a tensor is just an n-dimensional array — same idea as a NumPy ndarray. The two things that make PyTorch tensors different from NumPy arrays:
- Device transparency. The same code can run on CPU, NVIDIA GPU (CUDA), or Apple Silicon GPU (MPS) by changing one argument.
- Autograd. Every operation can record itself onto a graph so PyTorch can later play it backwards and compute gradients.
Conceptually, the dimensional ladder reads like this:
- 0D — scalar: a single number —
torch.tensor(3.14) - 1D — vector: a row of numbers —
torch.tensor([1, 2, 3]) - 2D — matrix: a table — rows × cols
- 3D: a batch of matrices — common for sequences
(batch, seq_len, features) - 4D: a batch of images —
(batch, channels, height, width)in PyTorch's NCHW convention
The mathematician in the room will object that "tensor" has a stricter meaning involving covariance / contravariance. In deep learning the word has been informally re-borrowed to mean "n-dim array with autograd." That's the meaning the framework, the docs, and every paper uses. Don't fight it.
One gentle warning: shape is the single most useful debugging tool you have in PyTorch. When something breaks, the first move is always print(x.shape). Internalize that habit early.