参考链接: 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的使用举例相关推荐

  1. pytorch torch.device类(表示在其上或将要分配torch.Tensor的设备)

    from https://pytorch.org/docs/1.1.0/tensor_attributes.html?highlight=torch%20device#torch.torch.devi ...

  2. pytorch torch.Tensor.new_ones()(返回一个与size大小相同的用1填充的张量。 默认返回的Tensor具有与此张量相同的torch.dtype和torch.device)

    from https://pytorch.org/docs/1.1.0/tensors.html?highlight=new_ones#torch.Tensor.new_ones new_ones(s ...

  3. pytorch(2)Tensor创建和获取tensor的size信息、torch.dtype、torch.device、torch.layout

    获取tensor的size信息 >>> import torch >>> from torch.autograd import Variable >>& ...

  4. torch.device

    问题: device = torch.device("cuda" if torch.cuda.is_available() else "cpu") Attrib ...

  5. torch.device()

    即 创建了device这个(GPU/CPU) 设备,然后将model(data)分配到device上,使用device对model(data)进行训练学习.

  6. PyTorch笔记: GPU上训练的模型加载到CPU/错误处理Attempting to deserialize object on a CUDA device but torch.cuda.is_a

    我之前在GPU上训练了一个模型,同时把模型的参数记录在resnet18_ultimate.pkl上 在本地的CPU上,我想把参数加载,于是一开始我是这么写代码的: import torch impor ...

  7. RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count() is 1.

    成功解决 RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count() i ...

  8. RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is

    pytorch,pycharm下做项目遇到的一个小问题,报了如下错误: RuntimeError: Attempting to deserialize object on a CUDA device ...

  9. size() → torch.Size使用举例

    参考链接: size() → torch.Size 实验代码举例: Microsoft Windows [版本 10.0.18363.1256] (c) 2019 Microsoft Corporat ...

  10. Attempting to deserialize object on a CUDA device but torch.cuda.is_available()的可能原因

    RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is Fal ...

最新文章

  1. C#3.0-新增加功能浅析(1)
  2. sort对结构体排序
  3. Firefox Test Pilot 计划正式关闭
  4. (17)css3新增背景属性
  5. 桥梁模式 :Bridge(转自Terrylee)
  6. C语言 几种排序方法(冒泡、选择、插入、归并、快速)
  7. su组件在什么窗口_草图大师SketchUp(SU)快捷键
  8. BZOJ1370 [Baltic2003]Gang团伙
  9. 腾讯专利仅次谷歌;​苹果或将 iPhone 订单转给和硕;​Uber 接受比特币支付 | 极客头条...
  10. 51Nod-1015 水仙花数【进制+查表搜索】
  11. Atitit.自定义存储引擎的接口设计 api 标准化 attilax 总结  mysql
  12. paip.mysql 批量kill 连接.
  13. MacOS Ventura 13.0 Beta4 (22A5311f) 带 OC 0.8.3 / Cl 5148 / PE 三分区原版黑苹果镜像
  14. linux7怎么关闭更新,如何让centos7关闭yum自动更新系统
  15. 科研方法-X_LAB-方法总结和实践记录
  16. Flash控件使用参考手册
  17. poj入门水题整理1--按刷题顺序解释
  18. 递归算法的时间&空间复杂度!
  19. GAN-生成对抗网络(Pytorch)合集(2)--pixtopix-CycleGAN
  20. 基于stm32f103的磁悬浮

热门文章

  1. 维修计算机小能人,电脑小能人作文「精选」
  2. 推荐一款网络测试APP-网络百宝箱
  3. 操作系统--虚拟内存
  4. BBU+RRU基本介绍
  5. Android email中无法登陆gmail邮箱 M
  6. win10计算机管理如何分区,如何给win10磁盘分区?教你一招快速给win10磁盘分区的方法...
  7. 网站开发进阶(二十四)HTML颜色代码表
  8. 寻租——乞丐没有白拿施舍
  9. 【已解决】点击Import Package没有Environment与Effects等问题(从官网下载Environment等相关资源包)
  10. json数组排序,深拷贝,浅拷贝,删除,增加,筛选,