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

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

在这里插入图片描述

原文及翻译:

to(*args, **kwargs)
方法:  to(*args, **kwargs)
    Moves and/or casts the parameters and buffers.
    将参数和缓冲在不同设备之间移动和/或类型转换.

    This can be called as
    该方法可以用以下方式来调用:

    to(device=None, dtype=None, non_blocking=False)
    调用方法: to(device=None, dtype=None, non_blocking=False)
    
    to(dtype, non_blocking=False)
    调用方法: to(dtype, non_blocking=False)

    to(tensor, non_blocking=False)
    调用方法: to(tensor, non_blocking=False)

    Its signature is similar to torch.Tensor.to(), but only accepts floating point desired 
    dtype s. In addition, this method will only cast the floating point parameters and 
    buffers to dtype (if given). The integral parameters and buffers will be moved device, 
    if that is given, but with dtypes unchanged. When non_blocking is set, it tries to 
    convert/move asynchronously with respect to the host if possible, e.g., moving CPU 
    Tensors with pinned memory to CUDA devices.
    该方法的签名格式非常类似于torch.Tensor.to(),区别在于该方法只接受浮点类型的dtype.此外,该方法
    只会对浮点类型的参数和缓冲进行类型(如果给出数据类型dtype)转换.对于整数类型的参数和缓冲,该方法
    不会进行数据类型dtype转换,但是会将参数和缓冲移动到指定设备(如果在该方法中给出了参数device).
    当non_blocking是True,尽可能地(相对于宿主主机)尝试异步式转换/移动,比如:将一个固定内
    存(pinned memory)的CPU张量转换到CUDA张量.

    See below for examples.
    查看以下的例子.


    Note  注意:
    This method modifies the module in-place.
    该方法是对模块进行原地(in-place)修改.


    Parameters  参数
            device (torch.device) – the desired device of the parameters and 
            buffers in this module
            device (torch.device类型) - 这个模块中的参数和缓冲所要转换到的那个设备
            dtype (torch.dtype) – the desired floating point type of the floating 
            point parameters and buffers in this module
            dtype (torch.dtype类型) – 这个模块中的浮点参数和缓冲所要转换成的浮点类型.
            tensor (torch.Tensor) – Tensor whose dtype and device are the desired dtype 
            and device for all parameters and buffers in this module
            tensor (torch.Tensor类型) – 该张量的数据类型dtype和设备类型device就是该模块中所有
            参数和缓冲所需要转换成的数据类型和设备类型.

    Returns  函数返回
        self  自身self
        
    Return type  返回类型
        Module  模块类型Module



    Example:  例子:

    >>> linear = nn.Linear(2, 2)
    >>> linear.weight
    Parameter containing:
    tensor([[ 0.1913, -0.3420],
            [-0.5113, -0.2325]])
    >>> linear.to(torch.double)
    Linear(in_features=2, out_features=2, bias=True)
    >>> linear.weight
    Parameter containing:
    tensor([[ 0.1913, -0.3420],
            [-0.5113, -0.2325]], dtype=torch.float64)
    >>> gpu1 = torch.device("cuda:1")
    >>> linear.to(gpu1, dtype=torch.half, non_blocking=True)
    Linear(in_features=2, out_features=2, bias=True)
    >>> linear.weight
    Parameter containing:
    tensor([[ 0.1914, -0.3420],
            [-0.5112, -0.2324]], dtype=torch.float16, device='cuda:1')
    >>> cpu = torch.device("cpu")
    >>> linear.to(cpu)
    Linear(in_features=2, out_features=2, bias=True)
    >>> linear.weight
    Parameter containing:
    tensor([[ 0.1914, -0.3420],
            [-0.5112, -0.2324]], dtype=torch.float16)

代码实验展示:

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 0x0000020539ACD330>
>>>
>>> import torch.nn as nn
>>> linear = nn.Linear(2, 2)
>>> linear.weight
Parameter containing:
tensor([[ 0.6812,  0.6901],
        [ 0.5486, -0.1540]], requires_grad=True)
>>> linear.weight.dtype
torch.float32
>>> linear.weight.type()
'torch.FloatTensor'
>>>
>>> linear.to(torch.double)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.6812,  0.6901],
        [ 0.5486, -0.1540]], dtype=torch.float64, requires_grad=True)
>>> linear.weight.dtype
torch.float64
>>> linear.weight.type()
'torch.DoubleTensor'
>>>
>>> gpu = torch.device("cuda:0")
>>> linear.to(gpu, dtype=torch.half, non_blocking=True)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.6812,  0.6899],
        [ 0.5483, -0.1541]], device='cuda:0', dtype=torch.float16,
       requires_grad=True)
>>> cpu = torch.device("cpu")
>>> linear.to(cpu, dtype=torch.float32)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.6812,  0.6899],
        [ 0.5483, -0.1541]], requires_grad=True)
>>> linear.weight.dtype
torch.float32
>>> linear.weight.type()
'torch.FloatTensor'
>>>
>>> linear.to(gpu, dtype=torch.half, non_blocking=True)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.6812,  0.6899],
        [ 0.5483, -0.1541]], device='cuda:0', dtype=torch.float16,
       requires_grad=True)
>>> linear.weight.dtype
torch.float16
>>> linear.weight.type()
'torch.cuda.HalfTensor'
>>> linear.to(cpu, dtype=torch.float32)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.6812,  0.6899],
        [ 0.5483, -0.1541]], requires_grad=True)
>>> linear.weight.dtype
torch.float32
>>> linear.weight.type()
'torch.FloatTensor'
>>>
>>>
>>>

实验代码展示:

import torch 
import torch.nn as nn
torch.manual_seed(seed=20200910)
class Model(torch.nn.Module):
    def __init__(self):
        super(Model,self).__init__()
        self.conv1=torch.nn.Sequential(  # 输入torch.Size([64, 1, 28, 28])
                torch.nn.Conv2d(1,64,kernel_size=3,stride=1,padding=1),
                torch.nn.ReLU(),  # 输出torch.Size([64, 64, 28, 28])
                torch.nn.Conv2d(64,128,kernel_size=3,stride=1,padding=1),  # 输出torch.Size([64, 128, 28, 28])
                torch.nn.ReLU(),
                torch.nn.MaxPool2d(stride=2,kernel_size=2)  # 输出torch.Size([64, 128, 14, 14])
        )

        self.dense=torch.nn.Sequential(  # 输入torch.Size([64, 14*14*128])
                    torch.nn.Linear(14*14*128,1024),  # 输出torch.Size([64, 1024])
                    torch.nn.ReLU(),
                    torch.nn.Dropout(p=0.5),
                    torch.nn.Linear(1024,10)  # 输出torch.Size([64, 10])        
        )
        self.layer4cxq1 = torch.nn.Conv2d(1,3,2,2)
        self.layer4cxq2 = torch.nn.ReLU()
        self.layer4cxq3 = torch.nn.MaxPool2d(stride=2,kernel_size=2)
        self.layer4cxq4 = torch.nn.Linear(14*14*128,1024)
        self.layer4cxq5 = torch.nn.Dropout(p=0.8)
        self.attribute4cxq = nn.Parameter(torch.tensor(20200910.0))
        self.attribute4lzq = nn.Parameter(torch.tensor([2.0,3.0,4.0,5.0]))    
        self.attribute4hh = nn.Parameter(torch.randn(3,4,5,6))
        self.attribute4wyf = nn.Parameter(torch.randn(7,8,9,10))

    def forward(self,x):  # torch.Size([64, 1, 28, 28])
        x = self.conv1(x)  # 输出torch.Size([64, 128, 14, 14])
        x = x.view(-1,14*14*128)  # torch.Size([64, 14*14*128])
        x = self.dense(x)  # 输出torch.Size([64, 10])
        return x

print('cuda(GPU)是否可用:',torch.cuda.is_available())
print('torch的版本:',torch.__version__)

model = Model() #.cuda()

print('调用方法to()之前'.center(100,'-'))
print(model.layer4cxq1.weight)
print(model.layer4cxq1.weight.dtype)
print(model.layer4cxq1.weight.type())


model.to(torch.double)
print('调用方法to(torch.double)之后'.center(100,'-'))
print(model.layer4cxq1.weight)
print(model.layer4cxq1.weight.dtype)
print(model.layer4cxq1.weight.type())


model.to(device='cuda:0', dtype=torch.half, non_blocking=True)
print('调用方法to(device=\'cuda:0\', dtype=torch.half, non_blocking=True)之后'.center(100,'-'))
print(model.layer4cxq1.weight)
print(model.layer4cxq1.weight.dtype)
print(model.layer4cxq1.weight.type())



print('调用方法to(device=\'cpu\', dtype=torch.float, non_blocking=True)之前'.center(100,'-'))
model.to(device='cpu', dtype=torch.float, non_blocking=True)
print(model.layer4cxq1.weight)
print(model.layer4cxq1.weight.dtype)
print(model.layer4cxq1.weight.type())


tensor_data_cpu = torch.randn(88,device='cpu',dtype=torch.float64)
print('调用方法to(tensor_data_cpu, non_blocking=False)之前'.center(100,'-'))
model.to(tensor_data_cpu, non_blocking=False)
print(model.layer4cxq1.weight)
print(model.layer4cxq1.weight.dtype)
print(model.layer4cxq1.weight.type())


tensor_data_cuda = torch.randn(88,device='cuda',dtype=torch.float64)
print('调用方法to(tensor_data_cuda, non_blocking=False)之前'.center(100,'-'))
model.to(tensor_data_cuda, non_blocking=False)
print(model.layer4cxq1.weight)
print(model.layer4cxq1.weight.dtype)
print(model.layer4cxq1.weight.type())



tensor_data_cuda_0 = torch.randn(88,device='cuda:0',dtype=torch.float64)
print('调用方法to(tensor_data_cuda_0, non_blocking=False)之前'.center(100,'-'))
model.to(tensor_data_cuda_0, non_blocking=False)
print(model.layer4cxq1.weight)
print(model.layer4cxq1.weight.dtype)
print(model.layer4cxq1.weight.type())

控制台输出结果:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

加载个人及系统配置文件用了 943 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq> conda activate ssd4pytorch1_2_0
(ssd4pytorch1_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>  & 'D:\Anaconda3\envs\ssd4pytorch1_2_0\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\lib\python\debugpy\launcher' '51425' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\test4cxq\test2.py'
cuda(GPU)是否可用: True
torch的版本: 1.2.0+cu92
---------------------------------------------调用方法to()之前---------------------------------------------
Parameter containing:
tensor([[[[ 0.2312, -0.3369],
          [-0.1310, -0.3258]]],


        [[[-0.3074,  0.2618],
          [ 0.1085,  0.0533]]],


        [[[-0.3613,  0.3689],
          [ 0.2623, -0.0366]]]], requires_grad=True)
torch.float32
torch.FloatTensor
---------------------------------------调用方法to(torch.double)之后---------------------------------------
Parameter containing:
tensor([[[[ 0.2312, -0.3369],
          [-0.1310, -0.3258]]],


        [[[-0.3074,  0.2618],
          [ 0.1085,  0.0533]]],


        [[[-0.3613,  0.3689],
          [ 0.2623, -0.0366]]]], dtype=torch.float64, requires_grad=True)
torch.float64
torch.DoubleTensor
-------------------调用方法to(device='cuda:0', dtype=torch.half, non_blocking=True)之后-------------------
Parameter containing:
tensor([[[[ 0.2312, -0.3369],
          [-0.1310, -0.3257]]],


        [[[-0.3074,  0.2617],
          [ 0.1085,  0.0533]]],


        [[[-0.3613,  0.3689],
          [ 0.2622, -0.0366]]]], device='cuda:0', dtype=torch.float16,
       requires_grad=True)
torch.float16
torch.cuda.HalfTensor
--------------------调用方法to(device='cpu', dtype=torch.float, non_blocking=True)之前--------------------  
Parameter containing:
tensor([[[[ 0.2312, -0.3369],
          [-0.1310, -0.3257]]],


        [[[-0.3074,  0.2617],
          [ 0.1085,  0.0533]]],


        [[[-0.3613,  0.3689],
          [ 0.2622, -0.0366]]]], requires_grad=True)
torch.float32
torch.FloatTensor
---------------------------调用方法to(tensor_data_cpu, non_blocking=False)之前----------------------------  
Parameter containing:
tensor([[[[ 0.2312, -0.3369],
          [-0.1310, -0.3257]]],


        [[[-0.3074,  0.2617],
          [ 0.1085,  0.0533]]],


        [[[-0.3613,  0.3689],
          [ 0.2622, -0.0366]]]], dtype=torch.float64, requires_grad=True)
torch.float64
torch.DoubleTensor
---------------------------调用方法to(tensor_data_cuda, non_blocking=False)之前---------------------------  
Parameter containing:
tensor([[[[ 0.2312, -0.3369],
          [-0.1310, -0.3257]]],


        [[[-0.3074,  0.2617],
          [ 0.1085,  0.0533]]],


        [[[-0.3613,  0.3689],
          [ 0.2622, -0.0366]]]], device='cuda:0', dtype=torch.float64,
       requires_grad=True)
torch.float64
torch.cuda.DoubleTensor
--------------------------调用方法to(tensor_data_cuda_0, non_blocking=False)之前--------------------------  
Parameter containing:
tensor([[[[ 0.2312, -0.3369],
          [-0.1310, -0.3257]]],


        [[[-0.3074,  0.2617],
          [ 0.1085,  0.0533]]],


        [[[-0.3613,  0.3689],
          [ 0.2622, -0.0366]]]], device='cuda:0', dtype=torch.float64,
       requires_grad=True)
torch.float64
torch.cuda.DoubleTensor
(ssd4pytorch1_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>

猜你喜欢

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