TensorFlow加载Keras模型出现不一致引用警告,是否需处理?
问题:加载TFX Pusher保存的含自定义层Keras模型时的警告问题
加载通过TFX的Pusher组件保存的、包含自定义层的Keras模型时,出现多条如下警告:
WARNING:tensorflow:Inconsistent references when loading the checkpoint into this object graph. For example, in the saved checkpoint object, `model.layer.weight` and `model.layer_copy.weight` reference the same variable, while in the current object these are two different variables. The referenced variables are:(<keras.layers.core.dense.Dense object at 0x7f1d0c557190> and <keras.engine.functional.Functional object at 0x7f1d0c557f10>). WARNING:tensorflow:Inconsistent references when loading the checkpoint into this object graph. For example, in the saved checkpoint object, `model.layer.weight` and `model.layer_copy.weight` reference the same variable, while in the current object these are two different variables. The referenced variables are:(<keras.layers.core.dense.Dense object at 0x7f1d0c5577c0> and <Layers.SingleOutputWithName.SingleOutputWithName object at 0x7f1d0c4cde80>). WARNING:tensorflow:Inconsistent references when loading the checkpoint into this object graph. For example, in the saved checkpoint object, `model.layer.weight` and `model.layer_copy.weight` reference the same variable, while in the current object these are two different variables. The referenced variables are:(<keras.engine.functional.Functional object at 0x7f1d0c4cd7c0> and <keras.saving.legacy.saved_model.load.TensorFlowTransform>TransformFeaturesLayer object at 0x7f1d0c557e20>). WARNING:tensorflow:Inconsistent references when loading the checkpoint into this object graph. For example, in the saved checkpoint object, `model.layer.weight` and `model.layer_copy.weight` reference the same variable, while in the current object these are two different variables. The referenced variables are:(<keras.saving.legacy.saved_model.load.TensorFlowTransform>TransformFeaturesLayer object at 0x7f1d0c557e20> and <keras.engine.input_layer.InputLayer object at 0x7f1d0d5e1a00>). WARNING:tensorflow:Inconsistent references when loading the checkpoint into this object graph. For example, in the saved checkpoint object, `model.layer.weight` and `model.layer_copy.weight` reference the same variable, while in the current object these are two different variables. The referenced variables are:(<keras.engine.functional.Functional object at 0x7f1d0c4cd7c0> and <keras.saving.legacy.saved_model.load.TensorFlowTransform>TransformFeaturesLayer object at 0x7f1d0c557e20>).
目前模型能正常加载运行,但不确定这些警告是否会影响模型输出,想咨询这些警告是否重要,是否需要进行处理。
我的模型结构简单,未使用权重共享等复杂机制,仅包含如下自定义层:
import tensorflow as tf from tensorflow.keras import layers @tf.keras.utils.register_keras_serializable() class SingleOutputWithName(layers.Layer): def __init__(self, label_name: str, **kwargs): super(SingleOutputWithName, self).__init__() self.label_name = label_name def call(self, x): return {self.label_name: x, 'dummy': x} def get_config(self): config = super().get_config() config['label_name'] = self.label_name return config
回答
这些警告暂时不会影响模型的输出正确性,但属于TensorFlow加载模型时的内部引用不一致问题,具体分析和处理建议如下:
- 警告本质:模型保存时的对象图中,某些变量被多个层共享引用,但加载后这些引用指向了不同的对象实例。你的模型没有手动设置权重共享,大概率是TFX或Keras在保存/加载流程中自动生成的临时层(比如
TransformFeaturesLayer这类TFX相关预处理层)导致的内部引用差异。 - 影响判断:既然模型能正常加载运行,且你的自定义层
SingleOutputWithName本身没有可训练权重(仅做输出格式转换),这些引用不一致不会改变模型的计算逻辑和权重值,输出结果是可靠的。
如果想要消除警告,可以尝试以下方法:
- 确认加载模型时自定义层的注册正确(你已经用
@tf.keras.utils.register_keras_serializable()完成注册,这步没问题); - 用
tf.keras.models.load_model()加载时,指定compile=False(如果不需要重新编译模型),减少加载时的额外检查; - 检查TFX与TensorFlow的版本兼容性,部分版本组合会存在这类小问题,升级到稳定版可能解决。
总结:如果当前模型运行正常、输出符合预期,可以暂时不处理这些警告;若追求无警告的运行环境,再尝试上述优化方案。
内容的提问来源于stack exchange,提问作者Mehran
相关产品推荐
相关产品推荐

