当前位置:网站首页>Pytorch preserves different forms of pre training models
Pytorch preserves different forms of pre training models
2022-04-23 20:47:00 【NuerNuer】
Be careful , suffix .pt and .pth It doesn't seem to make any difference
When saving, you can save the whole model or only parameters , You can also build a new dictionary and save it again , This is also true. This corresponds to the need to do different processing when reading , When we load load_state_dict The argument to the function is zero OrderedDict Parameters of type , Here are Four different storage methods and their reading get OrderedDict The way .
1. preservation
# coding=gbk
import torch
import torch.nn as nn
class MLP_(nn.Module):
def __init__(self):
super(MLP_, self).__init__()
self.hidden = nn.Linear(3, 2)
self.act = nn.ReLU()
self.output = nn.Linear(2, 1)
def forward(self, x):
a = self.act(self.hidden(x))
return self.output(a)
net = MLP_()
# Save the entire model
torch.save(net, 'a1.pt')
all_model = {'model':net} # Add key values to the model part , So if you want to save the optimizer parameters , You can add new values to the dictionary
torch.save(all_model, 'a2.pt')
# Save only parameters
torch.save(net.state_dict(),'a3.pt')
all_states = {'state_dict': net.state_dict()} # Add key values to the model parameters section , So if you want to save the optimizer parameters , You can add new values to the dictionary
torch.save(all_states, 'a4.pt')
2. load
# coding=gbk
import torch
from save import MLP_
if __name__ == "__main__":
with torch.no_grad():
a1 = 'a1.pt'
a2 = 'a2.pt'
a3 = 'a3.pt'
a4 = 'a4.pt'
a1_ = torch.load(a1)
print(a1_.state_dict())
a2_ = torch.load(a2)['model'] # Select the corresponding value through the key value
print(a2_.state_dict())
a3_ = torch.load(a3)
print(a3_)
a4_ = torch.load(a4)['state_dict'] # Select the corresponding value through the key value
print(a4_)
Reference resources :https://zhuanlan.zhihu.com/p/94971100
版权声明
本文为[NuerNuer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210545523057.html
边栏推荐
- How to do after winning the new debt? Is it safe to open an account online
- How to configure SSH public key in code cloud
- UKFslam
- 浅谈数据库设计之三大范式
- Awk example skills
- 一些接地气的话儿
- Identifier CV is not defined in opencv4_ CAP_ PROP_ FPS; CV_ CAP_ PROP_ FRAME_ COUNT; CV_ CAP_ PROP_ POS_ Frames problem
- PHP的Laravel与Composer部署项目时常见问题
- SQL: query duplicate data and delete duplicate data
- On IRP from the perspective of source code
猜你喜欢
A login and exit component based on token
Commande dos pour la pénétration de l'Intranet
What about laptop Caton? Teach you to reinstall the system with one click to "revive" the computer
Lunch on the 23rd day at home
一些接地气的话儿
PHP的Laravel与Composer部署项目时常见问题
Leetcode 542, 01 matrix
Fastdfs思维导图
Summary and effect analysis of methods for calculating binocular parallax
Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high
随机推荐
Learn to C language fourth day
缓存淘汰算法初步认识(LRU和LFU)
go interface
电脑越用越慢怎么办?文件误删除恢复方法
C knowledge
Vscode download speed up
LeetCode 1351、统计有序矩阵中的负数
3-5 obtaining cookies through XSS and the use of XSS background management system
go array
How to do after winning the new debt? Is it safe to open an account online
go map
中创存储|想要一个好用的分布式存储云盘,到底该怎么选
MySQL进阶之表的增删改查
100天拿下11K,转岗测试的超全学习指南
Unity ECS dots notes
Mysql database common sense storage engine
Leetcode 1337. Row K with the weakest combat effectiveness in the matrix
Leetcode 20. Valid parentheses
Elastic box model
Preliminary understanding of cache elimination algorithm (LRU and LFU)