【问题标题】:ValueError: Could not find matching function to call loaded from the SavedModel and 'CheckpointLoadStatus' object has no attribute 'predict'ValueError:找不到匹配的函数来调用从 SavedModel 加载的调用,并且“CheckpointLoadStatus”对象没有属性“predict”
【发布时间】:2021-05-25 10:19:12
【问题描述】:

我正在努力将评论分类为多个标签,并通过引用此code 构建了一个多标签文本分类器。分类模型基于 Bert 文本模型。我遇到了一个关于如何通过训练模型预测看不见的数据的问题,并发布了一个问题here。根据那里提供的解决方案,尝试保存我的模型并通过以下方式加载它。

text_model.save('/tmp/model')
loaded_model=tf.keras.models.load_model('/tmp/model')

result = loaded_model.predict(np.asarray(item))

当我尝试使用加载的模型预测看不见的数据时,出现以下错误。

ValueError: Could not find matching function to call loaded from the SavedModel. Got:
  Positional arguments (2 total):
    * Tensor("inputs:0", shape=(None, 1), dtype=int64)
    * False
  Keyword arguments: {}

Expected these arguments to match one of the following 4 option(s):

Option 1:
  Positional arguments (2 total):
    * TensorSpec(shape=(None, None), dtype=tf.int32, name='input_1')
    * False
  Keyword arguments: {}

Option 2:
  Positional arguments (2 total):
    * TensorSpec(shape=(None, None), dtype=tf.int32, name='inputs')
    * True
  Keyword arguments: {}

Option 3:
  Positional arguments (2 total):
    * TensorSpec(shape=(None, None), dtype=tf.int32, name='inputs')
    * False
  Keyword arguments: {}

Option 4:
  Positional arguments (2 total):
    * TensorSpec(shape=(None, None), dtype=tf.int32, name='input_1')
    * True
  Keyword arguments: {}

在研究了相同的案例后,我尝试使用save_weightsload_weights。代码如下

text_model.save_weights("model.hd5") 

loaded_model=TEXT_MODEL(vocabulary_size=VOCAB_LENGTH,
                        embedding_dimensions=EMB_DIM,
                        cnn_filters=CNN_FILTERS,
                        dnn_units=DNN_UNITS,
                        model_output_classes=OUTPUT_CLASSES,
                        dropout_rate=DROPOUT_RATE
                        )
loaded_model=text_model.load_weights('model.hd5')
result = loaded_model.predict(np.asarray(item1))

它给了我一个错误'CheckpointLoadStatus' object has no attribute 'predict'

如果这段代码还不够我已经在this问题中提供了模型的实现和训练部分的代码。

【问题讨论】:

    标签: tensorflow machine-learning keras save tf.keras


    【解决方案1】:

    我能够解决我的问题。在加载权重之前我还没有建立模型。因此,它没有初始化子类模型中的层,并给出了错误'CheckpointLoadStatus' object has no attribute 'predict'。下面的代码展示了我是如何通过应用 build() 方法解决这个问题的。

    text_model.save_weights("model.h5")
    new_model=TEXT_MODEL(vocabulary_size=VOCAB_LENGTH,
                            embedding_dimensions=EMB_DIM,
                            cnn_filters=CNN_FILTERS,
                            dnn_units=DNN_UNITS,
                            model_output_classes=OUTPUT_CLASSES,
                            dropout_rate=DROPOUT_RATE
                            )
    new_model.build((2487,260))
    new_model.load_weights('model.h5')
    result = new_model.predict([item1])
    

    【讨论】:

      猜你喜欢
      • 2019-12-25
      • 2020-08-28
      • 1970-01-01
      • 2020-02-22
      • 2020-07-08
      • 2020-09-01
      • 2021-07-11
      • 1970-01-01
      • 2022-07-27
      相关资源
      最近更新 更多