pytorch 与 numpy 的数组广播机制

numpy 的文档提到数组广播机制为:
When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward. Two dimensions are compatible when
they are equal, or

  1. one of them is 1
  2. If these conditions are not met, a ValueError: frames are not aligned exception is thrown, indicating that the arrays have incompatible shapes. The size of the resulting array is the maximum size along each dimension of the input arrays.

翻译过来就是,从两个数组地末尾开始算起,若轴长相等或者其中一个地维度为1,则认为是广播兼容的,否则是不兼容地。广播兼容的数组会在缺失的维度和长度为1的维度上进行。

例如:

a.shape+b.shapec.shape
(4, 1)+(1)-->(4, 1)
(4, 1)+(3,)-->(4, 3)
(2, 3, 4)+(1, 4)-->(2, 3, 4)
(2, 3, 4)+(3, 1)-->(2, 3, 4)
(2, 3, 4)+(2, 1, 1)-->(2, 3, 4)
(2, 3, 4)+(3, )X
(4, 3)+(4,)X
(4, 3)+(3,)-->(4, 3)
(4, 3)+(3)-->(4, 3)

910605-20181018103747652-608707640.png

转载于:https://www.cnblogs.com/zi-wang/p/9809096.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值