如何使用自定义模型通过 ml-engine 在批量预测中获得 'keys'?

How to get 'keys' in batch predictions with ml-engine using a custom model?

我一直致力于部署自定义估算器(tensorflow 模型)。在 ml-engine 上训练后一切正常,但是当在批处理模型中使用 ml-engine 预测时,我无法获得密钥(或原始输入的任何 id),因为您知道批处理预测处于分布式模式并且 "keys"有助于理解哪些预测对应。我发现这个 post 解决了这个问题,但是使用了预制(罐装)tensorflow 模型(人口普查用例)。 如何调整我的自定义模型 (tf.contrib.learn.Estimator()) 以便在预测中获得 "keys"? 我的输出文件示例:

{"predicted": [0.04930919408798218, 0.05402487516403198, 0.059984803199768066, 0.017936021089553833]}

而我的模型函数如下:

SEQ_LEN = 12
DEFAULTS = [[0.0] for x in range(0, SEQ_LEN)]
BATCH_SIZE = 32
TIMESERIES_COL = 'rawdata'
N_OUTPUTS = 4  # in each sequence, 1-8 are features, and 9-12 are labels
N_INPUTS = SEQ_LEN - N_OUTPUTS
LSTM_SIZE = 10 # number of hidden layers in each of the LSTM cells
LAMBDA_L2_REG = 0 # regularization coefficient


def simple_rnn(features, targets, mode):
    # 0. Reformat input shape to become a sequence
    x = tf.split(features[TIMESERIES_COL], N_INPUTS, 1)
    #print 'x={}'.format(x)

    # 1. configure the RNN
    lstm_cell = tf.contrib.rnn.BasicLSTMCell(LSTM_SIZE, forget_bias=1.0)
    outputs, _ = tf.contrib.rnn.static_rnn(lstm_cell, x, dtype=tf.float32)

    # slice to keep only the last cell of the RNN
    outputs = outputs[-1]
    #print 'last outputs={}'.format(outputs)

    # output is result of linear activation of last layer of RNN
    w = tf.Variable(tf.random_normal([LSTM_SIZE, N_OUTPUTS]))
    b = tf.Variable(tf.random_normal([N_OUTPUTS]))
    predictions = tf.matmul(outputs, w) + b

    # 2. loss function, training/eval ops
    if mode == tf.contrib.learn.ModeKeys.TRAIN or mode == tf.contrib.learn.ModeKeys.EVAL:
        l2_reg = tf.reduce_mean(tf.nn.l2_loss(w))
        loss = tf.losses.mean_squared_error(targets, predictions)+LAMBDA_L2_REG*l2_reg
        train_op = tf.contrib.layers.optimize_loss(
            loss=loss,
            global_step=tf.contrib.framework.get_global_step(),
            #learning_rate=0.01,
            learning_rate = tf.train.exponential_decay(0.01, tf.contrib.framework.get_global_step(),500, 0.96, staircase=True),
            optimizer="Adam",
            clip_gradients=2.5)
        eval_metric_ops = {
    "rmse": tf.metrics.root_mean_squared_error(targets, predictions)
    }
    else:
        loss = None
        train_op = None
        eval_metric_ops = None

    # 3. Create predictions
    predictions_dict = {"predicted": predictions}

    # 4. return ModelFnOps
    return tf.contrib.learn.ModelFnOps(
        mode=mode,
        predictions=predictions_dict,
        loss=loss,
        train_op=train_op,
        eval_metric_ops=eval_metric_ops)

我使用 python 2.7 和 tensorflow 1.6。 提前致谢!

您要找的是forward_features。但是,该函数中存在模型导出无法正常工作的错误;该修复程序看起来要到 TF 1.8 才会发布。

this answer, including a potential workaround, repeated here for your convenience (taken from this code sample 中有更多信息):

def forward_key_to_export(estimator):
    estimator = tf.contrib.estimator.forward_features(estimator, KEY_COLUMN)
    # return estimator

    ## This shouldn't be necessary (I've filed CL/187793590 to update extenders.py with this code)
    config = estimator.config
    def model_fn2(features, labels, mode):
      estimatorSpec = estimator._call_model_fn(features, labels, mode, config=config)
      if estimatorSpec.export_outputs:
        for ekey in ['predict', 'serving_default']:
          if (ekey in estimatorSpec.export_outputs and
              isinstance(estimatorSpec.export_outputs[ekey],
                         tf.estimator.export.PredictOutput)):
               estimatorSpec.export_outputs[ekey] = \
                 tf.estimator.export.PredictOutput(estimatorSpec.predictions)
      return estimatorSpec
    return tf.estimator.Estimator(model_fn=model_fn2, config=config)
    ##

要使用它,您需要执行以下操作:

estimator = build_estimator(...)
estimator = forward_key_to_export(estimator)