python 报错:Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

报错:Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same解决方案

在验证所提模型是否正常时我们经常会使用下面的代码:

if __name__ == "__main__":
    model = MSTransception() # 也可替换为自己的模型
    tmp_0 = model(torch.rand(1, 3, 224, 224).cuda())
    print(tmp_0.shape)

执行完报错提示:

报错:Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

解决办法;
需要在网络模型model = MSTransception(num_classes=9)后面添加设备即可,即;model = MSTransception(num_classes=9).cuda()。注意:并不是所有网络都需要在后面添加**.cuda()**,只有在出现上述错误的时候才用到

if __name__ == "__main__":
    model = MSTransception().cuda()
    tmp_0 = model(torch.rand(1, 3, 224, 224).cuda())
    print(tmp_0.shape)

——>点关注,不迷路!创作不易,感谢支持!