pytorch torch.device类(表示在其上或将要分配torch.Tensor的设备)
from https://pytorch.org/docs/1.1.0/tensor_attributes.html?highlight=torch%20device#torch.torch.device
CLASS torch.device
A torch.device is an object representing the device on which a torch.Tensor is or will be allocated.
torch.device是一個對象,表示在其上或?qū)⒁峙鋞orch.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 represents the current device for the device type; 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”)和該設備類型的可選設備序號。
如果設備序號不存在,則代表設備類型的當前設備;
例如 用設備’cuda’構(gòu)造的torch.Tensor等效于’cuda:X’,其中X是torch.cuda.current_device()的結(jié)果。
A torch.Tensor’s device can be accessed via the Tensor.device property.
可以通過Tensor.device屬性訪問torch.Tensor的設備。
A torch.device can be constructed via a string or via a string and device ordinal
可以通過字符串或通過字符串和設備序號構(gòu)造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 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)NOTE
The torch.device argument in functions can generally be substituted with a string. This allows for fast prototyping of code.
函數(shù)中的torch.device參數(shù)通常可以用字符串替換。
這樣可以快速編寫代碼原型。
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設備)構(gòu)造設備。 這與Tensor.get_device()匹配,后者為cuda張量返回序數(shù),而cpu張量不支持該序數(shù)。 >>> 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: 使用設備的方法通常會接受(正確格式化的)字符串或(舊式)整數(shù)設備序數(shù),即以下所有等效方法: >>> torch.randn((2,3), device=torch.device('cuda:1')) >>> torch.randn((2,3), device='cuda:1') >>> torch.randn((2,3), device=1) # legacy 與50位技術專家面對面20年技術見證,附贈技術全景圖總結(jié)
以上是生活随笔為你收集整理的pytorch torch.device类(表示在其上或将要分配torch.Tensor的设备)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pytorch torch.Tensor
- 下一篇: 【中级软考】MTBF是什么?(平均故障间