首页 > 其他 > 详细

pytorch--cpu与gpu load时相互转化

时间:2019-06-06 19:16:06      阅读:1413      评论:0      收藏:0      [点我收藏+]

将gpu改为cpu时,遇到一个报错: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("0.9472_0048.weights",map_location=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)

pytorch--cpu与gpu load时相互转化

原文:https://www.cnblogs.com/llfctt/p/10986401.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!