Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Tensorflow for serving a model, what does the serving input function supposed to do exactly

So, I've been struggling to understand what the main task of a serving_input_fn() is when a trained model is exported in Tensorflow for serving purposes. There are some examples online that explain it but I'm having problems defining it for myself.

The problem I'm trying to solve is a regression problem where I have 29 inputs and one output. Is there a template for creating a corresponding serving input function for that? What if I use a one-class classification problem? Would my serving input function need to change or can I use the same function?

And finally, do I always need serving input functions or is it only when I use tf.estimator to export my model?

like image 318
Vahid Zadeh Avatar asked Jan 29 '18 21:01

Vahid Zadeh


1 Answers

You need a serving input function if you want your model to be able to make predictions. The serving_input_fn specifies what the caller of the predict() method will have to provide. You are essentially telling the model what data it has to get from the user.

If you have 29 inputs, your serving input function might look like:

def serving_input_fn():
    feature_placeholders = {
      'var1' : tf.placeholder(tf.float32, [None]),
      'var2' : tf.placeholder(tf.float32, [None]),
      ...
    }
    features = {
        key: tf.expand_dims(tensor, -1)
        for key, tensor in feature_placeholders.items()
    }
    return tf.estimator.export.ServingInputReceiver(features, 
                                                    feature_placeholders)

This would typically come in as JSON:

{"instances": [{"var1": [23, 34], "var2": [...], ...}]}

P.S. The output is not part of the serving input function because this is about the input to predict. If you are using a pre-made estimator, the output is already predetermined. If you are writing a custom estimator, you'd write an export signature.

like image 130
Lak Avatar answered Sep 21 '22 01:09

Lak