当前位置:网站首页>13. Model saving and loading
13. Model saving and loading
2022-07-08 00:55:00 【booze-J】
article
We save 3.MNIST Data set classification Take the training model in as an example , To demonstrate the saving and loading of the model .
The first way to save and load models
1. Save the way
To save the model, you only need to add after the model training
# Save the model The structure and parameters of the model can be saved at the same time
model.save("model.h5") # HDF5 file ,pip install h5py
This saving method can save the structure and parameters of the model at the same time .
2. Loading mode
Before loading the model, you need to import load_model
Method
from keras.models import load_model
Then the loaded code is a simple sentence :
# Load model
model = load_model("../model.h5")
This loading method can load the structure and parameters of the model at the same time .
The second way to save and load models
1. Save the way
Model parameters and model structure are stored separately :
# Save parameters
model.save_weights("my_model_weights.h5")
# Save network structure
json_string = model.to_json()
2. Loading mode
Before loading the model structure , You need to import model_from_json()
Method
from keras.models import model_from_json
Load network parameters and network structure respectively :
# Load parameters
model.load_weights("my_model_weights.h5")
# Load model structure
model = model_from_json(json_string)
Model retraining
The code running platform is jupyter-notebook, Code blocks in the article , According to jupyter-notebook Written in the order of division in , Run article code , Glue directly into jupyter-notebook that will do .
In fact, the model can be retrained after loading .
1. Import third-party library
import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.models import load_model
2. Loading data and data preprocessing
# Load data
(x_train,y_train),(x_test,y_test) = mnist.load_data()
# (60000, 28, 28)
print("x_shape:\n",x_train.shape)
# (60000,) Not yet one-hot code You need to operate by yourself later
print("y_shape:\n",y_train.shape)
# (60000, 28, 28) -> (60000,784) reshape() Middle parameter filling -1 Parameter results can be calculated automatically Divide 255.0 To normalize
x_train = x_train.reshape(x_train.shape[0],-1)/255.0
x_test = x_test.reshape(x_test.shape[0],-1)/255.0
# in one hot Format
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)
3. Model retraining
# Load model
model = load_model("../model.h5")
# Evaluation model
loss,accuracy = model.evaluate(x_test,y_test)
print("\ntest loss",loss)
print("accuracy:",accuracy)
Running results :
Compare the first saved model :
It can be found that the accuracy of the retraining model on the test set has been improved .
边栏推荐
- Password recovery vulnerability of foreign public testing
- Marubeni official website applet configuration tutorial is coming (with detailed steps)
- letcode43:字符串相乘
- Application practice | the efficiency of the data warehouse system has been comprehensively improved! Data warehouse construction based on Apache Doris in Tongcheng digital Department
- Stock account opening is free of charge. Is it safe to open an account on your mobile phone
- 新库上线 | CnOpenData中国星级酒店数据
- [Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
- 他们齐聚 2022 ECUG Con,只为「中国技术力量」
- 【obs】官方是配置USE_GPU_PRIORITY 效果为TRUE的
- Prompt configure: error: required tool not found: libtool solution when configuring and installing crosstool ng tool
猜你喜欢
国外众测之密码找回漏洞
[necessary for R & D personnel] how to make your own dataset and display it.
Semantic segmentation model base segmentation_ models_ Detailed introduction to pytorch
51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up
SDNU_ ACM_ ICPC_ 2022_ Summer_ Practice(1~2)
My best game based on wechat applet development
13.模型的保存和载入
ReentrantLock 公平锁源码 第0篇
英雄联盟胜负预测--简易肯德基上校
大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
随机推荐
The method of server defense against DDoS, Hangzhou advanced anti DDoS IP section 103.219.39 x
[Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
【愚公系列】2022年7月 Go教学课程 006-自动推导类型和输入输出
基于人脸识别实现课堂抬头率检测
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
5g NR system messages
Hotel
RPA cloud computer, let RPA out of the box with unlimited computing power?
基于卷积神经网络的恶意软件检测方法
玩轉Sonar
Application practice | the efficiency of the data warehouse system has been comprehensively improved! Data warehouse construction based on Apache Doris in Tongcheng digital Department
How to learn a new technology (programming language)
Introduction to paddle - using lenet to realize image classification method I in MNIST
QT establish signal slots between different classes and transfer parameters
手机上炒股安全么?
New library launched | cnopendata China Time-honored enterprise directory
STL--String类的常用功能复写
《因果性Causality》教程,哥本哈根大学Jonas Peters讲授
Reentrantlock fair lock source code Chapter 0
Interface test advanced interface script use - apipost (pre / post execution script)