推定量の評価中に、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 squared の実装は 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 を作成できますか? また、必要な場合、どのように実装しますか?