当前位置:网站首页>Solve notimplementederror: layer XX has arguments in`__ init__` and therefore must override `get_ config`

Solve notimplementederror: layer XX has arguments in`__ init__` and therefore must override `get_ config`

2022-06-11 08:07:00 sinysama

One 、 Wrong presentation

NotImplementedError: Layer XX has arguments in __init__ and therefore must override get_config.
(XX Represents a custom CLASS)

Two 、 The reason for the error

Use save After the method , Not re started at Class Custom properties in

3、 ... and 、 reason

There are two interfaces for model preservation ,save and save_weights Method .
The difference between the following :
save: Save the structure and parameters of the network model diagram .
save_weights: Save only the parameters of the network model .

If you use save Method , Self defined Class The statement inside requires get_config Reconfigure the declaration , otherwise Tensorflow Unable to save the diagram structure of the model ( As for why , I won't go into it , After all, I use other people's interfaces , Just know this feature ).

If you don't want to rewrite , It can be used save_weights Temporarily solve the problem of saving parameters , There would be no such mistake . But, after all, save() More comprehensive , In order to save trouble in the later period , It is recommended to use it ( Although it takes up a little memory ).

Four 、 Solution

solve save() The specific operation of error reporting is as follows :
In the definition of Class in , increase One get_config Function is used to update the configuration ( The specific operation is as follows ).
among __init__ All properties declared in , Need to be in get_config Function update once . See the picture below Middle part of circle .

get_config The template is as follows , Just replace it and run .

def get_config(self):
        config = super().get_config().copy()
        config.update({
    
            ' attribute 1': self. attribute 1,
            ' attribute 2': self. attribute 2,
            ' attribute 3': self. attribute 3,
        })
        return config

 Insert picture description here
Run again , The model was saved successfully !

原网站

版权声明
本文为[sinysama]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020515459716.html