tf.estimator 服务功能失败

tf.estimator serving function failing

我正在使用 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",但我不知道如何操作。任何帮助将不胜感激

关注这个link

我在创建 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)