PyTorch Tensor Broadcasting
Broadcasting is a powerful feature in PyTorch that allows operations between tensors of different shapes. Instead of creating new tensors with repeated data, PyTorch implicitly expands the smaller tensor to match the shape of the larger one during operations, saving memory and computational resources.
Introduction to Broadcasting
When performing operations between tensors, PyTorch requires compatible shapes. However, PyTorch doesn't always need tensors to have identical shapes—this is where broadcasting comes in. Broadcasting automatically expands smaller tensors across dimensions to match the shape of larger tensors, enabling operations like addition, subtraction, multiplication, and division between differently-shaped tensors.
Broadcasting Rules in PyTorch
PyTorch follows NumPy's broadcasting semantics. Two tensors are compatible for broadcasting if they satisfy the following rules:
- Each tensor has at least one dimension.
- When comparing dimensions from right to left:
- Dimensions must be equal, or
- One of the dimensions must be 1, or
- One of the tensors doesn't have the dimension (it's considered as having size 1)
Let's explore these rules with examples.