Definition
Broadcasting (Tensor)
Broadcasting is a mechanism in tensor operations that allows arithmetic between tensors of different, but compatible, shapes. Whenever one of the shapes has a size of 1 (or the dimension is missing), that tensor is conceptually “copied” along that dimension until it matches the other array’s size. This eliminates the need to explicitly repeat or tile the data, while letting you write compact vectorised code.
Example: Let and be two tensors.
Intuitively, adding and together would result in a shape mismatch. However, with broadcasting:
- has shape : it can be expanded along the last dimension to
- has shape : it can be expanded along the first dimension to
After that automatic expansion, both match shape , and the elementwise addition is performed. This neatly yields all pairwise sums of rows from A and columns from B, without manually calling a “repeat” or “tile” function.
Broadcasting was introduced by numpy and adapted by PyTorch and other tensor libraries.
Example
Let and be two tensors.
To add and together, and are expanded to have compatible shapes:
Now, both can be added elementwise: