最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
pytorch中的model=model.to(device)使用说明代码示例
时间:2022-06-25 01:59:57 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下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')
device = torch.device('cpu')
model = TheModelClass(*args, **kwargs)
model.load_state_dict(torch.load(PATH, map_location=device))
将由GPU保存的模型加载到GPU上。确保对输入的tensors调用input = input.to(device)方法。
device = torch.device("cuda")
model = TheModelClass(*args, **kwargs)
model.load_state_dict(torch.load(PATH))
model.to(device)
将由CPU保存的模型加载到GPU上。
确保对输入的tensors调用input = input.to(device)方法。map_location是将模型加载到GPU上,model.to(torch.device('cuda'))是将模型参数加载为CUDA的tensor。
最后保证使用.to(torch.device('cuda'))方法将需要使用的参数放入CUDA。
device = torch.device("cuda")
model = TheModelClass(*args, **kwargs)
model.load_state_dict(torch.load(PATH, map_location="cuda:0")) # Choose whatever GPU device number you want
model.to(device)
相关文章
- 漫蛙网页版入口防走失指南-漫蛙网页版入口防走失必备跳转链接 12-20
- 百度搜有红包怎么领-百度搜有红包活动入口在哪 12-20
- 抖音网页版电脑端入口-抖音官网电脑版高清在线 12-20
- 纯纯写作官方授权网页版快捷登录通道-纯纯写作电脑端官网直达入口 12-20
- 3D免费同人漫画高清入口-3D同人漫画网站畅快立体沉浸 12-20
- poki小游戏免费畅玩入口-poki小游戏即点即玩免下载入口 12-20