广播机制的语法三点
1. 从内到外追溯
比如 (1,2,3,4) 的追溯顺序为4, 3, 2 然后 1,也就是追溯他的存储结构。
底层先存储4, 然后。。。
(z, y, x)这么记忆吧
2.张量必须有一个维度
3.追溯过程中,要么两张量,维度大小一样 或者 其中一个为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