当前位置:网站首页>pytorch训练好的模型在加载和保存过程中的问题
pytorch训练好的模型在加载和保存过程中的问题
2022-07-06 08:27:00 【MAR-Sky】
在gpu上训练完成,在cpu上加载
torch.save(model.state_dict(), PATH)# 在gpu上训练后保存
# 在cpu的模型上加载使用
model.load_state_dict(torch.load(PATH, map_location='cpu'))
在cpu上训练完成,在gpu上加载
torch.save(model.state_dict(), PATH)# 在gpu上训练后保存
# 在cpu的模型上加载使用
model.load_state_dict(torch.load(PATH, map_location='cuda:0'))
在使用中需要注意的加载内容
当数据放入GPU,需要训练的模型也要放入GPU
''' data_loader:pytorch中加载数据 '''
for i, sample in enumerate(data_loader): # 对数据进行按批次遍历
image, target = sample # 每一批次加载返回值
if CUDA:
image = image.cuda() # 输入输出传入gpu
target = target.cuda()
# print(target.size)
optimizer.zero_grad() # 优化函数
output = mymodel(image)
mymodel.to(torch.device("cuda"))

多个gpu训练时的加载
参考:https://blog.csdn.net/weixin_43794311/article/details/120940090
import torch.nn as nn
mymodel = nn.DataParallel(mymodel)
pytorch中的nn模块使用nn.DataParallel将模型加载到多个GPU,需要注意,这种加载方式保存的权重参数会比不使用nn.DataParallel加载模型保存的权重参数的关键字前多一个"module."。是否使用nn.DataParallel加载模型,会导致下次再加载模型的时候可能会出现下图的问题,
当权重参数前面多一个“module."时,最简单的方式就是使用nn.DataParallel对模型加载,
边栏推荐
- logback1.3. X configuration details and Practice
- matplotlib. Widgets are easy to use
- C language custom type: struct
- Analysis of Top1 accuracy and top5 accuracy examples
- [2022 Guangdong saim] Lagrange interpolation (multivariate function extreme value divide and conquer NTT)
- 使用 BR 恢复 S3 兼容存储上的备份数据
- hcip--mpls
- Convolution, pooling, activation function, initialization, normalization, regularization, learning rate - Summary of deep learning foundation
- [MySQL] lock
- ESP系列引脚说明图汇总
猜你喜欢

2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers

Ruffian Heng embedded bimonthly, issue 49

Deep learning: derivation of shallow neural networks and deep neural networks

Hungry for 4 years + Ali for 2 years: some conclusions and Thoughts on the road of research and development

Unified ordering background interface product description Chinese garbled

JVM performance tuning and practical basic theory - Part 1

704 二分查找

ESP系列引脚說明圖匯總
![[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]](/img/ae/4e616882f6d68acdf8e885843e68a3.jpg)
[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]

Leetcode question brushing (5.28) hash table
随机推荐
Leetcode skimming (5.29) hash table
根据csv文件某一列字符串中某个数字排序
【MySQL】日志
String to leading 0
使用 TiUP 升级 TiDB
Online yaml to CSV tool
IP lab, the first weekly recheck
从 TiDB 集群迁移数据至另一 TiDB 集群
China Light conveyor belt in-depth research and investment strategy report (2022 Edition)
LDAP Application Section (4) Jenkins Access
2022.02.13 - NC002. sort
From monomer structure to microservice architecture, introduction to microservices
指针进阶---指针数组,数组指针
Circular reference of ES6 module
JS inheritance method
【MySQL】鎖
灰度升级 TiDB Operator
1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero
1204 character deletion operation (2)
延迟初始化和密封类