如果满足以下规则,则两个张量是“可广播的”:

  • 每个张量至少有一个维度。

  • 当开始比较各维度大小时,从最后的维度开始,维度大小必须相等、其中之一为 1,或者其中之一不存在。

比如:

>>> x=torch.empty(5,7,3)
>>> y=torch.empty(5,7,3)
# same shapes are always broadcastable (i.e. the above rules always hold)

>>> x=torch.empty((0,))
>>> y=torch.empty(2,2)
# x and y are not broadcastable, because x does not have at least 1 dimension

# can line up trailing dimensions
>>> x=torch.empty(5,3,4,1)
>>> y=torch.empty(  3,1,1)
# x and y are broadcastable.
# 1st trailing dimension: both have size 1
# 2nd trailing dimension: y has size 1
# 3rd trailing dimension: x size == y size
# 4th trailing dimension: y dimension doesn't exist

# but:
>>> x=torch.empty(5,2,4,1)
>>> y=torch.empty(  3,1,1)
# x and y are not broadcastable, because in the 3rd trailing dimension 2 != 3

<
Previous Post
pytorch 自动微分机制
>
Next Post
类unix系统如何清理磁盘空间, 以及mac系统是如何分配磁盘空间的?