PyTorch——device与cuda.device用法
文章目录
- 1 查看当前的device
- 2 cpu设备可以使用“cpu:0”来指定
- 3 gpu设备可以使用“cuda:0”来指定
- 4 查询CPU和GPU设备数量
- 5 从CPU设备上转换到GPU设备
- 5.1 torch.Tensor方法默认使用CPU设备
- 5.2 使用to方法将cpu的Tensor转换到GPU设备上
- 5.3 使用.cuda方法将cpu的Tensor转换到GPU设备上
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
PyTorch——device与cuda.device用法相关推荐
- RuntimeError: Expected object of device type cuda but got device type cpu for argument pytorch数据位置
RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 'target' i ...
- PyTorch笔记: GPU上训练的模型加载到CPU/错误处理Attempting to deserialize object on a CUDA device but torch.cuda.is_a
我之前在GPU上训练了一个模型,同时把模型的参数记录在resnet18_ultimate.pkl上 在本地的CPU上,我想把参数加载,于是一开始我是这么写代码的: import torch impor ...
- RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 'target'
RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 'target' i ...
- 【错误处理】RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count
RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count 1 项目场景 2 ...
- Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False.
记录一下遇到的问题. 最近在搭环境时出现了这个错误,网上找了许多方案都没法解决,说的大概都是cuda版本和pytorch版本不兼容,我按照pytorch官网的指令安装了许多次都没用. 我的cuda版本 ...
- RuntimeError: Attempting to deserialize object on CUDA device 2 but torch.cuda.device_count() is 2.
解决报错RuntimeError: Attempting to deserialize object on CUDA device 2 but torch.cuda.device_count() is ...
- 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 ...
- Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False
Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False 此时改为: torch ...
- 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 ...
- RuntimeError: Attempting to deserialize object on CUDA device 3 but torch.cuda.device_count() is 1.
问题描述: 运行测试程序的时候,报错"RuntimeError: Attempting to deserialize object on CUDA device 3 but torch.cu ...
最新文章
- php编译7教程,PHP7 快速编译安装
- android蓝牙音乐之AVRCP介绍和使用
- 五分钟学会悲观乐观锁-java vs mysql vs redis三种实现
- Win7 IIS7 ASP.NET MVC3.0 部署问题
- 运维linux脚本实例,Shell脚本使用示例
- 成年人的世界里,赚钱是保护自己和身边人最高效的手段
- vue项目中使用sass的方法
- 数据分析数据可视化(四)
- 一笔期货成交的始末(可能有问题)
- 微软服务器探针产品,服务器云探针
- 查看链接文件的最终目标的多种方法
- mysql脏页处理方法_mysql刷脏页的一次总结
- oracle.jdbc.driver.OracleDriver is deprecated
- Triangle程序编译
- 程序设计思维与实践 csp路径解析、csp炉石传说
- 矩阵键盘逐行扫描C语言,(原创)51单片机C语言程序设计--速学教程实例(入门篇)之矩阵键盘(逐行扫描法).pdf...
- 【Web技术】1176- 前端模块依赖关系分析与应用
- Mybatis—动态SQL语句与逆向工程
- [转]程序员的恶性循环 [时刻警醒自己]
- 【资料下载】133个各行业脑图精华免费下载