tf.estimator 的自定义指标

Custom metrics with tf.estimator

我希望 tensorflow 在评估我的估算器期间计算确定系数(R 平方)。我尝试基于官方指标的实现以以下方式松散地实现它:

def r_squared(labels, predictions, weights=None,
              metrics_collections=None,
              updates_collections=None,
              name=None):

    total_error = tf.reduce_sum(tf.square(labels - tf.reduce_mean(labels)))
    unexplained_error = tf.reduce_sum(tf.square(labels - predictions))
    r_sq = 1 - tf.div(unexplained_error, total_error)

    # update_rsq_op = ?

    if metrics_collections:
        ops.add_to_collections(metrics_collections, r_sq)

    # if updates_collections:
    #     ops.add_to_collections(updates_collections, update_rsq_op)

    return r_sq #, update_rsq_op

然后,我将此函数用作 EstimatorSpec 中的指标:

estim_specs = tf.estimator.EstimatorSpec(
    ...
    eval_metric_ops={
        'r_squared': r_squared(labels, predictions),
        ...
    })

但是,这失败了,因为我的 R 平方实现没有 return update_op。

TypeError: Values of eval_metric_ops must be (metric_value, update_op) tuples, given: Tensor("sub_4:0", dtype=float64) for key: r_squared

现在我想知道 update_op 到底应该做什么?我真的需要实现一个 update_op 或者我能以某种方式创建某种虚拟 update_op 吗?如果有必要,我将如何实施?

好的,所以我想通了。我可以将我的指标包装在一个平均指标中并使用它的 update_op。这似乎对我有用。

def r_squared(labels, predictions, weights=None,
              metrics_collections=None,
              updates_collections=None,
              name=None):

    total_error = tf.reduce_sum(tf.square(labels - tf.reduce_mean(labels)))
    unexplained_error = tf.reduce_sum(tf.square(labels - predictions))
    r_sq = 1 - tf.div(unexplained_error, total_error)

    m_r_sq, update_rsq_op = tf.metrics.mean(r_sq)

    if metrics_collections:
        ops.add_to_collections(metrics_collections, m_r_sq)

    if updates_collections:
        ops.add_to_collections(updates_collections, update_rsq_op)

    return m_r_sq, update_rsq_op

我想我会提到你可以只使用 tensorflow_addons.metrics.RQsquare(). Tensorflow Add Ons is on PyPi here and the documentation is a part of Tensorflow here。您所要做的就是将 y_shape 设置为输出的形状,对于单个输出变量通常是 (1,)