pytorch初学

(pytorch_gpu) D:pytorch-text>python
Python 3.7.9 (default, Aug 31 2020, 17:10:11) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch #在pycharm中打开的虚拟环境的终端
>>> a = torch.Tensor(2,2)     #默认为torch.FloatTensor
>>> a
tensor([[3.4732e+19, 4.5914e-41],
[3.4732e+19, 4.5914e-41]])
>>> a.type()
'torch.FloatTensor'
>>> b = a.double()    #可以将FloatTensor转化成DoubleTensor
>>> b
tensor([[3.4732e+19, 4.5914e-41],
[3.4732e+19, 4.5914e-41]], dtype=torch.float64)
>>> b.type()
'torch.DoubleTensor'
>>> a.type()
'torch.FloatTensor'
>>> c = a.type(torch.DoubleTensor)
>>> c
tensor([[3.4732e+19, 4.5914e-41],
[3.4732e+19, 4.5914e-41]], dtype=torch.float64)
>>> c.type
<built-in method type of Tensor object at 0x000001BA3A5929F8>
>>> c.type()
'torch.DoubleTensor'
>>> a.type()
'torch.FloatTensor'
>>> a.type_as(b)
tensor([[3.4732e+19, 4.5914e-41],
[3.4732e+19, 4.5914e-41]], dtype=torch.float64)
>>> a
tensor([[3.4732e+19, 4.5914e-41],
[3.4732e+19, 4.5914e-41]])
>>> a.type()
'torch.FloatTensor'
>>> r = a.type_as(b) #将a转化成b的类型并赋值给r
>>> r.type()
'torch.DoubleTensor'
共有四种方法转化Tensor 1.直接命名 2, b.double()    3.  b.type(torch.doubleTensor)

4.r = b.type_as(d)将b转化成d的格式并赋值给r

原文地址:https://www.cnblogs.com/kelvin-liu/p/14292648.html