PyTorch中的resize_(*sizes)方法和resize_as_(tensor)使用举例

参考链接: resize_(*sizes) → Tensor
参考链接: resize_as_(tensor) → Tensor

在这里插入图片描述

代码实验举例:

Microsoft Windows [版本 10.0.18363.1256]
(c) 2019 Microsoft Corporation。保留所有权利。

C:\Users\chenxuqi>conda activate ssd4pytorch1_2_0

(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x00000257BB6AD330>
>>>
>>> x = torch.tensor([[1, 2], [3, 4], [5, 6]])
>>> x
tensor([[1, 2],
        [3, 4],
        [5, 6]])
>>> x.resize_(2, 2)
tensor([[1, 2],
        [3, 4]])
>>> x
tensor([[1, 2],
        [3, 4]])
>>> x.resize_(2, 3)
tensor([[1, 2, 3],
        [4, 5, 6]])
>>> x.resize_(3, 2)
tensor([[1, 2],
        [3, 4],
        [5, 6]])
>>> x
tensor([[1, 2],
        [3, 4],
        [5, 6]])
>>>
>>>
>>>
>>> x = torch.tensor([[1, 2], [3, 4], [5, 6]])
>>> y = torch.randn(2,4)
>>> y
tensor([[ 0.2824, -0.3715,  0.9088, -1.7601],
        [-0.1806,  2.0937,  1.0406, -1.7651]])
>>> x
tensor([[1, 2],
        [3, 4],
        [5, 6]])
>>> y.resize_as_(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Expected object of scalar type Float but got scalar type Long for argument #2 'the_template'
>>> y.resize_as_(x.to(torch.float32))
tensor([[ 0.2824, -0.3715],
        [ 0.9088, -1.7601],
        [-0.1806,  2.0937]])
>>>
>>> y
tensor([[ 0.2824, -0.3715],
        [ 0.9088, -1.7601],
        [-0.1806,  2.0937]])
>>>
>>> y = torch.randn(2,4)
>>> x = torch.tensor([[1, 2], [3, 4], [5, 6]])
>>> x
tensor([[1, 2],
        [3, 4],
        [5, 6]])
>>> y
tensor([[ 1.1216,  0.8440,  0.1783,  0.6859],
        [-1.5942, -0.2006, -0.4050, -0.5556]])
>>> x.resize_as_(y)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'the_template'
>>> x.resize_as_(y.to(torch.long))
tensor([[                1,                 2,                 3,
                         4],
        [                5,                 6, 32651591223607388,
         17451921009344612]])
>>>
>>>
>>>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值