今天在pytorch中遇到这个问题
RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same or input should be a MKLDNN tensor and weight is a dense tensor
原因是你在使用torch.save时用的GPU或者CPU训练,而你在另个文件使用torch.load没有正确对应,即torch.save使用GPU,torch.loader也使用GPU
解决方法
1.问题来源是输入数据没有加载到GPU,解决方法为
tensor = tensor.cuda()
tensor = tensor.to('cuda')
任选 但是要注意直接tensor.to('cuda')等方法不行
原因:
tensor.to()函数不是“ in -place”方法。tensor.to() 这个函数功能是产生一个新的tensor,并不会改变原数据。
2.在你使用的模型后面加model=torch.load("tudui_29_gpu.pth",map_location=torch.device('cpu'))
原理 当你从硬盘加载已保存的模型时,如果你的模型是在 GPU 上训练的,你需要使用 map_location=torch.device('cpu')
将模型加载到 CPU 上。