【问题标题】:Outputting multiple loss components to tensorboard from tensorflow estimators从张量流估计器将多个损失分量输出到张量板
【发布时间】:2020-04-09 09:21:17
【问题描述】:

我对 tensorflow 很陌生,我正在努力让 tensorboard 显示我的一些自定义指标。我正在使用的模型是一个 tf.estimator.Estimator,带有一个关联的 EstimatorSpec。我要记录的第一个新指标来自我的损失函数,它由两个部分组成:年龄预测的损失(tf.float32)和类别预测的损失(单热/多类),我加在一起确定总损失(我的模型同时预测班级和年龄)。总损失在训练期间输出得很好并显示在张量板上,但我想跟踪个人年龄和类别预测损失组件。

我认为应该可行的解决方案是向 EstimatorSpec 添加一个 eval_metric_ops 参数,如此处所述 (Custom eval_metric_ops in Estimator in Tensorflow)。但是,我无法使这种方法起作用。我定义了一个自定义度量函数,如下所示:

def age_loss_function(labels, ages_pred, ages_true):
  per_sample_age_loss = get_age_loss_per_sample(ages_pred, ages_true) ### works fine

  #### The error happens on this line:
  mean_abs_age_diff, age_loss_update_fn = tf.metrics.Mean(per_sample_age_loss)
  ######

  return mean_abs_age_diff, age_loss_update_fn

eval_metric_ops = {"age_loss": age_loss_function}  #### Want to use this in EstimatorSpec

说明似乎说我需要错误度量和更新函数,它们都应该从 tf.metrics 命令返回,就像我链接的例子一样。但是这个命令对我来说失败并显示错误消息:

tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.

我可能只是在滥用 API。如果有人可以指导我正确使用,我将不胜感激。谢谢!

【问题讨论】:

    标签: tensorflow tensorboard


    【解决方案1】:

    看起来问题出在版本更改上。我已经更新到 tensorflow 2.0,而我遵循的说明来自 1.X。改用 tf.compat.v1.metrics.mean() 可以解决这个问题。

    【讨论】:

      猜你喜欢
      • 2019-08-02
      • 2019-04-11
      • 2018-12-07
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多