值错误:找不到要从 SavedModel 加载的匹配函数,并且"检查点加载状态"对象没有属性"预测"



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

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'

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

我能够解决我的问题。在装重物之前我还没有建立模型。因此,它没有初始化子类模型中的层,并给出错误为'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])

最新更新