当前位置:   article > 正文

pytorch cpu与gpu load时相互转化 torch.load(map_location=)

pytorch cpu与gpu load时相互转化 torch.load(map_location=)

pytorch将cpu训练好的模型参数load到gpu上,或者gpu->cpu上

假设我们只保存了模型的参数(model.state_dict())到文件名为modelparameters.pth, model = Net()

1. cpu -> cpu或者gpu -> gpu:

checkpoint = torch.load('modelparameters.pth')

model.load_state_dict(checkpoint)

2. cpu -> gpu 1

torch.load('modelparameters.pth', map_location=lambda storage, loc: storage.cuda(1))

3. gpu 1 -> gpu 0

torch.load('modelparameters.pth', map_location={'cuda:1':'cuda:0'})

4. gpu -> cpu

torch.load('modelparameters.pth', map_location=lambda storage, loc: storage)

或者:

如果出现这个报错

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='cpu' to map your storages to the CPU.

torch.load(opt.model,map_location='cpu')

 

方便查阅

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/851508
推荐阅读
相关标签
  

闽ICP备14008679号