参考链接: type(dtype=None, non_blocking=False, **kwargs)
总结:
该方法的功能是:
当不指定dtype时,返回类型.
当指定dtype时,返回类型转换后的数据,如果类型已经符合要求,
那么不做额外的复制,返回原对象.
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
>>> a = torch.ones(2,3)
>>> a.type()
'torch.FloatTensor'
>>> a.dtype
torch.float32
>>>
>>> a
tensor([[1., 1., 1.],
[1., 1., 1.]])
>>>
>>>
>>>
>>>
>>> b = a.type(dtype='torch.DoubleTensor')
>>> b
tensor([[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
>>> b.type()
'torch.DoubleTensor'
>>> b.dtype
torch.float64
>>>
>>>
>>>
>>> b = a.type(dtype='torch.cuda.DoubleTensor')
>>> b
tensor([[1., 1., 1.],
[1., 1., 1.]], device='cuda:0', dtype=torch.float64)
>>> b.type()
'torch.cuda.DoubleTensor'
>>> b.dtype
torch.float64
>>>
>>>
>>> a
tensor([[1., 1., 1.],
[1., 1., 1.]])
>>> a.type()
'torch.FloatTensor'
>>> a.dtype
torch.float32
>>>
>>>
>>>
>>>