Tensorflow 中 Estimator 中的自定义 eval_metric_ops [英] Custom eval_metric_ops in Estimator in Tensorflow

查看:38
本文介绍了Tensorflow 中 Estimator 中的自定义 eval_metric_ops的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的估算器的 eval_metric_ops 中添加 r 平方,如下所示:

I am trying to add the r squared in the eval_metric_ops in my estimator like this:

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
    )

但我有以下错误:

TypeError: eval_metric_ops 的值必须是 (metric_value, update_op)元组,给定: Tensor("rsquared:0", shape=(), dtype=float32) 键:平方

TypeError: Values of eval_metric_ops must be (metric_value, update_op) tuples, given: Tensor("rsquared:0", shape=(), dtype=float32) for key: rsquared

我也尝试不使用 name 参数,但没有任何改变.你知道如何创建这个 eval_metric_ops 吗?

I tried without the name argument too but does not change anything. Do you know how to create this eval_metric_ops ?

推荐答案

eval_metric_ops 需要一个以名称为键的度量结果字典.dict 的值是调用度量函数的结果.您案例中的度量函数可以使用 tf.metrics 实现:

eval_metric_opsneeds a dict of metric results keyed by name. The values of the dict are the results of calling a metric function. The metric function in your case can be implemented using 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))

这篇关于Tensorflow 中 Estimator 中的自定义 eval_metric_ops的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆