Keras Model saving erroring: TypeError: get_config() missing 1 required positional argument: 'self'

Keras Model saving erroring: TypeError: get_config() missing 1 required positional argument: 'self'

我正在 运行ning keras tensorflow 2.0,我正在尝试创建一个模型,训练它,然后保存它。我的代码如下(简化):

    model = Model()

    model.save("saved_models/model1", overwrite=True, save_format="tf")

这里,Model()是一个class,子class是tf.keras.Model并实现了一个call(inputs)功能。

模型 class(下面要求)如下所示:

class Model(tf.keras.Model):
    def __init__(self, loadFile: str = None):
        super(Model, self).__init__()

        #TODO: transfer from above into here V
        self.conv1 = tf.keras.layers.Conv2D(filters=10,
                                            kernel_size=(10, 30),
                                            strides=(1, 30),
                                            padding="same",
                                            data_format="channels_last",
                                            activation=tf.keras.activations.relu,
                                            use_bias=True,
                                            kernel_initializer=tf.keras.initializers.glorot_normal,
                                            bias_initializer=tf.keras.initializers.zeros)

        self.pool1 = tf.keras.layers.MaxPool2D(pool_size=(2, 1),
                                               strides=(2, 1),
                                               padding="same",
                                               data_format="channels_last")

        self.conv2 = tf.keras.layers.Conv2D(filters=10,
                                            kernel_size=(4, 1),
                                            strides=(2, 1),
                                            padding="same",
                                            data_format="channels_last",
                                            activation=tf.keras.activations.relu,
                                            use_bias=True,
                                            kernel_initializer=tf.keras.initializers.glorot_normal,
                                            bias_initializer=tf.keras.initializers.zeros)

        self.pool2 = tf.keras.layers.MaxPool2D(pool_size=(2, 1),
                                               strides=(2, 1),
                                               padding="same",
                                               data_format="channels_last")

        self.flattened = tf.keras.layers.Flatten(data_format="channels_last")

        self.fcl1 = tf.keras.layers.Dense(units=500,
                                          activation=tf.keras.activations.softmax,
                                          use_bias=True,
                                          kernel_initializer=tf.keras.initializers.glorot_normal,
                                          bias_initializer=tf.keras.initializers.zeros)

        self.fcl2 = tf.keras.layers.Dense(units=4,
                                          activation=tf.keras.activations.softmax,
                                          use_bias=True,
                                          kernel_initializer=tf.keras.initializers.glorot_normal,
                                          bias_initializer=tf.keras.initializers.zeros)

        if loadFile is not None:
            self.load_weights(loadFile)

    def call(self, inputs, training: bool = False):
        conv1 = self.conv1(inputs)
        pool1 = self.pool1(conv1)
        conv2 = self.conv2(pool1)
        pool2 = self.pool2(conv2)

        flattened = self.flattened(pool2)
        fcl1 = self.fcl1(flattened)
        return self.fcl2(fcl1)

当我 运行 以上时,我收到以下警告:

W tensorflow/python/util/util.cc:280] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.

后面出现以下错误:

File "C:/Users/lavre/Desktop/Programming/Python/MyProject/neural_net/model.py", line 155, in <module>
  model.save(value, overwrite=True, save_format="tf")
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\engine\network.py", line 1213, in save
  saving.save_model(self, filepath, overwrite, include_optimizer, save_format)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\saving\save.py", line 106, in save_model
  saved_model.save(model, filepath, overwrite, include_optimizer)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\saving\saved_model.py", line 1492, in save
  save_lib.save(model, filepath)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\saved_model\save.py", line 849, in save
  saveable_view, asset_info.asset_index)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\saved_model\save.py", line 604, in _serialize_object_graph
  _write_object_proto(obj, obj_proto, asset_file_def_index)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\saved_model\save.py", line 643, in _write_object_proto
  metadata=obj._tracking_metadata)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 2195, in _tracking_metadata
  metadata['config'] = self.get_config()
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\layers\convolutional.py", line 252, in get_config
  'kernel_initializer': initializers.serialize(self.kernel_initializer),
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\initializers.py", line 166, in serialize
  return serialize_keras_object(initializer)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 140, in serialize_keras_object
  instance.get_config())
TypeError: get_config() missing 1 required positional argument: 'self'

为什么会出现此错误?还有第一个警告是什么意思?

改变你的初始化部分,你可以这样做:

self.conv1 = tf.keras.layers.Conv2D(filters=10,
                                        kernel_size=(10, 30),
                                        strides=(1, 30),
                                        padding="same",
                                        data_format="channels_last",
                                        activation=tf.keras.activations.relu,
                                        use_bias=True,
                                        kernel_initializer=tf.keras.initializers.glorot_normal(),
                                        bias_initializer=tf.keras.initializers.zeros())

像这样更改其他层,这对我的代码很有用。