torch.Tensor.to(*args, **kwargs)使用举例

参考链接: torch.Tensor.to(*args, **kwargs)

在这里插入图片描述

在这里插入图片描述

原文及翻译:

to(*args, **kwargs) → Tensor
方法:to(*args, **kwargs) 返回值的类型是Tensor
    Performs Tensor dtype and/or device conversion. A torch.dtype and torch.device are 
    inferred from the arguments of self.to(*args, **kwargs).
    该方法执行的是将一个张量进行数据类型和/或设备类型转换. 张量的数据类型torch.dtype和
    张量的设备类型torch.device是从self.to(*args, **kwargs)的参数中推断出来的.

    Note  注意:
    If the self Tensor already has the correct torch.dtype and torch.device, then self 
    is returned. Otherwise, the returned tensor is a copy of self with the desired 
    torch.dtype and torch.device.
    如果当前的self张量的数据类型torch.dtype和设备类型torch.device已经满足转换后的要求,
    那么函数直接返回self.否则,返回的张量是所需数据类型(torch.dtype)和所需设备类
    型(torch.device)的self张量数据的拷贝.

    Here are the ways to call to:
    以下是调用该方法的几种方式:

    to(dtype, non_blocking=False, copy=False) → Tensor
    方法: to(dtype, non_blocking=False, copy=False) 返回的类型是Tensor

        Returns a Tensor with the specified dtype
        返回指定数据类型(dtype)的张量

    to(device=None, dtype=None, non_blocking=False, copy=False) → Tensor
	方法: to(device=None, dtype=None, non_blocking=False, copy=False) 返回的类型是Tensor
        Returns a Tensor with the specified device and (optional) dtype. 
        If dtype is None it is inferred to be self.dtype. When non_blocking, 
        tries to convert asynchronously with respect to the host if possible, 
        e.g., converting a CPU Tensor with pinned memory to a CUDA Tensor. 
        When copy is set, a new Tensor is created even when the Tensor already 
        matches the desired conversion.
        返回具有特定设备类型和(可选)特定数据类型的张量Tensor.
        如果传入的参数dtype是None,那么它的取值是由self.dtype推断出来的.
        当non_blocking是True,尽可能地(相对于宿主主机)尝试异步式转换,比如:
        将一个固定内存(pinned memory)的CPU张量转换到CUDA张量.
        当设置了copy参数时,无论当前张量的数据类型和设备类型是否已经满足当前转换要求,
        都将创建一个新的张量.

    to(other, non_blocking=False, copy=False) → Tensor
    方法: to(other, non_blocking=False, copy=False) 返回的类型是Tensor
        Returns a Tensor with same torch.dtype and torch.device as the Tensor other. 
        When non_blocking, tries to convert asynchronously with respect to the host 
        if possible, e.g., converting a CPU Tensor with pinned memory to a CUDA Tensor. 
        When copy is set, a new Tensor is created even when the Tensor already 
        matches the desired conversion.
        返回一个张量,并且该张量的数据类型(torch.dtype)和设备类型(torch.device)
        和other张量的类型相同.non_blocking是True,尽可能地(相对于宿主主机)尝试异步式转换,
        比如:将一个固定内存(pinned memory)的CPU张量转换到CUDA张量.当设置了copy参数时,无论
        当前张量的数据类型和设备类型是否已经满足当前转换要求,都将创建一个新的张量.




    Example:  例子:

    >>> tensor = torch.randn(2, 2)  # Initially dtype=float32, device=cpu  
    >>> # 设置初始的数据类型和设备类型,分别是: dtype=float32, device=cpu  
    >>> tensor.to(torch.float64)
    tensor([[-0.5044,  0.0005],
            [ 0.3310, -0.0584]], dtype=torch.float64)

    >>> cuda0 = torch.device('cuda:0')
    >>> tensor.to(cuda0)
    tensor([[-0.5044,  0.0005],
            [ 0.3310, -0.0584]], device='cuda:0')

    >>> tensor.to(cuda0, dtype=torch.float64)
    tensor([[-0.5044,  0.0005],
            [ 0.3310, -0.0584]], dtype=torch.float64, device='cuda:0')

    >>> other = torch.randn((), dtype=torch.float64, device=cuda0)
    >>> tensor.to(other, non_blocking=True)
    tensor([[-0.5044,  0.0005],
            [ 0.3310, -0.0584]], dtype=torch.float64, device='cuda:0')

代码实验展示:

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 0x00000238001BD330>
>>>
>>> a = torch.randn(2, 2)  # Initially dtype=float32, device=cpu
>>> a
tensor([[ 0.2824, -0.3715],
        [ 0.9088, -1.7601]])
>>> a.dtype
torch.float32
>>> a.type()
'torch.FloatTensor'
>>> b = a.to(torch.float64)
>>> a
tensor([[ 0.2824, -0.3715],
        [ 0.9088, -1.7601]])
>>> b
tensor([[ 0.2824, -0.3715],
        [ 0.9088, -1.7601]], dtype=torch.float64)
>>> a.dtype
torch.float32
>>> b.dtype
torch.float64
>>>
>>>
>>>
>>> cuda0 = torch.device('cuda:0')
>>> c = a.to(cuda0)
>>> c
tensor([[ 0.2824, -0.3715],
        [ 0.9088, -1.7601]], device='cuda:0')
>>> c.dtype
torch.float32
>>> c.type()
'torch.cuda.FloatTensor'
>>>
>>>
>>> d = a.to(cuda0, dtype=torch.float64)
>>> d
tensor([[ 0.2824, -0.3715],
        [ 0.9088, -1.7601]], device='cuda:0', dtype=torch.float64)
>>> a
tensor([[ 0.2824, -0.3715],
        [ 0.9088, -1.7601]])
>>> a.dtype, a.type()
(torch.float32, 'torch.FloatTensor')
>>> d.dtype, d.type()
(torch.float64, 'torch.cuda.DoubleTensor')
>>>
>>> other = torch.randn((), dtype=torch.float64, device=cuda0)
>>> other
tensor(0.1078, device='cuda:0', dtype=torch.float64)
>>> other.shape
torch.Size([])
>>> other.item()
0.10781455569052101
>>> f = a.to(other, non_blocking=True)
>>> f
tensor([[ 0.2824, -0.3715],
        [ 0.9088, -1.7601]], device='cuda:0', dtype=torch.float64)
>>> a.dtype, a.type()
(torch.float32, 'torch.FloatTensor')
>>> f.dtype, f.type()
(torch.float64, 'torch.cuda.DoubleTensor')
>>>
>>> other = torch.randn((3,5), dtype=torch.float64, device=cuda0)
>>> other
tensor([[ 0.4757, -1.0417, -1.6356, -0.1920, -1.5530],
        [ 0.9415, -1.7936, -0.3805, -0.6078, -2.3351],
        [-0.2019,  1.0919, -0.0553,  1.1729, -0.1473]], device='cuda:0',
       dtype=torch.float64)
>>> a
tensor([[ 0.2824, -0.3715],
        [ 0.9088, -1.7601]])
>>> g = a.to(other, non_blocking=True)
>>> g
tensor([[ 0.2824, -0.3715],
        [ 0.9088, -1.7601]], device='cuda:0', dtype=torch.float64)
>>> a.dtype, a.type()
(torch.float32, 'torch.FloatTensor')
>>> g.dtype, g.type()
(torch.float64, 'torch.cuda.DoubleTensor')
>>>
>>>
>>>

猜你喜欢

转载自blog.csdn.net/m0_46653437/article/details/112727204
今日推荐