Pytorch之扩充tensor

Pytorch之扩充tensor

直接看代码:

b = torch.zeros((3, 2, 6, 6))

a = torch.zeros((3, 2, 1, 1))
a.expand_as(b).size()
Out[32]: torch.Size([3, 2, 6, 6])

a = torch.zeros((3, 2, 2, 1))
a.expand_as(b).size()
Traceback (most recent call last):
  File "/home/lart/.conda/envs/pt/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-34-972575f79e92>", line 1, in <module>
    a.expand_as(b).size()
RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton dimension 2.  Target sizes: [3, 2, 6, 6].  Tensor sizes: [3, 2, 2, 1]

a = torch.zeros((3, 2, 1, 2))
a.expand_as(b).size()
Traceback (most recent call last):
  File "/home/lart/.conda/envs/pt/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-36-972575f79e92>", line 1, in <module>
    a.expand_as(b).size()
RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton dimension 3.  Target sizes: [3, 2, 6, 6].  Tensor sizes: [3, 2, 1, 2]

a = torch.zeros((3, 2, 2, 2))
a.expand_as(b).size()
Traceback (most recent call last):
  File "/home/lart/.conda/envs/pt/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-38-972575f79e92>", line 1, in <module>
    a.expand_as(b).size()
RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton dimension 3.  Target sizes: [3, 2, 6, 6].  Tensor sizes: [3, 2, 2, 2]

a = torch.zeros((3, 2, 6, 2))
a.expand_as(b).size()
Traceback (most recent call last):
  File "/home/lart/.conda/envs/pt/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-40-972575f79e92>", line 1, in <module>
    a.expand_as(b).size()
RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton dimension 3.  Target sizes: [3, 2, 6, 6].  Tensor sizes: [3, 2, 6, 2]

a = torch.zeros((3, 2, 6, 1))
a.expand_as(b).size()
Out[44]: torch.Size([3, 2, 6, 6])

a = torch.zeros((3, 2, 1, 6))
a.expand_as(b).size()
Out[46]: torch.Size([3, 2, 6, 6])

tensor.expand_as在这里用于扩展tensor到目标形状,常用的多是在H和W方向上的扩展。假设目标形状为N, C, H, W,则要求tensor.size()=n, c, h, w(这里假设N,C不变):

  1. h=w=1
  2. h=1, w!=1
  3. h!=1, w=1
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值