当前位置:网站首页>Tensorflow2.0 save model
Tensorflow2.0 save model
2022-07-03 10:03:00 【Master Hao】
import tensorflow as tf
import json
import tensorflow.keras.models
# This is a separate package
import tensorflow.keras
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation=‘relu’),
tf.keras.layers.Dense(10, activation=‘softmax’)
])
model.compile(optimizer=‘adam’,
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=[‘sparse_categorical_accuracy’])
model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)
model.summary()
‘’’
# Save as h5 Model : Use model.save To save the model , The structure of the model 、 Weight parameters and optimizer settings , Setting of cost function ,metrics All settings of are saved
# Extraction method :tensorflow.keras.models.load_model(“mymodel.h5”)
#model.save(“mymodel.h5”)
‘’’
‘’’
#SavedModel yes Tensorflow A model format in , Its advantage is that it has nothing to do with language , No addition h5 suffix ,pb structure ,variable A weight , Can be used in addition to Sequential Models other than
#model.save(“mymodel”)
# Just want to save the structure of the model , Without saving the weight of the model 、 Optimizer and cost function , The main use of json
‘’’
‘’’
json_config=model.to_json()
with open(“model.json”,“w”)as m:
json.dump(json_config,m)
# Read the model :
with open(“model.json”) as m:
json_config=json.load(m)
model2=tensorflow.keras.models.model_from_json(json_config)
model2.summary()
‘’’
‘’’
Save only the parameters of the model :
model.save_weights(‘path_to_my_model.h5’)
# Onload , You need to use json Import model structure , Or define the model structure
model.load_weights(‘path_to_my_model.h5’)
# You also need to define compile The content in , Including optimizer and cost function . because model.save() Will save compile The content in ,
and model.save_weights Only the parameters of the model will be saved . therefore ,load_weights It needs to be redefined in the future compile The content of , In this way, the model can be further trained .
‘’’
边栏推荐
- (1) What is a lambda expression
- How does the memory database give full play to the advantages of memory?
- SCM is now overwhelming, a wide variety, so that developers are overwhelmed
- Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
- 03 fastjason solves circular references
- Crash工具基本使用及实战分享
- Fundamentals of Electronic Technology (III)__ Chapter 6 combinational logic circuit
- 4G module board level control interface designed by charging pile
- SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers
- Retinaface: single stage dense face localization in the wild
猜你喜欢

Getting started with JMX, MBean, mxbean, mbeanserver

Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction

Not many people can finally bring their interests to college graduation

我想各位朋友都应该知道学习的基本规律就是:从易到难

When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn

03 FastJson 解决循环引用

Quelle langue choisir pour programmer un micro - ordinateur à puce unique

Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction

The third paper of information system project manager in soft examination

在三线城市、在县城,很难毕业就拿到10K
随机推荐
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
Project cost management__ Topic of comprehensive calculation
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
(2)接口中新增的方法
(2) New methods in the interface
How does the memory database give full play to the advantages of memory?
单片机学到什么程度能找到工作,这个标准不好量化
内存数据库究竟是如何发挥内存优势的?
Serial port programming
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
RESNET code details
2020-08-23
Introduction to chromium embedded framework (CEF)
(1) What is a lambda expression
QT is a method of batch modifying the style of a certain type of control after naming the control
03 fastjason solves circular references
In third tier cities and counties, it is difficult to get 10K after graduation
03 FastJson 解决循环引用
Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
自動裝箱與拆箱了解嗎?原理是什麼?