当前位置:网站首页>pytorch 载入历史模型时更换gpu卡号,map_location设置
pytorch 载入历史模型时更换gpu卡号,map_location设置
2022-07-03 13:00:00 【子燕若水】
pytorch 在保存训练后模型的时候,会把训练过程中使用的设备号(例如gpu卡号cuda:0 ,cpu)也一并保存下来。当pytorch重新载入历史模型时,模型默认根据训练时的设备卡号,把权值载入到相应的卡号上。
然而,有的时候测试过程和训练过程的设备情况是不一致的。
举个例子,A主机有四块GPU卡,然后我们用cuda:3 训练模型,并保存模型。
在测试时候,我们需要在客户的B主机跑模型,但是B主机只有一块gpu卡:cuda:0 。
如果按照默认方式载入模型的话,pytorch会报找不到gpu设备,或其他一些错误。
此时,载入的时候需要做一个变换,为torch.load指定gpu设备的映射方式:
根据pytorch的文档,在加载模型的时候,可以指定将模型的tensor加载到特定目标GPU上。
加载方法有:
>>> torch.load('tensors.pt')
# 1. Load all tensors onto the GPU 0
>>> torch.load('tensors.pt', map_location=torch.device('cuda:0'))
# 2. Load all tensors onto GPU 1
>>> torch.load('tensors.pt', map_location=lambda storage, loc: storage.cuda(1))
# 3. Map tensors from GPU 1 to GPU 0
>>> torch.load('tensors.pt', map_location={'cuda:1':'cuda:0'})
实测发现:
方法1 根本就没有加载到目标卡,模型原来在什么卡训练的,还是加载到原来的旧卡号上,因此指定失败。
方法3,代码之间就报错,location.startswith(‘cuda’): AttributeError: ‘NoneType’ object has no attribute ‘startswitch’,分析代码发现这是torch自己的bug! 坑爹的。
方法2: 可以正常的把tensor都加载到cuda1上。
————————————————
版权声明:本文为CSDN博主「Icoding_F2014」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jmh1996/article/details/111041108
边栏推荐
- Red Hat Satellite 6:更好地管理服务器和云
- DQL basic query
- 刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
- CVPR 2022 | 美团技术团队精选6篇优秀论文解读
- 人身变声器的原理
- Resolved (error in viewing data information in machine learning) attributeerror: target_ names
- Flink SQL knows why (12): is it difficult to join streams? (top)
- Resource Cost Optimization Practice of R & D team
- Slf4j log facade
- AI 考高数得分 81,网友:AI 模型也免不了“内卷”!
猜你喜欢

regular expression

MyCms 自媒体商城 v3.4.1 发布,使用手册更新

Finite State Machine FSM

道路建设问题

刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?

Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL

2022-02-14 incluxdb cluster write data writetoshard parsing

User and group command exercises

MySQL constraints

Road construction issues
随机推荐
Flink SQL knows why (7): haven't you even seen the ETL and group AGG scenarios that are most suitable for Flink SQL?
开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
Libuv库 - 设计概述(中文版)
Swiftui development experience: the five most powerful principles that a programmer needs to master
CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
When we are doing flow batch integration, what are we doing?
PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
Multi table query of MySQL - multi table relationship and related exercises
Flink code is written like this. It's strange that the window can be triggered (bad programming habits)
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
Flink SQL knows why (13): is it difficult to join streams? (next)
Logseq 评测:优点、缺点、评价、学习教程
Solve system has not been booted with SYSTEMd as init system (PID 1) Can‘t operate.
SwiftUI 开发经验之作为一名程序员需要掌握的五个最有力的原则
Sword finger offer 15 Number of 1 in binary
Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter 7 exercises]
Convolution emotion analysis task4
Logback 日志框架