PyTorch tensors
Tensors are the fundamental data types of PyTorch. A tensor is a multidimensional matrix similar to NumPy’s ndarrays:
- A scalar can be represented as a zero-dimensional tensor.
- A vector can be represented as a one-dimensional tensor.
- A two-dimensional matrix can be represented as a two-dimensional tensor.
- A multi-dimensional matrix can be represented as a multi-dimensional tensor.
Pictorially, the tensors look as follows:
For instance, we can consider a color image as a three-dimensional tensor of pixel values, since a color image consists of height x width x 3
pixels – where the three channels correspond to the RGB channels. Similarly, a grayscale image can be considered a two-dimensional tensor, as it consists of height x width
pixels.
By the end of this section, we will have learned why tensors are useful and how to initialize them, as well as how to perform various...