【发布时间】:2020-12-19 21:35:22
【问题描述】:
我试图从加载的 tensorflow 模型中进行预测。虽然我不确定我之前保存它的方式是否正确,但特别是我对 serving_input_fn() 函数 (MAX_SEQ_LENGTH=128) 中的代码有疑问:
def serving_input_fn():
feature_spec = { "input_ids" : tf.FixedLenFeature([None,MAX_SEQ_LENGTH], tf.int64),
"input_mask" : tf.FixedLenFeature([None,MAX_SEQ_LENGTH], tf.int64),
"segment_ids" : tf.FixedLenFeature([None,MAX_SEQ_LENGTH], tf.int64),
"label_ids" : tf.FixedLenFeature([None], tf.int64) }
serialized_tf_example = tf.placeholder(dtype=tf.string,shape=[None],name='input_example_tensor')
receiver_tensors = {'example': serialized_tf_example}
features = tf.parse_example(serialized_tf_example, feature_spec)
return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)
estimator.export_saved_model('gs://bucket/trained_model', serving_input_receiver_fn=serving_input_fn)
当我尝试从加载的模型中预测时:
from tensorflow.contrib import predictor
predict_fn = predictor.from_saved_model(LOAD_PATH)
input_features_test = convert_examples_to_features( test_examples,label_list, MAX_SEQ_LENGTH, tokenizer)
predictions = predict_fn({'example':input_features_test[0]})
它返回此错误:
ValueError: 无法为张量提供形状 () 的值 'input_example_tensor:0',形状为'(?,)'
我应该如何更改 serving_input_fn() 方法?
如果你想重现它:github_repo(你应该从here下载变量并将它放在trained_model/1608370941/文件夹中)
This 是我在谷歌云 TPU 上微调 BERT 模型的教程。
【问题讨论】:
标签: python tensorflow bert-language-model tensorflow1.15