一、问题描述

在跑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相关推荐

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

  2. 解决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 ...

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

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

  5. 异常解决(一)-- RuntimeError: expected device cpu but got device cuda:0

    最近在编写深度学习的相关代码,基于PyTorch,运行程序的时候,报错,报错内容如下所示: RuntimeError: expected device cpu but got device cuda: ...

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

  7. 当使用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 出错过程及细节 在一个新服务器上 ...

  8. 解决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数据库驱动总 ...

  9. 【已解决】(uni-app)IOS 端 H5 应用无法发起请求,请求状态码为0,错误描述为request:fail

    [已解决](uni-app)IOS 端 H5 应用无法发起请求,请求状态码为0,错误描述为request:fail 目录 [已解决](uni-app)IOS 端 H5 应用无法发起请求,请求状态码为0 ...

最新文章

  1. Activity启动流程详解
  2. Swift3.0语言教程删除字符与处理字符编码
  3. jQuery中append、insertBefore、after与insertAfter方法注意事项
  4. The requested URL /nagios/cgi-bin/statusmap.cgi was not found on this server
  5. hdu 4747 mex 线段树+思维
  6. 天池 在线编程 到达终点
  7. python爬虫实现方式_python爬虫的实现方法
  8. ios 与web服务器传值_中高级iOS大厂面试宝典,拿到offer率80%,金三银四将是你的新起点...
  9. 用户微信好友关系属于个人隐私吗?深圳南山法院说不属于...
  10. 《机器学习实战》中英文电子书 + 源代码下载
  11. python控制微信app_Appium+python在微信的hybridapp里的坑
  12. 图片怎么转换成pdf格式?手把手教你图片转PDF怎么转
  13. bzoj 1467 exBSGS
  14. 《星际争霸2》【技术分析】星际争霸2的一些技术特性
  15. vue进阶实战 vue进阶课程 vue进阶
  16. Cloud Foundry 峰会进入中国 全球专家与你面对面
  17. 大数据技术原理与应用之【云数据库】习题
  18. [Java] Comparator接口/compare方法的介绍与使用
  19. 360类redis存储服务Pika的安装和使用
  20. 微信小程序学习day01-WXML 模板语法

热门文章

  1. 北航 计算机学院 加试两门,北航强军计划考研计算机学院招生.pdf
  2. 云笔记有什么功能作用,浏览器如何添加云笔记插件
  3. ccd视觉定位教程_什么是CCD视觉定位自动焊锡机器人?
  4. ib中文文学课如何学习重点?
  5. matlab 莫比乌斯曲面,『Rhino实例.2』莫比乌斯曲面
  6. 华为和H3c--交换技术
  7. 网站建设之邮件发送形式
  8. SSM+阳光大学宿舍管理系统 毕业设计-附源码211714
  9. 5G移动通信标准学习笔记(一)
  10. 计算机图灵,什么叫做图灵计算机?