首页 > 解决方案 > 为什么 tf.metric 返回零?

问题描述

我有一些这样的代码。它计算两个不同输出(控制转向)的平均平均误差。我想定义一个结合其他两个指标的指标。

import tensorflow as tf

lambda_speed = 0.05

control_mae = tf.metrics.mean_absolute_error(ground_truth_control, predictions_control, weights=weights)
speed_mae = tf.metrics.mean_absolute_error(ground_truth_speed, prediction_speed, name='speed_loss')

mae_total = ((1 - lambda_speed) * nonspeed_mae[0] + lambda_speed * speed_mae[0],
             tf.no_op())

eval_metric_ops = {
    "mae_total": mae_total,
}
tf.estimator.EstimatorSpec(
    mode, predictions=predictions, loss=total_loss, train_op=train_op, eval_metric_ops=eval_metric_ops,
)

在调试时,我检查了数据和预测是否正常。可悲的是,mae_total每个时代的每一步都为零?为什么?

标签: tensorflowdeep-learningmetricstensorflow-estimator

解决方案


如果我使用指标,我必须提及 中使用的所有指标eval_metrics_ops,否则它们将不会由 tf.estimator 运行。像这样:

import tensorflow as tf

lambda_speed = 0.05

control_mae = tf.metrics.mean_absolute_error(ground_truth_control, predictions_control, weights=weights)
speed_mae = tf.metrics.mean_absolute_error(ground_truth_speed, prediction_speed, name='speed_loss')

mae_total = ((1 - lambda_speed) * nonspeed_mae[0] + lambda_speed * speed_mae[0],
             tf.no_op())

eval_metric_ops = {
    "control_mae": control_mae,
    "speed_mae": speed_mae,
    "mae_total": mae_total,
}
tf.estimator.EstimatorSpec(
    mode, predictions=predictions, loss=total_loss, train_op=train_op, eval_metric_ops=eval_metric_ops,
)

推荐阅读