目前我正在开发一个tensorflow模型。该模型根据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服务于这个模型,代码如下:
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编码的张量,并将其提供给我的模型吗?在训练我的模型时,我只是在喂它们之前用一个函数对它们进行了转换。在服务时,这似乎是不可能的,因为我只能定义输入函数,而不能定义输入数据流。
发布于 2019-04-09 22:49:01
tf.one_hot提供了一种热编码。
但是,更广泛地说,您需要协调训练和服务以使用相同的索引。Tensorflow Transform提供了在训练数据处理阶段执行许多转换(单热、缩放、分段)的方法,包括单热编码,并将转换保存为模型图的一部分,因此在服务时自动重新应用相同的转换,从而节省了您的手动工作。通过下面的链接查看他们的示例:
示例: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。
https://stackoverflow.com/questions/55271842
复制相似问题