当前位置:网站首页>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 .
边栏推荐
- Huawei switch s5735s-l24t4s-qa2 cannot be remotely accessed by telnet
- 哪个券商公司开户佣金低又安全,又靠谱
- [reprint] solve the problem that CONDA installs pytorch too slowly
- Codeforces Round #804 (Div. 2)(A~D)
- Binder core API
- RPA cloud computer, let RPA out of the box with unlimited computing power?
- DNS series (I): why does the updated DNS record not take effect?
- Solution to the problem of unserialize3 in the advanced web area of the attack and defense world
- Introduction to paddle - using lenet to realize image classification method II in MNIST
- How to insert highlighted code blocks in WPS and word
猜你喜欢

Kubernetes Static Pod (静态Pod)

Jemter distributed

基于微信小程序开发的我最在行的小游戏

5G NR 系统消息

The standby database has been delayed. Check that the MRP is wait_ for_ Log, apply after restarting MRP_ Log but wait again later_ for_ log

C # generics and performance comparison

Jouer sonar

Operating system principle --- summary of interview knowledge points

letcode43:字符串相乘

From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
随机推荐
韦东山第二期课程内容概要
手机上炒股安全么?
国外众测之密码找回漏洞
Experience of autumn recruitment in 22 years
They gathered at the 2022 ecug con just for "China's technological power"
What is load balancing? How does DNS achieve load balancing?
什么是负载均衡?DNS如何实现负载均衡?
After going to ByteDance, I learned that there are so many test engineers with an annual salary of 40W?
Service Mesh介绍,Istio概述
DNS series (I): why does the updated DNS record not take effect?
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
Qt添加资源文件,为QAction添加图标,建立信号槽函数并实现
接口测试要测试什么?
tourist的NTT模板
13.模型的保存和载入
大二级分类产品页权重低,不收录怎么办?
Marubeni official website applet configuration tutorial is coming (with detailed steps)
新库上线 | CnOpenData中国星级酒店数据
What if the testing process is not perfect and the development is not active?
股票开户免费办理佣金最低的券商,手机上开户安全吗