Tensors are the fundamental data structures in PyTorch. They are used to represent multi-dimensional data used for numerical computation, like images, videos, and time series data. Tensors can be thought of as a generalization of vectors and matrices to higher dimensional spaces.
In PyTorch, tensors are represented as multidimensional arrays with a specific data type (e.g. float, int) and a shape (e.g. (3, 4, 5) for a tensor with 3 dimensions and size 3, 4, and 5 along each of those dimensions). Tensors in PyTorch can be created in various ways, such as from Python lists or NumPy arrays, or by using PyTorch’s built-in functions for generating specific types of tensors.
Tensors are widely used in PyTorch for performing operations on data, such as applying mathematical functions, applying machine learning algorithms, and making predictions. For example, in a typical machine learning workflow, we might use tensors to represent the input data (e.g. images) and the target labels, then use PyTorch to train a neural network model to predict the labels from the input data. During training, PyTorch uses tensors to represent the model parameters (e.g. weights and biases), the input data, and the predicted output.
Here is an example of creating a tensor and performing some basic mathematical operations:
import torch
# Creating a tensor from a Python list
a = torch.tensor([[1, 2], [3, 4]])
# Creating a tensor of zeros with shape (3, 4)
b = torch.zeros((3, 4))
# Adding tensors element-wise
c = a + b
# Multiplying tensors element-wise
d = torch.mul(a, c)
# Computing the mean of a tensor
e = torch.mean(d)
print(a)
print(b)
print(c)
print(d)
print(e)
This code snippet creates two tensors, ‘a‘ and ‘b‘, and then performs some basic mathematical operations on them. The resulting tensors, ‘c‘ and ‘d‘, are then printed, along with the mean of ‘d‘.