首页 > 解决方案 > tf.estimator 服务功能失败

问题描述

我正在使用 tf.estimator 来训练和服务我的 tensorflow 模型。培训按预期完成,但未能服务。我将我的数据作为 TFRecordDataset 读取。我的解析函数将转换应用于特征“x2”。“x2”是一个被拆分的字符串。转换后的特征是“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"]

我的服务功能是

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)

过去,当我在解析函数中没有任何转换时,它总是有效,但现在它因错误而失败。

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)

我想我必须在我的服务功能中将转换应用于“x2”,但我不知道如何。任何帮助将不胜感激

标签: tensorflowtensorflow-servingtensorflow-transform

解决方案


按照这个链接

创建receiver_tensor 后,我处理了特征“x3”。在服务函数中拆分字符串需要在拆分之前挤压张量

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)

推荐阅读