Tensorflow:创建从稀疏值读取的服务输入函数

时间:2018-07-16 18:55:35

标签: python tensorflow tensorflow-transform

我正在使用来自tf.Transform的预处理数据在Tensorflow中创建一个估算器。为了提供服务,我希望能够直接从JSON文件读取,如下所示:

{"uid":"123","item_ids":["23","f4",6g"],"top_k":5}

top_k值不是模型图的一部分,仅在服务时间用于估算器中。我的问题是我不知道代表稀疏张量的值item_ids

我的服务功能如下:

def serving_input_fn():
    inputs = {}
    for key, t in zip(metadata.SERVING_CATEGORICAL_COLUMNS, metadata.SERVING_CATEGORICAL_DEFAULTS):
        # here should read the list values as sparse placeholders but 
        # sparse_placeholder produces a SparseTensor which
        # produce the error ValueError: receiver_tensor item_ids must be a Tensor.
        # in ServingInputReceiver
        if isinstance(t, list):
            inputs.update({key:tf.placeholder(shape=[None], dtype=tf.string)}) 
        else:
            inputs.update({key:tf.placeholder(shape=[None], dtype=tf.string)})

    for key, t in zip(metadata.SERVING_NUMERICAL_COLUMNS, metadata.SERVING_NUMERICAL_DEFAULTS):
        if isinstance(t, list):
            inputs.update({key:tf.placeholder(shape=[None], dtype=tf.float32)})
        else:
            inputs.update({key:tf.placeholder(shape=[None], dtype=tf.float32)})

    features = {}
    for key, tensor in inputs.items():
        if isinstance(tensor, tf.SparseTensor):
            features[key] = tensor
        else:
            features[key] = tf.expand_dims(tensor, -1)

    # here because the top_k value was not part of the preprocessing using
    # tf.Transform I pass only the values that need preprocessing
    raw_features_to_transform = {k: features[k] for k in metadata.INPUT_FEATURE_NAMES}
    transformed_features = tf_transform_output.transform_raw_features(
        raw_features_to_transform)
    transformed_features = {k: transformed_features[k] for k in metadata.SERVING_COLUMNS if k in transformed_features}
    features.update(transformed_features)

    return tf.estimator.export.ServingInputReceiver(
        features=features,
        receiver_tensors=inputs
    )

当我使用gcloud ml-engine local predict运行上面生成的图形时,出现以下错误

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

其中Placeholder_1:0item_ids值的占位符。

关于如何支持可变长度值的任何想法?

2 个答案:

答案 0 :(得分:0)

占位符的定义方式可能存在问题。 请看这个例子: https://github.com/tensorflow/transform/blob/v0.8.0/examples/sentiment_example.py#L271

我们的示例使用的是使用功能说明的解析服务输入功能。 它是从原始元数据中获取功能规范的,但是在您的情况下,由于您不解析输入数据,因此可以执行类似的操作并调用:

RAW_DATA_METADATA.schema.as_batched_placeholders()

为您定义占位符。

答案 1 :(得分:0)

调用tf.placeholder(shape=[None], ...)将创建一个占位符,其形状是长度未知的向量。创建占位符时,您可能希望指定tf.placeholder(shape=None, ...)来指定完全未知的形状(可能是矩阵,这是您在投放时传递的内容)。