关于#深度学习#的问题:运行时报错(语言-python)

网上一篇论文的源代码,运行时报错
if torch.cuda.is_available():
        model = model.cuda()
         
if torch.cuda.device_count() > 1:
        model = nn.DataParallel(model)
        model.load_state_dict(torch.load(modelPath))

RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.
我的解答思路和尝试过的方法
我想要达到的结果

你的主机没有GPU,但是读入的预训练模型是使用GPU训练的,所以报错了,只需要在读取模型的torch.load加上参数map_location=torch.device('cpu'),大概是这样:

model = torch.load(model_path , map_location=torch.device('cpu'))

报错提示其实已经给出了解决方法:
If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.