如何让 Keras 仅对验证数据计算某个指标?

mar*_*uan 5 python keras tensorflow

我正在使用tf.kerasTensorFlow 1.14.0。我已经实现了一个计算量非常大的自定义指标,如果我只是将它添加到作为model.compile(..., metrics=[...]).

如何让 Keras 在训练迭代期间跳过度量的计算,但在每个时期结束时根据验证数据(并打印)计算它?

小智 8

为此,您可以在指标计算中创建一个 tf.Variable,用于确定计算是否继续,然后在使用回调运行测试时更新它。例如

class MyCustomMetric(tf.keras.metrics.Metrics):

    def __init__(self, **kwargs):
        # Initialise as normal and add flag variable for when to run computation
        super(MyCustomMetric, self).__init__(**kwargs)
        self.metric_variable = self.add_weight(name='metric_variable', initializer='zeros')
        self.on = tf.Variable(False)

    def update_state(self, y_true, y_pred, sample_weight=None):
        # Use conditional to determine if computation is done
        if self.on:
            # run computation
            self.metric_variable.assign_add(computation_result)

    def result(self):
        return self.metric_variable

    def reset_states(self):
        self.metric_variable.assign(0.)

class ToggleMetrics(tf.keras.callbacks.Callback):
    '''On test begin (i.e. when evaluate() is called or 
     validation data is run during fit()) toggle metric flag '''
    def on_test_begin(self, logs):
        for metric in self.model.metrics:
            if 'MyCustomMetric' in metric.name:
                metric.on.assign(True)
    def on_test_end(self,  logs):
        for metric in self.model.metrics:
            if 'MyCustomMetric' in metric.name:
                metric.on.assign(False)
Run Code Online (Sandbox Code Playgroud)