当前位置:网站首页>Deep learning keras save model (when custom layer is included)
Deep learning keras save model (when custom layer is included)
2022-07-22 04:30:00 【V, Chao】
20210409 -
0. introduction
Generally speaking , If there are no special circumstances , When saving the model , By calling some api Just save it , In the article 《 Summary of basic knowledge and problems of in-depth learning 》 A way is briefly introduced , Save the model directly , And reload .
But in today's experiment , The emerging demand is , If in the defined model , If the user-defined layer is included, an error will be reported , See [1], There is no problem saving the model , But an error will be reported when loading .
Briefly explain the experimental environment
python 3.6.8
tensorflow-gpu 2.3.1
Keras 2.4.3
1. Error in loading model : Undefined layer
stay [1] in , If there is no series of regulations for the customized layer , When loading the model , An error will be reported as the definition layer .
ValueError: Unknown layer: CustomLayer
The above question uses api yes load_model
, Load the whole model and various weights in this way , For this kind of mistake , There are two ways to solve . Questions and answers [2] It's all mentioned in , One is when loading the model , stay api Specify custom classes , as follows :
new_model = tf.keras.models.load_model('model.h5', custom_objects={'CustomLayer': CustomLayer})
Maybe if you use a custom loss function , You also need to transfer this part of the content .
The other method is more convenient , If multiple customized contents are defined :
import tensorflow as tf
@tf.keras.utils.register_keras_serializable()
class CustomLayer(tf.keras.layers.Layer):
def __init__(self, k, **kwargs):
self.k = k
super(CustomLayer, self).__init__(**kwargs)
def get_config(self):
config = super().get_config()
config["k"] = self.k
return config
def call(self, input):
return tf.multiply(input, 2)
Add modifiers to the custom layer .
Look for the , Here, when he customizes the class , The above functions to be implemented . most important of all , Put your own parameters in the function get_config
To save . For details, see [2] The way , You can use his programming method as a template , Save the model , It also contains the content of parameter initialization , And you can see in build
Introduced in the process add_weight
No use get_config
In a statement .
2. Save weights
In this experiment , The last way to use is to just save the weights , In any case, we have to define this model first . The way to use is to add a callback function to save the model during training .
Reference resources
[1]Saving Keras models with Custom Layers
[2]Not able to load a saved model with custom layer
边栏推荐
猜你喜欢
XXL job (2.3.0) distributed task bean mode, glue shell scheduling practice, source code debug
Welcome to CSDN markdown editor - first use of editing template
Implementing DDD based on ABP -- domain service, application service and dto practice
inoic4学习笔记2
How to use the download tool proayee down
Minio file system 8.0.3
菜鸟同事贵我2k,原是背了涨薪秘籍?(赶紧收藏!)
Argocd user management, RBAC control, command line login, APP synchronization
ionic4学习笔记4--新增一个tab页面
Musk claims to be on the brain. Is it science or deception?
随机推荐
inoic4学习笔记2
highcharts图表及报表展示、导出数据
9.BOM对象?
【综合笔试题】难度 3.5/5,多解法热门二叉树笔试题
How to do the interface test in JSON format?
Matlab r2014a help file cannot be copied
Argocd user management, RBAC control, command line login, APP synchronization
ionic4学习笔记3
Why do Internet manufacturers love to "deliver takeout"?
Mysql数据库入门
年中盘点 | 2022年,PaaS 再升级
7.字符编码?
ionic4学习笔记4--新增一个tab页面
封装函数baseData.js
[零基础] 减少50%的bug,只需要30分钟...
Flex layout example
Quickly determine whether a file has a virus
Download tools - Google plug-ins tempermonkey and greasyfork
日期函数格式转换
Import and export of vmvare virtual machine (OVA format)