我正在使用以下示例张量流排名:handling_sparse_features on colab
最后,它显示了如何使用训练后的模型的排名进行预测。很好。
def predict_input_fn(path):
context_feature_spec = tf.feature_column.make_parse_example_spec(
context_feature_columns().values())
example_feature_spec = tf.feature_column.make_parse_example_spec(
list(example_feature_columns().values()))
dataset = tfr.data.build_ranking_dataset(
file_pattern=path,
data_format=tfr.data.ELWC,
batch_size=_BATCH_SIZE,
list_size=_LIST_SIZE,
context_feature_spec=context_feature_spec,
example_feature_spec=example_feature_spec,
reader=tf.data.TFRecordDataset,
shuffle=False,
num_epochs=1)
features = tf.compat.v1.data.make_one_shot_iterator(dataset).get_next()
return features
predictions = ranker.predict(input_fn=lambda: predict_input_fn("/tmp/test.tfrecords"))
x = next(predictions)
如何保存和使用模型(或“等级”),这样我就不必始终执行整个培训步骤了?我想保存排名,然后按上面的方式使用它:
predictions = ranker.predict(input_fn=lambda: predict_input_fn("/tmp/test.tfrecords"))
我找到了以下解释:https://github.com/tensorflow/ranking/issues/53 在那里,他们提出以下建议:
feature_columns = example_feature_columns()
feature_spec = tf.feature_column.make_parse_example_spec(feature_columns)
export_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)
ranker.export_savedmodel("savedmodel", export_fn)
但这仅包括示例的feature_spec。对于我的问题,我需要同时输入示例和上下文的feature_spec,类似于predict_input_fn。