You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

加载Keras LTC模型时遇'adjacency_matrix'参数不识别错误的解决求助

解决Keras LTC模型加载时的adjacency_matrix参数错误

问题分析

加载模型时出现('Keyword argument not understood:', 'adjacency_matrix'),是因为模型保存时会序列化LTCCell依赖的AutoNCP wiring对象,其中包含adjacency_matrix等参数,但加载时仅指定LTCCell在custom_objects中,缺少AutoNCP类的定义,且LTCCell默认的反序列化逻辑无法处理这些参数。

解决方案

方案1:补充CustomObjects包含AutoNCP类

修改加载代码,将wirings.AutoNCP也加入custom_objects,让Keras能正确重建wiring实例:

custom_objects = {'LTCCell': LTCCell, 'AutoNCP': wirings.AutoNCP}
model = tf.keras.models.load_model(model_path, custom_objects=custom_objects)

方案2:重写LTCCell的from_config方法

如果方案1无效,自定义LTCCell的反序列化逻辑,手动处理adjacency_matrix参数重建wiring:

class CustomLTCCell(LTCCell):
    @classmethod
    def from_config(cls, config):
        # 提取wiring的配置参数
        wiring_config = config.pop('wiring')
        # 重建AutoNCP实例
        wiring = wirings.AutoNCP(**wiring_config)
        # 传入重建的wiring初始化CustomLTCCell
        return cls(wiring, **config)

# 加载时使用自定义的CustomLTCCell
custom_objects = {'LTCCell': CustomLTCCell, 'AutoNCP': wirings.AutoNCP}
model = tf.keras.models.load_model(model_path, custom_objects=custom_objects)

方案3:改用TensorFlow SavedModel格式保存/加载

尝试使用TF原生的SavedModel格式(文件夹形式)替代.h5.keras,提升自定义层兼容性:

# 保存模型时(替换原保存代码)
model.save(model_path.replace('.h5.keras', ''), save_format='tf')

# 加载模型时
model = tf.keras.models.load_model(model_path.replace('.h5.keras', ''), custom_objects=custom_objects)

额外验证:确保LTCCell正确序列化wiring

如果原LTCCell的get_config方法未正确包含wiring配置,需要重写:

class LTCCell(LTCCell):
    def get_config(self):
        config = super().get_config()
        # 将wiring的配置加入模型配置
        config['wiring'] = self.wiring.get_config()
        return config

修改后重新训练并保存模型,再尝试加载。

内容的提问来源于stack exchange,提问作者Ana Flávia dos Reis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 01:36:16