【发布时间】: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_weights 和load_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