[英]Export a model with tf Estimator and export_savedmodel function
[英]How to export Estimator model with export_savedmodel function
像這樣做:
your_feature_spec = {
"some_feature": tf.FixedLenFeature([], dtype=tf.string, default_value=""),
"some_feature": tf.VarLenFeature(dtype=tf.string),
}
def _serving_input_receiver_fn():
serialized_tf_example = tf.placeholder(dtype=tf.string, shape=None,
name='input_example_tensor')
# key (e.g. 'examples') should be same with the inputKey when you
# buid the request for prediction
receiver_tensors = {'examples': serialized_tf_example}
features = tf.parse_example(serialized_tf_example, your_feature_spec)
return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)
estimator.export_savedmodel(export_dir, _serving_input_receiver_fn)
然后,您可以批量請求帶有“預測”簽名名稱的服務模型。
資料來源: https : //www.tensorflow.org/guide/saved_model#prepare_serving_inputs
如果你直接從master分支使用tensorflow,那么有一個模塊tensorflow.python.estimator.export,它提供了一個函數:
from tensorflow.python.estimator.export import export
feature_spec = {'MY_FEATURE': tf.constant(2.0, shape=[1, 1])}
serving_input_fn = export.build_raw_serving_input_receiver_fn(feature_spec)
不幸的是,至少對我而言,它不會比這更進一步,但我不確定我的模型是否真的正確,所以也許你比我更幸運。
或者,從pypi安裝的當前版本有以下功能:
serving_input_fn = tf.contrib.learn.utils.build_parsing_serving_input_fn(feature_spec)
serving_input_fn = tf.contrib.learn.utils.build_default_serving_input_fn(feature_spec)
但我也無法讓他們工作。
可能,我沒有正確理解這一點,所以我希望你會有更多的運氣。
克里斯
你有2個選擇:
在我的mlengine-boilerplate存儲庫中 ,我使用它將估算器模型導出到Cloud ML Engine,以便在線預測(預測的示例代碼 )輕松使用它。 重要組成部分:
def serving_input_fn():
feature_placeholders = {
'id': tf.placeholder(tf.string, [None], name="id_placeholder"),
'feat': tf.placeholder(tf.float32, [None, FEAT_LEN], name="feat_placeholder"),
#label is not required since serving is only used for inference
}
return input_fn_utils.InputFnOps(
feature_placeholders,
None,
feature_placeholders)
本教程介紹如何使用export_savedmodel
為使用估算器實現的Wide&Deep模型提供服務,以及如何將Tensorflow示例提供給導出的模型。 至關重要的部分:
from tensorflow.contrib.learn.python.learn.utils import input_fn_utils
serving_input_fn = input_fn_utils.build_parsing_serving_input_fn(feature_spec)
你需要有tf.train.Example和tf.train.Feature並將輸入傳遞給輸入接收函數並調用模型。 你可以看看這個例子https://github.com/tettusud/tensorflow-examples/tree/master/estimators
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.