天天看點

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

參考連結: to(*args, **kwargs)

torch.nn.Module.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>
           

繼續閱讀