RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 ‘target’ in call to _thnn_binary_cross_entropy_forward

出错误背景:Pytorch 中想使用 CUDA 对程序计算进行加速

错误的意思:object 的 device 类型期望得到的是 cuda 类型,但是实际上的类型确实 cpu 类型,在调用二分类交叉熵损失进行前向计算的时候

检查下面几点

  1. 模型是否放到了CUDA上 model = model.to(device)model = model.cuda(device)
  2. 输入数据是否放到了CUDA上 data = data.to(device)data = data.cuda(device)
  3. 模型内部新建的张量是否放到了CUDA上 p = torch.tensor([1]).to(device)p = torch.tensor([1]).cuda(device)
    一般情况下应该是忘记了第三点,而根据提示也可以知道,在进行二分类交叉熵损失进行前向计算的过程中,存在没有放到cuda上的张量,找到他,fix it !!!

其中:device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)

或者:写一个方法,来判断是否有cuda,然后决定是否放上去:

def trans_to_cuda(variable):if torch.cuda.is_available():return variable.cuda()else:return variable


http://www.taodudu.cc/news/show-63988.html

相关文章:

  • 熵,交叉熵,散度理解较为清晰
  • 交叉熵理解深度学习互信息
  • 各种正交以及正交和
  • 动态规划 搜索
  • 支持度置信度关联
  • Apriori算法通俗详解_fpgrowth_关联
  • 正向最大匹配 和逆向最大匹配对比比较
  • 数学符号1
  • RuntimeError: Expected object of backend CUDA but got backend CPU for argument
  • 通俗解释指数加权平均
  • CUDA error: device-side assert triggered Assertion t 」= 0 t n classes failed
  • Python多线程调试
  • 多线程调试
  • 模型不收敛
  • python读取word
  • TensorRT简介
  • 对列表去重并保持原来的顺序
  • torch.backends.cudnn.deterministic 使用cuda保证每次结果一样
  • torch.nn.functional.cross_entropy.ignore_index
  • GPT-3 Finetune
  • GPT3 api接口调用
  • GPT3应用
  • events.out.tfevents文件
  • 特殊字符编码
  • 自己动手实现20G中文预训练语言模型示例
  • 认知事物
  • 二元学习法3.0:三把学习大剑,打通学习的底层密码_学习方法
  • 自监督学习现状和未来
  • 从2012年到现在深度学习领域标志成果
  • 数据算法算力知识反绎学习

RuntimeError: Expected object of device type cuda but got device type cpu for argument pytorch数据位置相关推荐

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

  2. RuntimeError: Expected object of type torch.cuda.LongTensor but found type torch.cuda.IntTensor

    问题: anchor_label = np.array(self.id_dict[int(anchor_id)])identity_loss = criterion_identity(predicte ...

  3. 使用PyTorch前向运算时出现“RuntimeError: Expected object of scalar type Long but got scalar type Float for ……”

    1 问题描述 今天在使用PyTorch搭建模型时,出现了一个问题: RuntimeError: Expected object of scalar type Long but got scalar t ...

  4. 解决:RuntimeError: Expected object of scalar type Int but got scalar type Double

    问题描述: 最近在写使用神经网络预测的代码,在更换导入的数据后,原本的loss函数(如下所示)报错RuntimeError: Expected object of scalar type Int bu ...

  5. RuntimeError: Expected object of backend CUDA but got backend CPU for argument

    RuntimeError: Expected object of backend CUDA but got backend CPU for argument #4 'mat1' 原因:变量没有加cud ...

  6. RuntimeError: Expected object of backend CUDA but got backend CPU for argument #4 'mat1'

    RuntimeError: Expected object of backend CUDA but got backend CPU for argument #4 'mat1' 原因:变量没有加cud ...

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

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

  9. Pytorch 类型错误:Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor.

    Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor Pytorcht调试过程中,将数据传入模 ...

最新文章

  1. 几种和生成网络相似的纠缠网络(接近人脑)
  2. 如何在到处是“雷”的系统中「明哲保身」?这是第一招
  3. MySQL errno: 145 错误修复
  4. 腾讯云Kafka海量服务自动化运营实践
  5. Qt/C++工作笔记-对vector与QVector中erase操作的进一步认识(区别与联系)
  6. 云安全仍然是程序开发者头痛的问题
  7. underscore api
  8. 只学一门java可行吗,java可以作为第一门编程语言学习吗
  9. 持续交付2.0 pdf_便捷下载发布v7.2.0版本更新
  10. SpringBoot整合微信登录
  11. CorelDraw手表实例实操作让你全面了解技术的综合应用
  12. winpe 安装 win7 过程问题综述
  13. 初始值设置项里有未知的字段ndo_change_mtu
  14. JAVA(图形界面)
  15. 八位硕博大佬的百度之旅~
  16. 数据采集:如何使用物联网边缘计算网关采集数据?
  17. 概述 | 全景图像拼接技术全解析
  18. brand.php dnfire.cn_火灾报警系统品牌
  19. 前端物联网的起点 node-red 项目的开启
  20. Ubuntu16.04安装flash

热门文章

  1. Pandas 使用入门
  2. 【Sql Server】DataBase-事务与系统元数据函数
  3. GOF23设计模式(结构型模式)代理模式~
  4. CMOS图像传感器与DDI显示芯片
  5. C++ OP相关注意事项
  6. Visual SLAM
  7. HashMap 的长度为什么是 2 的幂次方?
  8. 2021年大数据常用语言Scala(十二):基础语法学习 方法调用方式
  9. 2021年大数据Flink(二十九):Flink 容错机制 Savepoint
  10. HTTP/HTTPS抓包工具-Fiddler