Search code examples
tensorflowtensorflow-servingtensorflow-transform

tf.estimator serving function failing


I am using the tf.estimator to train and serve my tensorflow model. the training completed as expected, but fails in serving. I read my data in as a TFRecordDataset. My parsing function applies a transformation to feature "x2". "x2" is a string that is split. the tranformed feature is "x3".

def parse_function(example_proto):
    features={"x1":tf.FixedLenFeature((), tf.string), "x2":tf.FixedLenFeature((), 
 tf.string),
         "label":tf.FixedLenFeature((), tf.int64)}
    parsed_features = tf.parse_example(example_proto, features)
    x3=tf.string_split(parsed_features["string"],',')
    parsed_features["x3"]=x3
    return parsed_features, parsed_features["label"]

My serving fucnction is

def serving_input_fn():
receiver_tensor = {}
for feature_name in record_columns:
    if feature_name in {"x1", "x2","x3"}:
        dtype = tf.string 
    else:
        dtype=tf.int32
    receiver_tensor[feature_name] = tf.placeholder(dtype, shape=[None])
features = {
    key: tf.expand_dims(tensor, -1)
    for key, tensor in receiver_tensor.items()
}
return tf.estimator.export.ServingInputReceiver(features, receiver_tensor)

It always worked in the past when I didn't have any transformations in my parsing function, but it fails now with the error.

cloud.ml.prediction.prediction_utils.PredictionError: Failed to run the provided model: Exception during running the graph: Cannot feed value of shape (2, 1) for Tensor u'Placeholder_2:0', which has shape '(?,)' (Error code: 2)

I think I have to apply the transformation to "x2" in my serving function, but I don't know how. Any help would be greatly appreciated


Solution

  • Following this link

    I processed feature "x3" after creating the receiver_tensor. Splitting the string in the serving fucntion required squeezing the tensor before splitting

    def serving_input_fn():
        receiver_tensor = {}
        receiver_tensor["x1"] = tf.placeholder(tf.string, shape=[None], name="x1")
        receiver_tensor["label"] = tf.placeholder(tf.int32, shape=[None], name="x2")
        receiver_tensor["x2"] = tf.placeholder(tf.string, shape=[None], 
          name="string")
    
        features = {
            key: tf.expand_dims(tensor, -1)
            for key, tensor in receiver_tensor.items()
        }
        features["x3"]=tf.string_split(tf.squeeze(features["x2"]),',')
        return tf.estimator.export.ServingInputReceiver(features, receiver_tensor)