当前位置:网站首页>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 .
‘’’
边栏推荐
- 学习开发没有捷径,也几乎不存在带路会学的快一些的情况
- Windows下MySQL的安装和删除
- 2. Elment UI date selector formatting problem
- Notes on C language learning of migrant workers majoring in electronic information engineering
- 新系列单片机还延续了STM32产品家族的低电压和节能两大优势
- Basic knowledge of MySQL database (an introduction to systematization)
- 自动装箱与拆箱了解吗?原理是什么?
- 【力扣刷题笔记(二)】特别技巧,模块突破,45道经典题目分类总结,在不断巩固中精进
- 內存數據庫究竟是如何發揮內存優勢的?
- 51 MCU tmod and timer configuration
猜你喜欢
Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
How does the memory database give full play to the advantages of memory?
03 fastjason solves circular references
There is no specific definition of embedded system
Pymssql controls SQL for Chinese queries
Project cost management__ Topic of comprehensive calculation
Retinaface: single stage dense face localization in the wild
I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first
要選擇那種語言為單片機編寫程序呢
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
随机推荐
(1) What is a lambda expression
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
Wireshark use
STM32 running lantern experiment - library function version
QT self drawing button with bubbles
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
4G module IMEI of charging pile design
STM32 general timer output PWM control steering gear
Project cost management__ Plan value_ Earned value_ Relationship among actual cost and Countermeasures
Introduction to chromium embedded framework (CEF)
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
【力扣刷题笔记(二)】特别技巧,模块突破,45道经典题目分类总结,在不断巩固中精进
STM32 interrupt switch
Working mode of 80C51 Serial Port
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
自動裝箱與拆箱了解嗎?原理是什麼?
Installation and removal of MySQL under Windows
After clicking the Save button, you can only click it once
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇