pytorch加载模型时使用map_location来在CPU、GPU间辗转腾挪

拟墨画扇 提交于 2020-01-14 07:36:54

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

  1. cpu -> cpu或者gpu -> gpu:
checkpoint = torch.load('modelparameters.pth')

model.load_state_dict(checkpoint)
  1. cpu -> gpu 1
torch.load('modelparameters.pth', map_location=lambda storage, loc: storage.cuda(1))
  1. gpu 1 -> gpu 0
torch.load('modelparameters.pth', map_location={'cuda:1':'cuda:0'})
  1. gpu -> cpu
torch.load('modelparameters.pth', map_location=lambda storage, loc: storage)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!