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调试过程中,将数据传入模型,进行计算。出现这个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.相关推荐
- 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前向运算时出现“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 ...
- 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 ...
- 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: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be
使用torchsummary的时候,出现以下错误: (涉及板块: python 库整理:Timm(1)_UQI-LIUWJ的博客-CSDN博客 pytorch 笔记:torchsummary_UQI- ...
- 解决:RuntimeError: Expected object of scalar type Int but got scalar type Double
问题描述: 最近在写使用神经网络预测的代码,在更换导入的数据后,原本的loss函数(如下所示)报错RuntimeError: Expected object of scalar type Int bu ...
- Pytorch出现RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor)
这个问题的主要原因是输入的数据类型与网络参数的类型不符. Input type为torch.cuda.FloatTensor(GPU数据类型), weight type(即net.parameters ...
- catia二次开发:函数是否需要加括号 ,type类型 ,出现未定义变量类型错误,怎么破, 背景色设置 ,检查模块是否封闭 ,几何图形集是否存在某一个元素
文章目录 函数是否需要加括号 type类型 出现未定义变量类型错误,怎么破 背景色设置 检查模块是否封闭 几何图形集是否存在某一个元素 函数是否需要加括号 type类型 出现未定义变量类型错误,怎么破 ...
- 【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 ...
最新文章
- ADPRL - 近似动态规划和强化学习 - Note 6 - Mitigating the Curse of Dimensionality
- C#递归获取JSON所有键值对
- Dockerfile使用方法详解
- jq封装接口ajax,jquery ajax方法封装及api文件设计的代码示例
- 珍惜平时一点一滴,这几个值得跟进学习的阿里、滴滴、微软超级牛人的公众号!...
- Ubuntu E: Sub-process /usr/bin/dpkg returned an error code (1)
- 免费破解版Xshell和Xftp
- 永中文档在线预览集群部署方案
- DS1302 时钟 51单片机实现时钟显示
- 堂堂985毕业就失业,我用3000块买大厂Offer
- 【微信网页授权】SpringBoot+uniapp实现网页授权获取用户基本信息
- vscode将后缀为.art文件识别为html文件
- 容联云:实现异步 手机短信验证码
- 吴恩达机器学习(十五)—— 应用实例:图片文字识别
- 【数值分析】插值法:拉格朗日插值、牛顿插值
- API接口安全加密,防止接口被黑客攻击
- 加油站爆出注水汽油 豪车熄火
- uniapp如何给全端小程序添加激励广告详细教程
- Flink 应用场景
- JAVA开发各种bug、踩坑记录