【翻译】torch.device的使用举例
生活随笔
收集整理的這篇文章主要介紹了
【翻译】torch.device的使用举例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考鏈接: class torch.device
原文及翻譯:
代碼實驗:
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.device('cuda:0') device(type='cuda', index=0) >>> >>> d = torch.device('cuda:0') >>> d device(type='cuda', index=0) >>> >>> torch.device('cpu') device(type='cpu') >>> d = torch.device('cpu') >>> d device(type='cpu') >>> >>> torch.device('cuda') # current cuda device device(type='cuda') >>> d = torch.device('cuda') # current cuda device >>> d device(type='cuda') >>> >>> # 通過一個字符串和設備序數來創建一個設備對象: >>> torch.device('cuda', 0) device(type='cuda', index=0) >>> d = torch.device('cuda', 0) >>> d device(type='cuda', index=0) >>> >>> torch.device('cpu', 0) device(type='cpu', index=0) >>> d = torch.device('cpu', 0) >>> >>> >>> >>> torch.device('cpu', 1) Traceback (most recent call last):File "<stdin>", line 1, in <module> RuntimeError: CPU device index must be -1 or zero, got 1 >>> torch.device('cpu', -1) Traceback (most recent call last):File "<stdin>", line 1, in <module> RuntimeError: Device index must not be negative >>> torch.device('cuda', 0) device(type='cuda', index=0) >>> torch.device('cuda', 1) device(type='cuda', index=1) >>> torch.device('cuda', 3) device(type='cuda', index=3) >>> >>> >>>原文及翻譯:
Note 注意: The torch.device argument in functions can generally be substituted with a string. This allows for fast prototyping of code. 函數的torch.device參數通常可以用一個字符串來替代.這有利于更快的代碼原型.>>> # Example of a function that takes in a torch.device >>> # 函數接收torch.device作為參數的例子. >>> cuda1 = torch.device('cuda:1') >>> torch.randn((2,3), device=cuda1)>>> # You can substitute the torch.device with a string >>> # 你可以用一個字符串來替代 torch.device >>> torch.randn((2,3), device='cuda:1')Note 注意: For legacy reasons, a device can be constructed via a single device ordinal, which is treated as a cuda device. This matches Tensor.get_device(), which returns an ordinal for cuda tensors and is not supported for cpu tensors. 由于遺留的歷史原因,我們可以通過單個設備序數來創建一個設備,這個序數被用來作為 一個cuda設備的序數.這個序數和Tensor.get_device()匹配,Tensor.get_device() 函數返回cuda張量的序數,并且這個序數不支持cpu張量.>>> torch.device(1) device(type='cuda', index=1)Note 注意:Methods which take a device will generally accept a (properly formatted) string or (legacy) integer device ordinal, i.e. the following are all equivalent: 接收一個device對象的方法通常也可以接收一個(正確格式化的)字符串或者 一個表示設備序數的(遺留的方式)整數,即以下的方式是等效的:>>> torch.randn((2,3), device=torch.device('cuda:1')) >>> torch.randn((2,3), device='cuda:1') >>> torch.randn((2,3), device=1) # legacy # 遺留的使用方式代碼實驗展示:
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 0x0000024D1851D330> >>> >>> torch.device('cuda:1') device(type='cuda', index=1) >>> cuda0 = torch.device('cuda:0') >>> cuda0 device(type='cuda', index=0) >>> cuda1 = torch.device('cuda:1') >>> cuda1 device(type='cuda', index=1) >>> torch.randn((2,3), device=cuda0) tensor([[-0.9908, 0.5920, -0.0895],[ 0.7582, 0.0068, -3.2594]], device='cuda:0') >>> torch.randn((2,3), device=cuda1) Traceback (most recent call last):File "<stdin>", line 1, in <module> RuntimeError: CUDA error: invalid device ordinal >>> # 報錯的原因是當前機器上只有一個GPU >>> torch.randn((2,3), device='cuda:1') Traceback (most recent call last):File "<stdin>", line 1, in <module> RuntimeError: CUDA error: invalid device ordinal >>> torch.randn((2,3), device='cuda:0') tensor([[ 1.2275, -0.4505, 0.2995],[ 0.6775, 0.2833, 1.3016]], device='cuda:0') >>> >>> torch.device(1) device(type='cuda', index=1) >>> torch.device(0) device(type='cuda', index=0) >>> >>> data = torch.randn((2,3), device='cuda:0') >>> data tensor([[ 0.8317, -0.8718, -1.9394],[-0.7548, 0.9575, 0.0592]], device='cuda:0') >>> data.get_device() 0 >>> data = torch.randn((2,3)) >>> data tensor([[ 0.2824, -0.3715, 0.9088],[-1.7601, -0.1806, 2.0937]]) >>> data.get_device() -1 >>> data = torch.randn((2,3), device='cpu') >>> data tensor([[ 1.0406, -1.7651, 1.1216],[ 0.8440, 0.1783, 0.6859]]) >>> data.get_device() -1 >>> torch.device(1) device(type='cuda', index=1) >>> torch.device(0) device(type='cuda', index=0) >>> torch.device(-1) Traceback (most recent call last):File "<stdin>", line 1, in <module> RuntimeError: Device index must not be negative >>> torch.randn((2,3), device=torch.device('cuda:1')) Traceback (most recent call last):File "<stdin>", line 1, in <module> RuntimeError: CUDA error: invalid device ordinal >>> torch.randn((2,3), device=torch.device('cuda:0')) tensor([[ 0.9059, 1.5271, -0.0798],[ 0.0769, -0.3586, -1.9597]], device='cuda:0') >>> torch.randn((2,3), device='cuda:1') Traceback (most recent call last):File "<stdin>", line 1, in <module> RuntimeError: CUDA error: invalid device ordinal >>> torch.randn((2,3), device='cuda:0') tensor([[ 0.3807, 0.7194, 1.1678],[-1.1230, 0.6926, 0.0385]], device='cuda:0') >>> torch.randn((2,3), device=1) # legacy Traceback (most recent call last):File "<stdin>", line 1, in <module> RuntimeError: CUDA error: invalid device ordinal >>> torch.randn((2,3), device=0) # legacy tensor([[ 0.6609, -1.1344, 0.2916],[-0.6726, -0.4537, -0.9022]], device='cuda:0') >>> torch.randn((2,3), device=-1) # legacy Traceback (most recent call last):File "<stdin>", line 1, in <module> RuntimeError: Device index must not be negative >>> >>> >>>代碼實驗:
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.cuda.is_available() True >>> print(torch.cuda.is_available()) True >>> print(torch.__version__) 1.2.0+cu92 >>> torch.manual_seed(seed=20200910) <torch._C.Generator object at 0x000001CA8034D330> >>> >>> device = (torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')) >>> device device(type='cuda') >>> device_1 = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu') >>> device_1 device(type='cuda') >>> torch.device('cpu') device(type='cpu') >>> torch.device('cuda') device(type='cuda') >>> torch.device('cuda0') Traceback (most recent call last):File "<stdin>", line 1, in <module> RuntimeError: Expected one of cpu, cuda, mkldnn, opengl, opencl, ideep, hip, msnpu device type at start of device string: cuda0 >>> torch.device('cuda:0') device(type='cuda', index=0) >>> torch.device('cuda:1') device(type='cuda', index=1) >>> torch.device('cuda:-1') Traceback (most recent call last):File "<stdin>", line 1, in <module> RuntimeError: Device index must be non-negative, got -1 >>> torch.device('cuda:3') device(type='cuda', index=3) >>> >>> >>>
代碼實驗:
總結
以上是生活随笔為你收集整理的【翻译】torch.device的使用举例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 银行面试之MySQL数据库
- 下一篇: Angular安装命令