Reputation: 195
Currently I am working on a tensorflow model. This model classifies a situation based on 2 string and a number. So my placeholders look as follows:
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")
I want to serve this model with Tensorflow Serving with this code:
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
})
But the model I wrote want the strings as one_hot coded input. Does someone knows how to transform the input tensors to one_hot coded ones, and feed those to my model? While training my model, I just transformed them with a function before feeding them. This seems not possible while serving because I can only define a input function, not the flow of the inputdata.
Upvotes: 0
Views: 692
Reputation: 492
tf.one_hot provide the one hot encoding.
However, more broadly, you need to coordinate the training and serving to use the same index. Tensorflow Transform provides the way to do many transformations (one-hot, scale, bucketize), including one-hot encoding, at training data processing phase, and save the transformation as part of the model graph, therefore automatically re-apply the same transformation at the serving time, saving you the manual work. Check out their example with the link below:
Example: https://www.tensorflow.org/tfx/transform/tutorials/TFT_simple_example
Example 2: https://github.com/tensorflow/transform/blob/master/examples/sentiment_example.py
The Full Python API: https://www.tensorflow.org/tfx/transform/api_docs/python/tft
The function you look for there is tft.compute_and_apply_vocabulary.
Upvotes: 1