当前位置:网站首页>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对模型加载,
边栏推荐
猜你喜欢
Leetcode question brushing (5.28) hash table
IoT -- 解读物联网四层架构
【MySQL】日志
ESP系列引脚說明圖匯總
On the day of resignation, jd.com deleted the database and ran away, and the programmer was sentenced
Roguelike游戏成破解重灾区,如何破局?
延迟初始化和密封类
Cisp-pte practice explanation
2022.02.13 - NC002. sort
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
随机推荐
Golang force buckle leetcode 1020 Number of enclaves
IP lab, the first weekly recheck
延迟初始化和密封类
1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero
Unified ordering background interface product description Chinese garbled
Bottom up - physical layer
Modify the video name from the name mapping relationship in the table
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
ESP series pin description diagram summary
Precise query of tree tree
Beijing invitation media
从 TiDB 集群迁移数据至另一 TiDB 集群
Use br to back up tidb cluster data to S3 compatible storage
灰度升级 TiDB Operator
Upgrade tidb operator
From monomer structure to microservice architecture, introduction to microservices
MySQL learning records 12jdbc operation transactions
Leetcode question brushing (5.31) string
Browser thread
MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object