Python 加载带有自定义图层的模型时出现Keras TypeError

Python 加载带有自定义图层的模型时出现Keras TypeError,python,tensorflow,keras,keras-layer,Python,Tensorflow,Keras,Keras Layer,我在正在保存的模型中有一个自定义Keras层。我想重新加载这个模型。这是我正在使用的代码: self.model = load_model(path, custom_objects={'MyLayer': MyLayer, 'custom_loss_fn': custom_loss_fn}) 这是我在模型中使用的自定义图层: class MyLayer(tf.keras.layers.Layer): def __init__(self, units, **kwargs):

我在正在保存的模型中有一个自定义Keras层。我想重新加载这个模型。这是我正在使用的代码:

self.model = load_model(path, custom_objects={'MyLayer': MyLayer, 'custom_loss_fn': custom_loss_fn})
这是我在模型中使用的自定义图层:

class MyLayer(tf.keras.layers.Layer):
    def __init__(self, units, **kwargs):
        super(MyLayer, self).__init__(units, **kwargs)
        self.W1 = tf.keras.layers.Dense(units)
        self.W2 = tf.keras.layers.Dense(units)
        self.V = tf.keras.layers.Dense(1)

    def call(self, values):
        ...

    def get_config(self):
        config = super().get_config()
        config.update({
          'w1': self.W1,
          'w2': self.W2,
          'v': self.V,
        })
        return config
当我尝试加载模型时,出现以下错误:

TypeError: __init__() missing 1 required positional argument: 'units'
为什么呢

更新
我使用kera的
ModelCheckpoint
回调保存模型。这可能是兼容性问题吗?

尝试相应地修改init函数

def __init__(self, units, **kwargs):    
   self.units = units
   ...
   super(MyCustomLayer, self).__init__(**kwargs)

如果将units=self.units放在init funct的顶部,会发生什么?你是说self.units=units?是的,这能解决问题吗?然后尝试将super()语句放在init function的底部。以上所有操作都不起作用。