【问题标题】:Tensorflow Serving Transform data from input function来自输入函数的 Tensorflow Serving 转换数据
【发布时间】:2019-08-11 19:13:22
【问题描述】:

目前我正在研究一个张量流模型。该模型根据 2 个字符串和一个数字对情况进行分类。所以我的占位符如下所示:

Input1 = tf.placeholder("string", shape=None, name="string1")
Input2 = tf.placeholder("string", shape=None, name="string2")
Input3 = tf.placeholder("float", shape=None, name="distance")
label = tf.placeholder("int64", shape=None, name="output")

我想通过 TensorFlow Serving 使用以下代码为该模型提供服务:

    signature_definition = tf.saved_model.signature_def_utils.build_signature_def(
        inputs={'input1': model_input1, 'input2': model_input2, 'input3': model_input3},
        outputs={'outputs': model_output},
        method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)

    builder = tf.saved_model.builder.SavedModelBuilder(SERVE_PATH)

    builder.add_meta_graph_and_variables(
        sess, [tf.saved_model.tag_constants.SERVING],
        signature_def_map={
            tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
                signature_definition
        }) 

但我编写的模型希望将字符串作为 one_hot 编码输入。有人知道如何将输入张量转换为 one_hot 编码的张量,并将它们提供给我的模型吗? 在训练我的模型时,我只是在喂它们之前用一个函数对它们进行了转换。这在服务时似乎是不可能的,因为我只能定义一个输入函数,而不是输入数据的流。

【问题讨论】:

    标签: python tensorflow tensorflow-serving


    【解决方案1】:

    tf.one_hot 提供 one hot 编码。

    但是,更广泛地说,您需要协调训练和服务以使用相同的索引。 Tensorflow Transform 提供了在训练数据处理阶段进行许多转换(one-hot、scale、bucketize)的方法,包括 one-hot 编码,并将转换保存为模型图的一部分,因此自动重新应用相同的转换在服务时间,为您节省体力劳动。通过以下链接查看他们的示例:

    示例:https://www.tensorflow.org/tfx/transform/tutorials/TFT_simple_example

    示例 2:https://github.com/tensorflow/transform/blob/master/examples/sentiment_example.py

    完整的 Python API:https://www.tensorflow.org/tfx/transform/api_docs/python/tft

    你要找的函数是 tft.compute_and_apply_vocabulary。

    【讨论】:

    • 感谢您的帮助!这正是我所需要的
    • 有什么方法可以自定义服务输入接收器来获取 json 输入而不是 tf.example?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 2020-03-27
    • 2018-05-09
    • 2015-05-27
    • 2019-12-06
    相关资源
    最近更新 更多