马春杰杰博客
致力于深度学习经验分享!

[mcj]pytorch中model=model.to(device)用法

这代表将模型加载到指定设备上。

其中,device=torch.device("cpu")代表的使用cpu,而device=torch.device("cuda")则代表的使用GPU。

当我们指定了设备之后,就需要将模型加载到相应设备中,此时需要使用model=model.to(device),将模型加载到相应的设备中。

将由GPU保存的模型加载到CPU上。

torch.load()函数中的map_location参数设置为torch.device('cpu')

将由GPU保存的模型加载到GPU上。确保对输入的tensors调用input = input.to(device)方法。

将由CPU保存的模型加载到GPU上。确保对输入的tensors调用input = input.to(device)方法。map_location是将模型加载到GPU上,model.to(torch.device('cuda'))是将模型参数加载为CUDA的tensor。最后保证使用.to(torch.device('cuda'))方法将需要使用的参数放入CUDA。

 

赞(242) 打赏
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《[mcj]pytorch中model=model.to(device)用法》
文章链接:https://www.machunjie.com/deeplearning/pytorch/606.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 2

  1. #1

    collections.orderedDict object has no contribute ‘load_state_dict’,请问您知道这个error怎么解决吗?

    的微分对策6年前 (2020-09-23)回复
    • 兄弟,你不留真实邮箱我根本联系不到你。。

      马春杰杰6年前 (2020-09-23)回复

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

:smile: :sad: :arrow: :cool: :confused: :cry: :eek: :evil: :exclaim: :idea: :lol: :mad: :mrgreen: :neutral: :question: :razz: :redface: :rolleyes: :surprised: :wink: :biggrin: :twisted: