Tensorflow:自定义错误聚合器



>假设我们要更改鲍鱼回归示例,以便有一个额外的评估指标。该指标称为pctWrong,等于误差> 1% 的预测的百分比:=== pseudocode === pctWrong = countTrue(if (|y-y_hat|/y > 1%) True else False) / countTotal

=== Python ===
105 # Calculate additional eval metrics
106 eval_metric_ops = {
107     "rmse": tf.metrics.root_mean_squared_error(
108         tf.cast(labels, tf.float64), predictions),
109     "pctWrong": ???
110 }

您将如何定义这样的指标?我找到了 tf.metrics.percentage_below((,这可能会有所帮助,但我不知道如何使用它。特别是我不知道如何获取其values参数。

更新:毕竟没那么难。下面的代码有点粗糙,但它有效。

# Calculate additional eval metrics
ys = tf.cast(labels, tf.float64)
y_hats = predictions
losses = tf.divide(tf.abs(tf.subtract(ys, y_hats)), ys)
accuracies = -losses
eval_metric_ops = {
"rmse": tf.metrics.root_mean_squared_error(tf.cast(labels, tf.float64), predictions),
"pctWrong": tf.metrics.percentage_below(accuracies, -0.01)
}

最新更新