Tensorflow中Estimator中的自定义eval_metric_ops

Nic*_*REY 4 python tensorflow tensorflow-estimator

我试图像这样在估算器的eval_metric_ops中添加r的平方:

def model_fn(features, labels, mode, params):
    predict = prediction(features, params, mode)
    loss = my_loss_fn
    eval_metric_ops = { 
        'rsquared': tf.subtract(1.0, tf.div(tf.reduce_sum(tf.squared_difference(label, tf.reduce_sum(tf.squared_difference(labels, tf.reduce_mean(labels)))),
                                   name = 'rsquared')
        }

    train_op = tf.contrib.layers.optimize_loss(
        loss = loss,
        global_step = global_step,
        learning_rate = 0.1,
        optimizer = "Adam"
    )

    predictions = {"predictions": predict}

    return tf.estimator.EstimatorSpec(
        mode = mode,
        predictions = predictions,
        loss = loss,
        train_op = train_op,
        eval_metric_ops = eval_metric_ops
    )
Run Code Online (Sandbox Code Playgroud)

但我有以下错误:

TypeError:eval_metric_ops的值必须为(metric_value,update_op)元组,给定:键:rsquared的Tensor(“ rsquared:0”,shape =(),dtype = float32)

我也尝试过不使用name参数,但是没有任何改变。您知道如何创建此eval_metric_ops吗?

vij*_*y m 5

eval_metric_ops需要按名称键入度量指标的字典。dict的值是调用度量函数的结果。您可以使用tf.metrics以下指标来实现指标功能:

 def metric_fn(labels, predict):
    SST, update_op1 = tf.metrics.mean_squared_error(labels, tf.reduce_mean(labels))
    SSE, update_op2 = tf.metrics.mean_squared_error(labels, predictions )
    return tf.subtract(1.0, tf.div(SSE, SST)), tf.group(update_op1, update_op2))
Run Code Online (Sandbox Code Playgroud)