Tensorflow Keras自定义指标错误在update_state上

问题描述

上下文

使用带有tf.Estimator界面的TF 1.15来训练和评估模型。尝试使用tf.keras.metric.Metric来编写自定义TF指标。

问题

我编写了一个自定义指标,并将其包含在eval_metrics_ops中(以下示例)。如果我使用指标定义估算器,则会出现以下错误

ValueError: Please call update_state(...) on the "<metric_name>" metric 

错误的措词看起来很清楚(我必须致电update_state()),但是我不确定在度量标准上应该在哪里致电update_state()(不确定我是否应该致电)。这不是一个最小的示例,但这是我编写的一个演示指标。

class MyMetric(tf.keras.metrics.Metric):
    def __init__(self,name="my_metric",**kwargs):
        super(MyMetric,self).__init__(name=name,**kwargs)

    def update_state(self,y_true,y_pred,sample_weight=None):
        self.true_samples = tf.reduce_sum(y_true)
    
    def result(self):
        return self.true_samples

创建一个dict,其中度量标准名称是键,而度量标准实例是值。 This提到了如何为dict创建eval_metrics_ops

metrics_ops = {"my_metric": MyMetric()}`. # The TensorFlow 1.15 documentation does not say we have to call `update_state(....) anywhere.`
estimator_spec = tf.estimator.EstimatorSpec(mode,model.loss,eval_metric_ops=metrics_ops)

有什么主意我该如何消除这个错误

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)