从张量估计器向张量板输出多个损失分量

问题描述 投票:0回答:1

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

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

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个回答
0
投票

看起来问题出在版本更改中。当我遵循的指令来自1.X时,我已更新到tensorflow 2.0。使用tf.compat.v1.metrics.mean()可以解决此问题。

© www.soinside.com 2019 - 2024. All rights reserved.