解决Expected all tensors to be on the same device, but found at least two devices, cuda:0
一、问题描述
在跑pytorch代码时报错:
Expected all tensors to be on the same device, but found at least two devices, cuda:0
二、解决方案
判断参数数据是在cpu还是哪张gpu上:
import torch
import torch.nn as nn# ----------- 判断模型是在CPU还是GPU上 ----------------------model = nn.LSTM(input_size=10, hidden_size=4, num_layers=1, batch_first=True)
print(next(model.parameters()).device) # 输出:cpumodel = model.cuda()
print(next(model.parameters()).device) # 输出:cuda:0model = model.cpu()
print(next(model.parameters()).device) # 输出:cpu# ----------- 判断数据是在CPU还是GPU上 ----------------------data = torch.ones([2, 3])
print(data.device) # 输出:cpudata = data.cuda()
print(data.device) # 输出:cuda:0data = data.cpu()
print(data.device) # 输出:cpu
查看模型的参数是在哪里(其实也可以直接data.is_cuda
):
model = nn.LSTM(input_size=10, hidden_size=4, num_layers=1, batch_first=True)
print(next(model.parameters()).device)
(1)可能是模型没有移动到和数据相同的device上:model.to(device)
(2)可能是input和参数没有在相同device上,如下类似的操作即可:
device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
data.to(device)
解决Expected all tensors to be on the same device, but found at least two devices, cuda:0相关推荐
- Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu
指向 s_map_norm = torch.mul(s_map_norm , gt) 查看s_map_norm与gt是否在cuda上,用: print(s_map_norm.is_cuda, gt.i ...
- 解决RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cp
今天在把.pt文件转ONNX文件时,遇到此错误. 报错 RuntimeError: Expected all tensors to be on the same device, but found a ...
- Expected all tensors to be on the same device, but found at least two devices 问题解决
解决方法1: 报错代码: anchor_points = self.anchor_points(samples).repeat(batch_size, 1, 1) output_coord = reg ...
- DDP报错::RuntimeError: Expected all tensors to be on the same device, but found at least two devic
DDP报错 RuntimeError: Expected all tensors to be on the same device, but found at least two devices, c ...
- 异常解决(一)-- RuntimeError: expected device cpu but got device cuda:0
最近在编写深度学习的相关代码,基于PyTorch,运行程序的时候,报错,报错内容如下所示: RuntimeError: expected device cpu but got device cuda: ...
- All input tensors must be on the same device
All input tensors must be on the same device RuntimeError: All input tensors must be on the same dev ...
- 当使用tensorflow时出现“the requested device apperas to be a GPU, but CUDA is not enabled”的解决办法
解决方法: 安装 GPU版本的 tensorflow, tensorflow-gpu 安装方法: pip install tensorflow-gpu==1.14.0 出错过程及细节 在一个新服务器上 ...
- 解决maven库中没有Oracle jdbc驱动的问题Cannot resolve com.oracle:ojdbc14:10.2.0.1.0
解决maven库中没有Oracle jdbc驱动的问题Cannot resolve com.oracle:ojdbc14:10.2.0.1.0 在IDEA Maven项目中添加oracle数据库驱动总 ...
- 【已解决】(uni-app)IOS 端 H5 应用无法发起请求,请求状态码为0,错误描述为request:fail
[已解决](uni-app)IOS 端 H5 应用无法发起请求,请求状态码为0,错误描述为request:fail 目录 [已解决](uni-app)IOS 端 H5 应用无法发起请求,请求状态码为0 ...
最新文章
- Activity启动流程详解
- Swift3.0语言教程删除字符与处理字符编码
- jQuery中append、insertBefore、after与insertAfter方法注意事项
- The requested URL /nagios/cgi-bin/statusmap.cgi was not found on this server
- hdu 4747 mex 线段树+思维
- 天池 在线编程 到达终点
- python爬虫实现方式_python爬虫的实现方法
- ios 与web服务器传值_中高级iOS大厂面试宝典,拿到offer率80%,金三银四将是你的新起点...
- 用户微信好友关系属于个人隐私吗?深圳南山法院说不属于...
- 《机器学习实战》中英文电子书 + 源代码下载
- python控制微信app_Appium+python在微信的hybridapp里的坑
- 图片怎么转换成pdf格式?手把手教你图片转PDF怎么转
- bzoj 1467 exBSGS
- 《星际争霸2》【技术分析】星际争霸2的一些技术特性
- vue进阶实战 vue进阶课程 vue进阶
- Cloud Foundry 峰会进入中国 全球专家与你面对面
- 大数据技术原理与应用之【云数据库】习题
- [Java] Comparator接口/compare方法的介绍与使用
- 360类redis存储服务Pika的安装和使用
- 微信小程序学习day01-WXML 模板语法