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是一个对象,表示在其上或将要分配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 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’构造的torch.Tensor等效于’cuda:X’,其中X是torch.cuda.current_device()的结果。

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
可以通过字符串或通过字符串和设备序号构造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.
函数中的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()匹配,后者为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:
使用设备的方法通常会接受(正确格式化的)字符串或(旧式)整数设备序数,即以下所有等效方法:
>>> torch.randn((2,3), device=torch.device('cuda:1'))
>>> torch.randn((2,3), device='cuda:1')
>>> torch.randn((2,3), device=1)  # legacy

你可能感兴趣的:(Pytorch)