0
点赞
收藏
分享

微信扫一扫

Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same(解决)

  • 问题描述
Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same
  • 在使用pytorch训练经典的MNIST数据集时,运行时,出现了以下的问题:
  • 问题原因:

错误内容大概就是指输入类型是CPU(torch.FloatTensor),而参数类型是GPU(torch.cuda.FloatTensor)
报错内容是:输入的是CPU类型的(torch.FloatTensor),然而输出的内容是GPU类型的,同时它提示,应该保持一直的数据类型

  • 解决错误:

首先检查我们是不是正确的使用了CUDA:
1.下面是正确的使用CUDA的方法:

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

2.而我之前在使用CUDA进行加速时,是这样写的:

 if torch.cuda.is_available():
model.cuda()

显然这样写是错误的,而应该采用第一种方法

  • 结果:

在解决了上述的问题后,使用经典的MNIST数据集训练的模型进行预测的结果也就展示出来了
Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same(解决)_数据类型


举报

相关推荐

0 条评论