For tensors of any rank, broadcast rules define how tensors with different dimensions can interact via element-wise operations. A justifying premise of broadcast is that a rank-
Consider tensors
- If
, no broadcast is needed. - Comparing dimensions
from right to left, broadcast is permitted if and only if at least one of the following is true: ; and/or ; and/or .
If broadcast is permitted between
- If the ranks are unequal, prepend the lower-ranked tensor with length-1 dimensions of unit value until the ranks match.
- For any dimension where one of the tensors has length 1 and the other has length
, expand the tensor with length 1 by repeating its value times.
Examples where broadcast is permitted
Scalar and any tensor. For example, given a scalar (rank-0 tensor) and a vector (rank-1 tensor), any scalar arithmetic operation between them is defined as performing that operation between the scalar and each element of the vector.
Length
Examples where broadcast is not permitted
Vectors of unequal length, neither of length 1. In this case, neither can be expanded to match the other.
Length