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

2022年12月27日12:59:13

pytorch框架在写test.py时,报了一个输入,输出类型不一致;

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.cuda.FloatTensor,是因为模型训练在gpu上跑的;

所以在加载模型时,指定设备为cpu这样就与输入类型保持一致。指定代码:

map_location=torch.device("cpu")

例如:

module = torch.load("vincent_0.pth", map_location=torch.device("cpu"))

然后问题解决了。

  • 作者:缄默0603
  • 原文链接:https://blog.csdn.net/weixin_58045467/article/details/124254456
    更新时间:2022年12月27日12:59:13 ,共 367 字。