tf.estimator服务函数失败

问题描述 投票:0回答:1

我正在使用tf.estimator训练并服务我的张量流模型。培训已按预期完成,但无法提供服务。我将数据读取为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”,但我不知道如何。任何帮助将不胜感激

tensorflow tensorflow-serving tensorflow-transform
1个回答
0
投票

跟随此link

创建了receive_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)
© www.soinside.com 2019 - 2024. All rights reserved.