torch.Tensor.type_as(tensor)的使用举例

参考链接: type_as(tensor) → Tensor
参考链接: torch.Tensor.type()使用举例

在这里插入图片描述
原文及翻译:

type_as(tensor) → Tensor
方法: type_as(tensor) → 返回Tensor类型
    Returns this tensor cast to the type of the given tensor.
    根据给定的张量tensor的类型,按照这个类型返回当前张量.
    
    This is a no-op if the tensor is already of the correct type. 
    This is equivalent to self.type(tensor.type())
    如果当前张量的类型已经满足要求,那么不做任何操作.
    该方法等效于调用self.type(tensor.type())
    
    Parameters  参数
        tensor (Tensor) – the tensor which has the desired type
        tensor (Tensor类型) – 该参数tensor给定了所需要的张量类型.

代码实验展示:

Microsoft Windows [版本 10.0.18363.1316]
(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 0x000001610C25D330>
>>>
>>> a = torch.randn(2,4,dtype=torch.float64)
>>> b = torch.randn(3,3)
>>> c = torch.randn(1,2)
>>> a, a.type()
(tensor([[ 0.2824, -0.3715,  0.9088, -1.7601],
        [-0.1806,  2.0937,  1.0406, -1.7651]], dtype=torch.float64), 'torch.DoubleTensor')
>>> b, b.type()
(tensor([[ 1.1216,  0.8440,  0.1783],
        [ 0.6859, -1.5942, -0.2006],
        [-0.4050, -0.5556,  0.9571]]), 'torch.FloatTensor')
>>> c, c.type()
(tensor([[ 0.7435, -0.2974]]), 'torch.FloatTensor')
>>>
>>> a.dtype, b.dtype, c.dtype
(torch.float64, torch.float32, torch.float32)
>>>
>>> id(a), id(b), id(c)
(1516376143496, 1518218770856, 1518218771016)
>>>
>>> b1 = b.type_as(c)
>>> b2 = b.type_as(a)
>>>
>>> a
tensor([[ 0.2824, -0.3715,  0.9088, -1.7601],
        [-0.1806,  2.0937,  1.0406, -1.7651]], dtype=torch.float64)
>>> b
tensor([[ 1.1216,  0.8440,  0.1783],
        [ 0.6859, -1.5942, -0.2006],
        [-0.4050, -0.5556,  0.9571]])
>>> c
tensor([[ 0.7435, -0.2974]])
>>>
>>> b1
tensor([[ 1.1216,  0.8440,  0.1783],
        [ 0.6859, -1.5942, -0.2006],
        [-0.4050, -0.5556,  0.9571]])
>>> b2
tensor([[ 1.1216,  0.8440,  0.1783],
        [ 0.6859, -1.5942, -0.2006],
        [-0.4050, -0.5556,  0.9571]], dtype=torch.float64)
>>> id(a), id(b), id(c)
(1516376143496, 1518218770856, 1518218771016)
>>> id(b) == id(b1)
True
>>> id(b) == id(b2)
False
>>> b.dtype, b1.dtype, b2.dtype,
(torch.float32, torch.float32, torch.float64)
>>> id(b), id(b1), id(b2),
(1518218770856, 1518218770856, 1516376185896)
>>>
>>> b is b1
True
>>> b is b2
False
>>>
>>>
>>>

代码实验展示:

Microsoft Windows [版本 10.0.18363.1316]
(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 0x00000156593CD330>
>>>
>>> a = torch.randn(2,4,dtype=torch.float64)
>>> b = torch.randn(3,3)
>>> c = torch.randn(1,2)
>>> a
tensor([[ 0.2824, -0.3715,  0.9088, -1.7601],
        [-0.1806,  2.0937,  1.0406, -1.7651]], dtype=torch.float64)
>>> b
tensor([[ 1.1216,  0.8440,  0.1783],
        [ 0.6859, -1.5942, -0.2006],
        [-0.4050, -0.5556,  0.9571]])
>>> c
tensor([[ 0.7435, -0.2974]])
>>> a, a.type(), a.dtype
(tensor([[ 0.2824, -0.3715,  0.9088, -1.7601],
        [-0.1806,  2.0937,  1.0406, -1.7651]], dtype=torch.float64), 'torch.DoubleTensor', torch.float64)
>>>
>>> b, b.type(), b.dtype
(tensor([[ 1.1216,  0.8440,  0.1783],
        [ 0.6859, -1.5942, -0.2006],
        [-0.4050, -0.5556,  0.9571]]), 'torch.FloatTensor', torch.float32)
>>>
>>> c, c.type(), c.dtype
(tensor([[ 0.7435, -0.2974]]), 'torch.FloatTensor', torch.float32)
>>>
>>> a.dtype, b.dtype, c.dtype
(torch.float64, torch.float32, torch.float32)
>>>
>>> a.type(), b.type(), c.type()
('torch.DoubleTensor', 'torch.FloatTensor', 'torch.FloatTensor')
>>>
>>> id(a), id(b), id(c)
(1470425384424, 1470122424744, 1470122424904)
>>>
>>> b1 = b.type_as(c)
>>> b2 = b.type_as(a)
>>> a
tensor([[ 0.2824, -0.3715,  0.9088, -1.7601],
        [-0.1806,  2.0937,  1.0406, -1.7651]], dtype=torch.float64)
>>> b
tensor([[ 1.1216,  0.8440,  0.1783],
        [ 0.6859, -1.5942, -0.2006],
        [-0.4050, -0.5556,  0.9571]])
>>> c
tensor([[ 0.7435, -0.2974]])
>>> b1
tensor([[ 1.1216,  0.8440,  0.1783],
        [ 0.6859, -1.5942, -0.2006],
        [-0.4050, -0.5556,  0.9571]])
>>> b2
tensor([[ 1.1216,  0.8440,  0.1783],
        [ 0.6859, -1.5942, -0.2006],
        [-0.4050, -0.5556,  0.9571]], dtype=torch.float64)
>>>
>>> id(a), id(b), id(c)
(1470425384424, 1470122424744, 1470122424904)
>>>
>>> id(b), id(b1), id(b2)
(1470122424744, 1470122424744, 1470425420408)
>>>
>>> id(b) == id(b1)
True
>>> id(b) == id(b2)
False
>>>
>>> b is b1
True
>>> b is b2
False
>>> b.dtype, b1.dtype, b2.dtype
(torch.float32, torch.float32, torch.float64)
>>> b.type(), b1.type(), b2.type()
('torch.FloatTensor', 'torch.FloatTensor', 'torch.DoubleTensor')
>>>
>>>
>>>

猜你喜欢

转载自blog.csdn.net/m0_46653437/article/details/112988632