当前位置:   article > 正文

pytorch 模型 cpu 和 gpu load--相互转换

gpu load

pytorch 模型 cpu 和 gpu load–相互转换

load 模型的时候,会有将模型加载到 cpu 还是 gpu 内存的区别。
会遇到这种问题。

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.

# 只要在 load 中加入下面就可以解决了
map_location='cpu'
  • 1
  • 2
  • 3
  • 4
  1. cpu -> cpu 或 gpu -> gpu:
model.load_state_dict(torch.load('model.pth'))
  • 1
  1. cpu -> gpu
torch.load('model.pth', map_location= lambda storage, loc: storage.cuda(0)). 
  • 1
  1. gpu 0 -> gpu 1
torch.load('model.pth', map_location={'cuda:0' : 'cuda:1'})
  • 1
  1. gpu -> cpu
torch.load('model.pth', map_location= lambda storage, loc: storage)
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/851513
推荐阅读
相关标签
  

闽ICP备14008679号