日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【翻译】torch.device的使用举例

發布時間:2023/12/16 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【翻译】torch.device的使用举例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考鏈接: class torch.device


原文及翻譯:

torch.device torch.device欄目class torch.device torch.device 類型A torch.device is an object representing the device on which a torch.Tensor is or will be allocated. torch.device的一個實例是一個對象,該對象代表的是張量torch.Tensor所在 的設備或將要分配到的設備.The torch.device contains a device type ('cpu' or 'cuda') and optional device ordinal for the device type. If the device ordinal is not present, this object will always represent the current device for the device type, even after torch.cuda.set_device() is called; e.g., a torch.Tensor constructed with device 'cuda' is equivalent to 'cuda:X' where X is the result of torch.cuda.current_device(). torch.device包含了一個設備類型('cpu' 或者 'cuda'),以及該設備類型的可選設備序數.如果該設備序數不存在,那么這個對象所代表的將總是該設備類型的當前設備,盡管 已經調用了torch.cuda.set_device()函數;例如,使用設備'cuda'來創建一個 torch.Tensor 對象等效于使用設備'cuda:X'來創建torch.Tensor對象,其中X是函數 torch.cuda.current_device()的返回結果.A torch.Tensor’s device can be accessed via the Tensor.device property. 通過使用張量torch.Tensor的屬性Tensor.device 就可以訪問該torch.Tensor張量 所在的設備.A torch.device can be constructed via a string or via a string and device ordinal 通過一個字符串或者一個字符串和設備序數就可以創建一個torch.device對象.Via a string: 通過一個字符串來創建: >>> torch.device('cuda:0') device(type='cuda', index=0)>>> torch.device('cpu') device(type='cpu')>>> torch.device('cuda') # current cuda device # 當前cuda設備 device(type='cuda')Via a string and device ordinal: 通過一個字符串和設備序數來創建一個設備對象: >>> torch.device('cuda', 0) device(type='cuda', index=0)>>> torch.device('cpu', 0) device(type='cpu', index=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.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) >>> >>> >>>



代碼實驗:

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 0x000001AF9D28D330> >>> >>> torch.randn((2,3), device='cuda') tensor([[-0.9908, 0.5920, -0.0895],[ 0.7582, 0.0068, -3.2594]], device='cuda:0') >>> torch.randn((2,3), 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.randn((2,3), device='cuda:0') tensor([[ 1.2275, -0.4505, 0.2995],[ 0.6775, 0.2833, 1.3016]], device='cuda:0') >>> torch.randn((2,3), device='cpu') tensor([[ 0.2824, -0.3715, 0.9088],[-1.7601, -0.1806, 2.0937]]) >>> torch.randn((2,3)) tensor([[ 1.0406, -1.7651, 1.1216],[ 0.8440, 0.1783, 0.6859]]) >>> >>> >>>

總結

以上是生活随笔為你收集整理的【翻译】torch.device的使用举例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。