如何使用export_savedmodel函数导出Estimator模型

Yuw*_*Yan 16 python tensorflow tensorflow-serving

有没有可用的教程export_savedmodel?

我走过了这篇文章的tensorflow.org和单元测试代码上github.com,仍然没有关于如何构造参数想法serving_input_fn的功能export_savedmodel

小智 10

像这样做:

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)
Run Code Online (Sandbox Code Playgroud)

然后,您可以批量请求带有"预测"签名名称的服务模型.

资料来源:https://www.tensorflow.org/guide/saved_model#prepare_serving_inputs


小智 2

如果您直接从主分支使用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)
Run Code Online (Sandbox Code Playgroud)

不幸的是,至少对我来说,它不会比这更进一步,但我不确定我的模型是否真的正确,所以也许你比我有更多的运气。

或者,从 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)
Run Code Online (Sandbox Code Playgroud)

但我也无法让他们工作。

可能,我没有正确理解这一点,所以我希望你能有更多的运气。

克里斯