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)
热门文章
- rancher k8s修改ingress默认80端口
- 领养宠物要签合同吗现在(领养宠物要签合同吗现在怎么办)
- 动物医院诊疗范围有哪些内容(动物医院诊疗范围有哪些内容呢)
- 上海宠物医院排名榜(上海出名的宠物医院)
- 动物打疫苗后抓了人是否还要打疫苗呢图片(动物打了疫苗的,抓伤了人怎么办)
- vue-cli4.5.13按需引入vant,解决vant样式失效问题
- Pytorch 类型错误:Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor.
- 畜牧站卖疫苗吗(畜牧站可以打狂犬疫苗吗)
- 日本韩国品牌服装排行榜(韩国本土少女衣服品牌)
- 动物医院诊疗许可证办理条件有哪些规定(动物诊疗许可证办理动物医院)