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

2023-11-09

参考链接: 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]])
>>>
>>>
>>>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【翻译】torch.device的使用举例 的相关文章

  • 原神--原学入门--元素结晶盾盾值计算

    由于在角色的元素精通面板是可以看到分为了四类加成 其中一项便是对于结晶反应得到的元素护盾的增幅 于是便开始着手学习相关的计算 便览众多说法 最终都是归一为了如同文章 数据讨论 结晶反应护盾值计算相关 NGA玩家社区 的说法一样的计算方法 1
  • 2020年第十一届蓝桥杯javaB组省赛

    文章目录 试题 A 门牌制作 试题 B 寻找 2020 试题 C 蛇形填数 试题 D 七段码 试题 E 排序 试题 F 成绩分析 试题 G 单词分析 试题 H 数字三角形 试题 I 子串分值和 试题 J 装饰珠 以下均为个人想法和解题思路
  • c/c++ 智能指针 weak_ptr 使用

    智能指针 weak ptr 使用 weak ptr用途 1 解决空悬指针问题 2 解决循环引用问题 weak ptr特点 没有 操作和 gt 操作 weak ptr是不控制所指对象生存周期的智能指针 它指向由一个shared ptr管理的对
  • java版 SpringCloud 之目前得前端框架都有哪些?

    1 AngularJS Angular JS 是一个有Google维护的开源前端web应用程序框架 它最初由Brat Tech LLC的Misko Hevery于2009年开发出来 Angular JS是一个模型 视图 控制器 MVC 模式
  • libdl.so的用途

    通过对某些bin或者so执行ldd 可以看到他们运行前需要连接的共享库 但是有时候会看到有2个与dl相关的so ld linux so和libdl so 这时候我就有些困惑了 他们分别做了什么工作呢 原来 ld linux so的工作是在程
  • monkeyrunner的基本

    导入我们需要用到的包和类并且起别名 import sys from com android monkeyrunner import MonkeyRunner as mr from com android monkeyrunner impor
  • 渗透测试工程师面试题大全(三)

    渗透测试工程师面试题大全 三 from backlion大佬 整理 101 什么是 WebShell WebShell 就是以 asp php jsp 或者 cgi 等网页文件形式存在的 种命令执行环境 也可以将其称做为 种网页后门 黑客在
  • VLAN间路由及路由器下连接交换机的配置方法

    方法一 建议路由器下连接三层交换机 例如Cisco3650 大体思路是 三层交换机与路由器之间建立OSPF邻居 将交换机上的Vlan三层网段宣告出来 Vlan中主机的默认网关设置为Vlan三层网段实现Vlan间互通以及对外通信 其中 Vla
  • 关于火星坐标系统

    转载 关于火星坐标系统 2011 09 08 23 11 57 分类 默认分类 字号 订阅 偶然得知中国有一种火星坐标系统 其原理是这样的 保密局开发了一个系统 能将实际的坐标转换成虚拟的坐标 所有在中国销售的数字地图必须使用这个系统进行坐
  • 显示Hello World

    C 语言 include iostream using namespace std int main cout lt lt Hello World lt
  • 没有编程基础,可以自学Python吗?

    可以 Python是一门简单优雅的计算机程序设计语言 自身的特点决定了它是一门易学难精的语言 所以对于零基础学员而言 只要肯努力 学习是没有问题的 下面说说相比于C语言 Java语言 Python的特点 Python语法简单 代码可读性高
  • 性能监控-influxDB+grafana+jmeter展示测试结果

    InfluxDB 是 Go 语言编写的时间序列数据库 用于处理海量写入与负载查询 涉及大量时间戳数据的任何用例 包括 DevOps 监控 应用程序指标等 我认为 InfluxDB 最大的特点在于可以按照时间序列面对海量数据时候的高性能读写能
  • 高数【求导】--猴博士爱讲课

    第三课 求导 1 5 照公式求导 常见的求导 2 5 隐函数求导 例 1 若 y y
  • spring Aop嵌套调用的解决办法

    众所周知 Spring AOP在同一个类里自身方法相互调用时是无法拦截的 问题示例代码 public String say String a System out println say a a say2 a return a a publ

随机推荐

  • px、em、rem、rpx 用法 与 区别

    这篇文章记录前端 包含小程序 开发中常用到的几个单位 px em rem rpx 的区别和用法 px px像素 Pixel 相对长度单位 像素px是相对于显示器屏幕分辨率而言的 PX特点 1 IE无法调整那些使用px作为单位的字体大小 2
  • 消息中间件 RocketMQ 源码解析:Message拉取&消费(上)

    摘要 原创出处 http www iocoder cn RocketMQ message pull and consume first 芋道源码 欢迎转载 保留摘要 谢谢 本文主要基于 RocketMQ 4 0 x 正式版 1 概述 2 C
  • std::vector push_back报错Access violation

    C C code 1 2 3 4 5 6 7 8 9
  • 简单聊聊FPGA的一些参数

    笔者 E林1010 在上一篇中 我们已经知道了 FPGA的几个主流厂家和其中Intel家族中FPGA的系列的分类 上一篇文章链接 https mp weixin qq com s 1YufdRZ3Kvvk1znDGu69Og 本文微信公众号
  • word无法显示图片的问题终于搞定!oh yeah!

    我的word中的图片只显示一个方框 这个问题困扰我有一段时间了 今天终于搞定 原因如下 Word中不能显示公式 问 在Word 2003中编辑好的公式无法显示 只显示为一个方框 该怎么办 答 Word把使用公式编辑器输入的公式作为图形处理
  • SPECCPU 2017测试指导

    一 依赖包下载安装 安装前需要安装依赖包 可通过本地源进行安装 yum install gcc gfortran 离线场景下需要外网下载好后传到本地再安装 Deepin gfortran安装包手动安装3个gfortran的包 可选 yum
  • UDS应用层协议解析(史上最全)

    UDS应用层协议解析 UDS应用层协议解读 下 诊断服务分类 基础服务类 0x10 诊断会话模式 任何会话模式切换至默认会话模式时 非默认会话模式下设置的状态需要reset 28服务 85服务设置的状态需要恢复至默认状态 27服务解锁状态需
  • Win平台搭建WordPress环境

    Win平台搭建WordPress环境 WordPress是一个开源流行的个人信息发布平台 使用PHP编写 现在有众多的网站都使用WordPress来搭建的 同时WordPress还提供了大量的插件 能够帮助人们搭建个性化的网站 安装PHP
  • 在IntelliJ IDEA上使用Maven创建Spring项目HelloWorld

    因为IDEA自带Maven插件 所以使用IDEA是不需要在下载Maven的文件的 也可使用自己下载的Maven Spring我们则是通过Maven来下载构建 所以不需要下载jar包的 大神勿喷 请自行绕道 本博客面向第一次接触spring的
  • 使用Python绘制语音信号的波形图

    improt library import numpy as np import wave import pylab as pl download open souce audio in http www voiptroubleshoote
  • (一)基于物联网的智能安防监控机器人2207231212569

    基于物联网的智能安防监控机器人2207231212569 项目摘要 机器人是人类一直期待的东西 但自动化的东西有点不同 理想情况下 机器人能够做的事情比自动化机器人想做的要多得多 自动化机器人希望实现监控和制造商想要实现的另一主要可用性 但
  • 【六袆 - Dubbo】Dubbo服务的简单调用;

    这里写目录标题 1 Dubbo服务的基本调用过程 1 1在Java中定义dubbo服务 以interface接口的方式 1 2 Provider提供服务的具体实现 并声明为dubbo服务 1 3 Consumer使用dubbo服务 1 Du
  • ArrayList LinkedList Set HashMap介绍

    在Java中提供了Collection和Map接口 其中List和Set继承了Collection接口 同时用Vector ArrayList LinkedList三个类实现List接口 HashSet TreeSet实现Set接口 直接有
  • 11-13 输入输出流的位置

    1 获取文件流的读取位置 使用 ftell 函数可以获取当前文件流的读取位置 其返回值为当前位置距 0 位置的字节数 文件以二进制形式打开后 默认从 0 位置开始读取 读取一定字节后 读取位置会向后推移该字节数 例如下面的代码 未读取时 p
  • Java中FileInputStream简介说明

    转自 Java中FileInputStream简介说明 FileInputStream简介说明 FileInputStream对象的功能用于从文件中读取数据 我们可使用new 关键字创建此对象 FileInputStream功能 用于从文件
  • C++报错 invalid operands to binary expression

    C 报错 invalid operands to binary expression c 为什么加 const 就解决了 invalid operands to binary expression c 为什么加 const 就解决了 inv
  • 四种IO模型

    四种IO模型 目录 一 什么是IO 二 阻塞IO 三 非阻塞IO 四 信号驱动IO 五 异步IO 目录 一 什么是IO 对于IO的简单理解 我们首先通过两个数据之间的交互过程来理解什么是IO 向上面这样数据从对应的发送缓冲区发送到对应的接受
  • 视频中的I帧、B帧、P帧

    视频文件都是一帧一帧存储的 为了使文件的大小减小 通常会对文件进行压缩 mpeg4 MP4 文件中的每一帧开始都是固定的 00 00 01 b6 那么在接下来的每一帧分别是什么帧呢 I帧 B帧 P帧 一般在这固定帧的后面2bit就是标志是什
  • 【山河送书第十一期】:朋友圈大佬都去读研了,这份备考书单我码住了,考研书籍五本!!

    朋友圈大佬都去读研了 这份备考书单我码住了 数据结构与算法分析 计算机网络 自顶向下方法 现代操作系统 深入理解计算机系统 概率论基础教程 原书第10版 线性代数 原书第10版 线性代数及其应用 重磅推荐 参与方式 往期赠书回顾 八九月的朋
  • 【翻译】torch.device的使用举例

    参考链接 class torch device 原文及翻译 torch device torch device栏目 class torch device torch device 类型 A torch device is an object