【问题标题】:Custom eval_metric_ops in Estimator in TensorflowTensorflow 中的 Estimator 中的自定义 eval_metric_ops
【发布时间】:2018-01-20 11:30:34
【问题描述】:

我正在尝试在估算器中的 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
    )

但我有以下错误:

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

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

【问题讨论】:

    标签: python tensorflow tensorflow-estimator


    【解决方案1】:

    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))
    

    【讨论】:

    • 完美,正是我想要的。谢谢!
    【解决方案2】:

    我尝试了接受的答案,但它在 TF 1.14 中不起作用,然后我尝试制作 my own。您只需更改以compute_* 开头的函数和相关变量即可将此处的source code examples 调整为您自己的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      相关资源
      最近更新 更多