当前位置:网站首页>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
边栏推荐
- Sword finger offer 15 Number of 1 in binary
- Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
- Brief analysis of tensorboard visual processing cases
- 正则表达式
- 【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
- Flink SQL knows why (XI): weight removal is not only count distinct, but also powerful duplication
- Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?
- The R language GT package and gtextras package gracefully and beautifully display tabular data: nflreadr package and gt of gtextras package_ plt_ The winloss function visualizes the win / loss values
- Logback 日志框架
- 这本数学书AI圈都在转,资深ML研究员历时7年之作,免费电子版可看
猜你喜欢
Flick SQL knows why (10): everyone uses accumulate window to calculate cumulative indicators
刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
Servlet
Flink code is written like this. It's strange that the window can be triggered (bad programming habits)
Logseq 评测:优点、缺点、评价、学习教程
这本数学书AI圈都在转,资深ML研究员历时7年之作,免费电子版可看
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter 6 exercises]
106. 如何提高 SAP UI5 应用路由 url 的可读性
Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?
随机推荐
Flink SQL knows why (XIV): the way to optimize the performance of dimension table join (Part 1) with source code
Some thoughts on business
MapReduce implements matrix multiplication - implementation code
2022-02-14 incluxdb cluster write data writetoshard parsing
2022-02-11 heap sorting and recursion
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
When updating mysql, the condition is a query
MySQL
Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL
Luogup3694 Bangbang chorus standing in line
JS convert pseudo array to array
Mysql database basic operation - regular expression
Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?
Solve system has not been booted with SYSTEMd as init system (PID 1) Can‘t operate.
已解决(机器学习中查看数据信息报错)AttributeError: target_names
Logback 日志框架
CVPR 2022 image restoration paper
Father and basketball
Tencent cloud tdsql database delivery and operation and maintenance Junior Engineer - some questions of Tencent cloud cloudlite certification (TCA) examination
Annotation and reflection