带有tf.estimator模型预测的返回键

时间:2019-08-21 22:27:13

标签: python tensorflow google-cloud-ml

我正在使用自定义的tensorflow Estimator,并尝试使用

tf.contrib.estimator.forward_features

与我的输出一起返回键列。我关注了post1post2

并应用

tf.contrib.estimator.forward_features

但是我收到了错误消息

 Predictions should be a dict to be able to forward features. Given: <class 'tensorflow.python.framework.ops.Tensor'>

我的模型函数看起来像这样

def model_fn(features, labels, mode):

    values = nnet(features)
    if mode == tf.estimator.ModeKeys.TRAIN:
       is_training = True
    else:
       is_training = False

    if mode == tf.estimator.ModeKeys.PREDICT:
       predictions = {
        'class_ids': tf.argmax(tf.nn.softmax(values),1),
        'probabilities': tf.nn.softmax(values),
        'logits': values,
        }
        export_outputs = {
            'prediction': tf.estimator.export.PredictOutput(predictions)
        }
    return tf.estimator.EstimatorSpec(mode,predictions=predictions,export_outputs=export_outputs)

    labels_one_hot=tf.one_hot(labels,4)

    score= tf.argmax(values,axis=1)

    loss_op= tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=values,labels=labels_one_hot))

    gradients = tf.gradients(loss_op, tf.trainable_variables())

    optimizer = tf.train.AdamOptimizer(learning_rate=.0001)

    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)

    if mode == tf.estimator.ModeKeys.TRAIN:

        train_optimizer = optimizer.apply_gradients(zip(gradients, tf.trainable_variables()),

        global_step=tf.train.get_global_step())

        acc_op=tf.metrics.accuracy( labels= labels,predictions=tf.argmax(values, axis=1))

        tf.summary.scalar('accuracy_rate', acc_op[1])
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)

        estim_specs = tf.estimator.EstimatorSpec(
             mode=mode,
             predictions=score,
             loss=loss_op,
             train_op=train_optimizer,
        eval_metric_ops={'acc': acc_op})
        return estim_specs 


    if mode == tf.estimator.ModeKeys.EVAL:
        predicted_indices = tf.argmax(values, axis=1)
        eval_metric_ops = {
          'accuracy': tf.metrics.accuracy(labels, predicted_indices)}

        return tf.estimator.EstimatorSpec(mode,loss=loss_op,eval_metric_ops=eval_metric_ops)

我这样称呼我的估算器

estimator = tf.estimator.Estimator(
    model_fn= model_fn,
    config= tf.estimator.RunConfig(
            save_checkpoints_steps = 2000,
            keep_checkpoint_max = 10,
            tf_random_seed = 101),
          model_dir= "tf_dir")

estimator = tf.contrib.estimator.forward_features(
  estimator,'key')

tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)

我正在返回字典以进行预测,如果我不拨打电话

tf.contrib.estimator.forward_features

1 个答案:

答案 0 :(得分:0)

解决方案是将密钥显式地携带到模型函数中,并将其添加到输出字典中。

需要添加的代码行是

def model_fn(features, labels, mode):

    values = nnet(features)

在此处添加密钥

    key=features["key_column"]

    if mode == tf.estimator.ModeKeys.TRAIN:
        is_training = True
    else:
        is_training = False


    if mode == tf.estimator.ModeKeys.PREDICT:
        predictions = {
            'class_ids': tf.argmax(tf.nn.softmax(values),1),
             'probabilities': tf.nn.softmax(values),
             'logits': values,

将密钥添加到字典输出

            'key_column':key
           }
        export_outputs = {
        'prediction': tf.estimator.export.PredictOutput(predictions)
          }
        return tf.estimator.EstimatorSpec(mode,predictions=predictions,export_outputs=export_outputs)