Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor

Pytorcht调试过程中,将数据传入模型,进行计算。出现这个error,表明你的数据格式有问题。也许模型是GPU上的,参数是CPU类型。也许模型是CPU,参数是GPU类型。这是由于用了.cuda()进行转换。两个方法可以解决。

1. 既然需要FloatTensor,就强制转换你的模型和数据为cpu,就将GPU的model和input转换为CPU。

    device1=torch.device("cpu")model_ft = model_ft.to(device1)#将模型转换为cpu版本。model_ft.train()inputs = inputs.to(device1)#将输入数据转换为CPU版本。labels = labels.to(device1)#将label转换为CPU版本。output = model_ft(inputs)_,preds = torch.max(output,1)

2. 将模型和数据转换成GPU版本,既然参数是cuda.FloatTensor,说明模型的参数是cuda类型的。强制转换输入和模型在GPU上就可以。

    device1=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")model_ft = model_ft.to(device1)#将模型转换为GPU版本。model_ft.train()inputs = inputs.to(device1)#将输入数据转换为GPU版本。labels = labels.to(device1)#将label转换为GPU版本。output = model_ft(inputs)_,preds = torch.max(output,1)

Pytorch 类型错误:Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor.相关推荐

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

  2. 使用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 ...

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

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

  5. 错误处理:RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be

    使用torchsummary的时候,出现以下错误: (涉及板块: python 库整理:Timm(1)_UQI-LIUWJ的博客-CSDN博客 pytorch 笔记:torchsummary_UQI- ...

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

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

  7. Pytorch出现RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor)

    这个问题的主要原因是输入的数据类型与网络参数的类型不符. Input type为torch.cuda.FloatTensor(GPU数据类型), weight type(即net.parameters ...

  8. catia二次开发:函数是否需要加括号 ,type类型 ,出现未定义变量类型错误,怎么破, 背景色设置 ,检查模块是否封闭 ,几何图形集是否存在某一个元素

    文章目录 函数是否需要加括号 type类型 出现未定义变量类型错误,怎么破 背景色设置 检查模块是否封闭 几何图形集是否存在某一个元素 函数是否需要加括号 type类型 出现未定义变量类型错误,怎么破 ...

  9. 【vue警告坑】 Invalid prop: type check failed for prop resultChild. Expected Object, got String with v

     [Vue warn]: Invalid prop: type check failed for prop "resultChild". Expected Object, got ...

最新文章

  1. ADPRL - 近似动态规划和强化学习 - Note 6 - Mitigating the Curse of Dimensionality
  2. C#递归获取JSON所有键值对
  3. Dockerfile使用方法详解
  4. jq封装接口ajax,jquery ajax方法封装及api文件设计的代码示例
  5. 珍惜平时一点一滴,这几个值得跟进学习的阿里、滴滴、微软超级牛人的公众号!...
  6. Ubuntu E: Sub-process /usr/bin/dpkg returned an error code (1)
  7. 免费破解版Xshell和Xftp
  8. 永中文档在线预览集群部署方案
  9. DS1302 时钟 51单片机实现时钟显示
  10. 堂堂985毕业就失业,我用3000块买大厂Offer
  11. 【微信网页授权】SpringBoot+uniapp实现网页授权获取用户基本信息
  12. vscode将后缀为.art文件识别为html文件
  13. 容联云:实现异步 手机短信验证码
  14. 吴恩达机器学习(十五)—— 应用实例:图片文字识别
  15. 【数值分析】插值法:拉格朗日插值、牛顿插值
  16. API接口安全加密,防止接口被黑客攻击
  17. 加油站爆出注水汽油 豪车熄火
  18. uniapp如何给全端小程序添加激励广告详细教程
  19. Flink 应用场景
  20. JAVA开发各种bug、踩坑记录

热门文章

  1. Box2d 基础入门知识
  2. mybatis CRUD操作
  3. Pytorch的网络结构可视化(tensorboardX)(详细)
  4. Python第三方包的egg info 是什么东西
  5. HarmonyOS系统概述
  6. ARMed解决方案对DSP的战争
  7. 预测汽车级Linux专业技术的需求
  8. Linux环境变量说明与配置
  9. python 正则表达式 re.compile() 的使用
  10. Android 白天/夜间模式切换