PyTorch device与cuda.device用法介绍
作者:研究生不迟到 时间:2022-07-26 23:49:10
1 查看当前的device
输入情况:
import torch
print("Default Device : {}".format(torch.Tensor([4, 5, 6]).device))
输出情况:
Default Device : cpu
2 cpu设备可以使用“cpu:0”来指定
输入情况
device = torch.Tensor([1, 2, 3], device="cpu:0").device
print("Device Type: {}".format(device))
输出情况
Device Type: cpu
3 gpu设备可以使用“cuda:0”来指定
输入情况
gpu = torch.device("cuda:0")
print("GPU Device:【{}:{}】".format(gpu.type, gpu.index))
输出情况
GPU Device:【cuda:0】
4 查询CPU和GPU设备数量
输入情况
print("Total GPU Count :{}".format(torch.cuda.device_count()))
print("Total CPU Count :{}".format(torch.cuda.os.cpu_count()))
输出情况
Total GPU Count :1
Total CPU Count :8
5 从CPU设备上转换到GPU设备
5.1 torch.Tensor方法默认使用CPU设备
输入情况
data = torch.Tensor([[1, 4, 7], [3, 6, 9], [2, 5, 8]])
print(data.shape)
输出情况
torch.Size([3, 3])
5.2 使用to方法将cpu的Tensor转换到GPU设备上
输入情况:
data_gpu = data.to(torch.device("cuda:0"))
print(data_gpu.device)
输出情况:
cuda:0
5.3 使用.cuda方法将cpu的Tensor转换到GPU设备上
输入情况:
data_gpu2 = data.cuda(torch.device("cuda:0"))
# 如果只有一块gpu的话 直接写成这样:data_gpu2 = data.cuda()
print(data_gpu2.device)
输出情况:
cuda:0
来源:https://blog.csdn.net/weixin_42521185/article/details/123913221
标签:PyTorch,device,使用
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python基于jieba分词实现snownlp情感分析
2023-11-14 21:43:38
![](https://img.aspxhome.com/file/2023/0/63590_0s.jpg)
Django框架HttpResponse对象用法实例分析
2022-06-06 09:33:55
python 爬虫百度地图的信息界面的实现方法
2021-11-10 20:29:35
![](https://img.aspxhome.com/file/2023/9/65419_0s.png)
Python二分查找+字符串模板+textwrap模块,
2023-03-12 11:12:44
![](https://img.aspxhome.com/file/2023/4/112444_0s.webp)
Python二进制数据结构Struct的具体使用
2022-07-10 00:01:59
![](https://img.aspxhome.com/file/2023/5/77895_0s.png)
Python异常模块traceback用法实例分析
2022-07-22 15:47:14
Python中extend和append的区别讲解
2021-03-28 04:20:37
python深度学习人工智能BackPropagation链式法则
2023-07-17 12:23:07
![](https://img.aspxhome.com/file/2023/5/102335_0s.jpg)
asp如何用Jmail组件的发送电子邮件?
2010-06-12 12:51:00
ASP获取ACCESS数据库的表名以及表名对应的字段名和字段类型
2009-12-25 18:57:00
基于Python闭包及其作用域详解
2023-11-07 07:22:02
![](https://img.aspxhome.com/file/2023/9/90839_0s.jpg)
Python @property装饰器原理解析
2022-02-19 20:28:05
Pycharm小白级简单使用教程
2022-05-12 23:26:32
![](https://img.aspxhome.com/file/2023/7/82827_0s.jpg)
ie7空格的间距要比ie6/firefox/opera的都要大
2008-05-24 16:54:00
python 使用pandas计算累积求和的方法
2021-05-22 19:46:16
对Python中range()函数和list的比较
2021-08-26 14:59:47
Python打包模块wheel的使用方法与将python包发布到PyPI的方法详解
2022-03-26 10:52:57
读写xml文件的2个小函数
2007-08-23 12:59:00
python基于watchdog库全自动化监控目录文件
2022-04-21 19:17:47
Python函数式编程实例详解
2023-02-26 21:26:31