参考链接: 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 "" , 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 "" , 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]])
>>>
>>>
>>>