很多时候我们在gpu上训练一个模型,但是在inference的时候不想使用gpu。或者想在别的gpu上使用,那么怎么办呢?
需要在load的时候就选择device。
保存了模型的参数(model.state_dict())到文件model.pth中。
1、cpu->cpu 或gpu->gpu
这种情况是最简单的:
复制代码
1
2
3checkpoint = torch.load('model.pth') model.load_state_dict(checkpoint)
2、cpu->gpu1
复制代码
1
2checkpoint=torch.load('model.pth', map_location=lambda storage, loc: storage.cuda(1)) model.load_state_dict(checkpoint)
3、gpu 0 -> gpu 1
复制代码
1
2checkpoint=torch.load('model.pth', map_location={'cuda:0':'cuda:1'}) model.load_state_dict(checkpoint)
4、gpu -> cpu
复制代码
1
2checkpoint=torch.load('model.pth', map_location=lambda storage, loc: storage) model.load_state_dict(checkpoint)
最后
以上就是友好金针菇最近收集整理的关于Pytorch——将模型load到gpu或cpu上的全部内容,更多相关Pytorch——将模型load到gpu或cpu上内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复