gpt4 book ai didi

python-3.x - 如何从 .h5 文件正确加载带有自定义层的 Keras 模型?

转载 作者:行者123 更新时间:2023-12-03 15:40:13 26 4
gpt4 key购买 nike

我用自定义层构建了一个 Keras 模型,并将其保存到 .h5文件由回调 ModelCheckPoint .
当我在训练后尝试加载此模型时,出现以下错误消息:

__init__() missing 1 required positional argument: 'pool_size'


这是自定义层的定义及其 __init__方法:

class MyMeanPooling(Layer):
def __init__(self, pool_size, axis=1, **kwargs):
self.supports_masking = True
self.pool_size = pool_size
self.axis = axis
self.y_shape = None
self.y_mask = None
super(MyMeanPooling, self).__init__(**kwargs)

这就是我将此层添加到我的模型的方式:

x = MyMeanPooling(globalvars.pool_size)(x)

这是我加载模型的方式:

from keras.models import load_model

model = load_model(model_path, custom_objects={'MyMeanPooling': MyMeanPooling})

这些是完整的错误消息:
Traceback (most recent call last):
File "D:/My Projects/Attention_BLSTM/script3.py", line 9, in <module>
model = load_model(model_path, custom_objects={'MyMeanPooling': MyMeanPooling})
File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\engine\saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\engine\saving.py", line 225, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\engine\saving.py", line 458, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\layers\__init__.py", line 55, in deserialize
printable_module_name='layer')
File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\utils\generic_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\engine\network.py", line 1022, in from_config
process_layer(layer_data)
File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\engine\network.py", line 1008, in process_layer
custom_objects=custom_objects)
File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\layers\__init__.py", line 55, in deserialize
printable_module_name='layer')
File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\utils\generic_utils.py", line 147, in deserialize_keras_object
return cls.from_config(config['config'])
File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\engine\base_layer.py", line 1109, in from_config
return cls(**config)
TypeError: __init__() missing 1 required positional argument: 'pool_size'

最佳答案

其实我不认为你可以加载这个模型。

最可能的问题是您没有实现 get_config()层中的方法。此方法返回应保存的配置值字典:

def get_config(self):
config = {'pool_size': self.pool_size,
'axis': self.axis}
base_config = super(MyMeanPooling, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

将此方法添加到您的层后,您必须重新训练模型,因为之前保存的模型没有保存该层的配置。这就是您无法加载它的原因,它需要在进行此更改后重新训练。

关于python-3.x - 如何从 .h5 文件正确加载带有自定义层的 Keras 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57506707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com