您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

RuntimeError:输入类型(torch.FloatTensor)和权重类型(torch.cuda.FloatTensor)应该相同

RuntimeError:输入类型(torch.FloatTensor)和权重类型(torch.cuda.FloatTensor)应该相同

因为模型在GPU上,但是数据在cpu上,所以会出现此错误。因此,您需要将输入张量发送到GPU。

inputs, labels = data                         # this is what you had
inputs, labels = inputs.cuda(), labels.cuda() # add this line

或者像这样,与其他代码保持一致:

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

inputs, labels = inputs.to(device), labels.to(device)

在 ,如果你输入张量在GPU上,但你的模型权重不是会得到提升。在这种情况下,您需要将模型权重发送到GPU。

model = MyModel()

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

下面是文件cuda()cpu(),它的反面。

其他 2022/1/1 18:44:13 有374人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶