@hojaelee , During broadcasting the shape matching of the two inputs X, Y
happen in reverse order i.e. starting from the -1
axis. This (i.e. -ve indexing) is also the preferred way to index ndarray
or any numpy based tensors (either in PyTorch or TF) instead of using +ve indexing. This way you will always know the correct shapes.
Consider this example:
import torch
X = torch.arange(12).reshape((12)) ## X.shape = [12]
Y = torch.arange(12).reshape((1,12)) ## Y.shape = [1,12]
Z = X+Y ## Z.shape = [1,12]
and contrast the above example with this below one
import torch
X = torch.arange(12).reshape((12)) ## X.shape = [12]
Y = torch.arange(12).reshape((12,1)) ## Y.shape = [12, 1] <--- NOTE
Z = X+Y ## Z.shape = [12,12] <--- NOTE
And in both the above examples, a very simple rule is followed during broadcasting:
- Start from RIGHT-to-LEFT indices (i.e. -ve indexing) instead of the conventional LEFT-to-RIGHT process.
- If at any point, the shape values mismatch; check
(2.1): If any of the two values are1
then inflate this tensor in this axis with the OTHER value
(2.2): Else, Throw ERROR(“dimension mismatch”) - Else, CONTINUE moving LEFT
Hope it helps.