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 torch.device类(表示在其上或将要分配torch.Tensor的设备)相关推荐

  1. 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 ...

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

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

  3. 使用 Pytorch 进行多类图像分类

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 关于数据集 此数据包含大小为150x150.分布在6个类别下的约2 ...

  4. 【深度学习】使用 Pytorch 进行多类图像分类

    关于数据集 此数据包含大小为150x150.分布在6个类别下的约25k图像. {'建筑物':0,'森林':1,'冰川':2,'山':3,'海':4,'街道':5} 训练.测试和预测数据在每个 zip ...

  5. 【Pytorch分布式训练】在MNIST数据集上训练一个简单CNN网络,将其改成分布式训练

    文章目录 普通单卡训练-GPU 普通单卡训练-CPU 分布式训练-GPU 分布式训练-CPU 租GPU服务器相关 以下代码示例基于:在MNIST数据集上训练一个简单CNN网络,将其改成分布式训练. 普 ...

  6. 《pytorch车型细分类网络》的源码

    说明:<pytorch车型细分类网络>.这篇文章代码有错误.我稍微调整了一下,可以正常跑了. 标题:pytorch动手实践:pytorch车型细分类网络 1)讲解,代码,主要参考知乎文章& ...

  7. Pytorch实现102类鲜花分类(102 Category Flower Dataset)

    Pytorch实现102类鲜花分类(VGG19和ResNet152模型) 本文主要讲解该算法的实现过程,原理部分需读者自行研究,可以找一些论文之类的. 实验环境 python3.6+pytorch1. ...

  8. 深度之眼 PyTorch 训练营第 4 期(5):构建模型 torch.nn.Module

    本文中,我们看一看如何构建模型. 创造一个模型分两步:构建模型和权值初始化.而构建模型又有"定义单独的网络层"和"把它们拼在一起"两步. 1. torch.nn ...

  9. torch.device

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

最新文章

  1. Android框架之路——EventBus的使用
  2. 谷歌宣布推出Dart编程新语言
  3. redis 生成dump.rdb文件
  4. yolov5的3.0版本代码在训练的时候报错:ImportError: cannot import name ‘amp‘ from ‘torch.cuda‘ 以及yolov5的3.0环境安装
  5. 客户/服务器模型:编程思想
  6. html制作表格保存为txt文件,可将HTML表格导出为Excel|csv|txt文件的jQuery插件
  7. Python super超类方法
  8. 笔记-信息系统开发基础-CMMI过程域分类-连续式的模型
  9. eclipse lib中包不能打开_Eclipse环境搭建
  10. java中的文件处理io_Java的IO前奏曲:文件操作类之___File
  11. html中一个页面大概多少px,当屏幕 (浏览器窗口) 小于 768px, 每一列的宽度是 100% -HTML教程_小白教程_css5.net...
  12. alpine安装curl
  13. 布谷鸟过滤器java使用_Redis布隆过滤器与布谷鸟过滤器
  14. MATLAB求函数零点与极值
  15. Latex之大小写罗马数字
  16. SRS源码阅读(一)
  17. 使用RStudio中的read.table导入Txt文件出现列的数目比列的名字要多的问题
  18. 程序员的修仙之路-筑基篇
  19. java中workFlowEvent_关于WorkFlow的使用以及例子
  20. 百度图片下载器2.0

热门文章

  1. 【MM】基于收货的发票校验Bapi Demo
  2. 【转载】扫盲概念RPA
  3. 【MM模块】Subcontracting 委外加工(外包)—1
  4. 【MM配置】Global Data 全局数据相关配置
  5. 【转】NAST表的使用
  6. 【PP操作手册】工程变更创建的创建
  7. 如何在FBL5N中增加所需字段
  8. 成本中心组和标准层次有何区别
  9. SAP 对话屏幕长文本编辑框
  10. MRP区域“MRP Area”的定义以及作用