Pytorch 广播机制

import torch
t1 = torch.rand(3, 1, 2)
t2 = torch.rand(2)

print((t1 + t2).shape)

 输出:torch.Size([3, 1, 2])

t1 = torch.rand(3, 1, 1, 2)
t2 = torch.rand(1)

print((t1 + t2).shape)

输出:torch.Size([3, 1, 1, 2])

t1 = torch.rand(3, 1, 1, 2)
t2 = torch.rand(3, 4, 2)

print((t1 + t2).shape)

输出:torch.Size([3, 3, 4, 2])

t1 = torch.rand(3, 4, 5, 6, 7, 8)
t2 = torch.rand(1, 1, 1, 7, 8)

print((t1 + t2).shape)

输出:torch.Size([3, 4, 5, 6, 7, 8])

t1 = torch.rand(3, 1, 1, 2)
t2 = torch.rand(3, 4, 3)

print((t1 + t2).shape)

输出:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-7-ea6af4cc5347> in <module>
      2 t2 = torch.rand(3, 4, 3)
      3 
----> 4 print((t1 + t2).shape)

RuntimeError: The size of tensor a (2) must match the size of tensor b (3) at non-singleton dimension 3

因为 (3, 1, 1, 2) 中的 2 和  (3, 4, 3) 中的 3 不相等 

补充:

_t = torch.rand(3, 4, 5)
print(_t)

输出:

tensor([

[[0.8293, 0.5944, 0.6949, 0.4223, 0.8664],

 [0.1768, 0.0928, 0.0037, 0.4964, 0.1105],

 [0.1414, 0.2498, 0.4602, 0.8202, 0.8564],

 [0.0716, 0.4953, 0.7533, 0.2038, 0.4296]],

[[0.7431, 0.0926, 0.7787, 0.7334, 0.8667],

 [0.2904, 0.4771, 0.2026, 0.4820, 0.4675],

 [0.0720, 0.3891, 0.2807, 0.3197, 0.5371],

 [0.5668, 0.6495, 0.6082, 0.7117, 0.5041]],

[[0.0380, 0.9071, 0.0562, 0.3246, 0.2108],

 [0.1268, 0.8655, 0.0733, 0.6428, 0.2792],

 [0.5878, 0.0264, 0.0615, 0.9818, 0.1933],

 [0.8651, 0.3628, 0.0460, 0.7909, 0.4836]]])

 

print(torch.mean(_t, dim = 0))

输出:

tensor([[0.5368, 0.5314, 0.5099, 0.4934, 0.6480],

            [0.1980, 0.4785, 0.0932, 0.5404, 0.2858],

            [0.2671, 0.2218, 0.2674, 0.7072, 0.5289],

            [0.5012, 0.5025, 0.4691, 0.5688, 0.4724]])

print(torch.mean(_t, dim = 0).shape)

输出:torch.Size([4, 5])

说明,_t.shape 中的 (3, 4, 5),dim = 0 对应 3,dim = 1 对应 4,dim = 2 对应 5。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值