感谢@Pedro Marques 和@lescurel 的贡献,我设法创建了一个度量类,它可以平滑跨时代的任何其他度量。由于使用了不同的状态变量,它也能够在训练期间使用验证集时工作。
有一个管理状态逻辑的基类:
class SmoothMetric(tf.keras.metrics.Metric):
def __init__(self, metric, name=None, **kwargs):
if name is None:
name = 'smooth_' + metric.name
super().__init__(name=name, **kwargs)
self.metric = metric
self.in_test_step = tf.Variable(False)
self.prev_in_test_step = tf.Variable(False)
self.train_states = {}
self.test_states = {}
def add_state_variable(self, name, **kwargs):
self.train_states[name] = tf.Variable(**kwargs)
self.test_states[name] = tf.Variable(**kwargs)
def get_state_variable(self, name):
return tf.cond(self.in_test_step, lambda: self.test_states[name], lambda: self.train_states[name])
def update_state(self, *args, **kwargs):
self.metric.update_state(*args, **kwargs)
self.prev_in_test_step.assign(self.in_test_step)
def update_filter_state_prevoius_epoch(self):
raise NotImplementedError('Needs to be overwritten')
def reset_state(self, *args, **kwargs):
# update state of the previous mode
tmp = self.in_test_step.read_value()
self.in_test_step.assign(self.prev_in_test_step)
self.update_filter_state_prevoius_epoch()
self.in_test_step.assign(tmp)
self.metric.reset_state(*args, **kwargs)
然后,我定义了一个继承自前一个的类,它实现了过滤器。在这种情况下,它实现了指数移动平均线,但也可以制作其他过滤器,例如简单移动平均线。
class SmoothMetricEMA(SmoothMetric):
def __init__(self, metric, alpha=0.1, name=None, **kwargs) -> None:
super().__init__(metric=metric, name=name, **kwargs)
self.alpha = alpha
self.add_state_variable('accum', initial_value=0.0)
self.add_state_variable('idx', initial_value=0)
def update_filter_state_prevoius_epoch(self):
self.get_state_variable('accum').assign(self.result())
self.get_state_variable('idx').assign_add(1)
def _result(self):
return self.metric.result()
def _filtered_result(self):
return (1 - self.alpha) * self.get_state_variable('accum') + self.alpha * self._result()
def result(self):
# if the idx is zero, the accumulator is 0 and _filtered_result will not give the correct result
return tf.cond(tf.equal(self.get_state_variable('idx'), 0), self._result, self._filtered_result)
这里是 SMA 过滤器替代方案:
class SmoothMetricSMA(SmoothMetric):
def __init__(self, metric, n_samples=10, remove_outliers=None, name=None, **kwargs) -> None:
super().__init__(metric=metric, name=name, **kwargs)
self.queue_size = n_samples - 1
self.remove_outliers = remove_outliers
self.add_state_variable('accum', initial_value=tf.zeros(self.queue_size))
self.add_state_variable('idx', initial_value=0)
def update_filter_state_prevoius_epoch(self):
self.get_state_variable('accum').scatter_update(
tf.IndexedSlices(self.metric.result(), self.get_state_variable('idx') % self.queue_size))
self.get_state_variable('idx').assign_add(1)
def _result(self):
return self.metric.result()
def _filtered_result(self):
queue = tf.concat([self.get_state_variable('accum')[:self.get_state_variable('idx')], tf.reshape(self._result(), (1,))], axis=0)
if self.remove_outliers is not None:
queue = tf.sort(queue)
eff_size = tf.cast(tf.minimum(self.get_state_variable('idx')+1, self.queue_size), tf.float32)
queue = queue[tf.cast(tf.math.floor(eff_size*self.remove_outliers/2), tf.int32):tf.cast(tf.math.ceil(eff_size*(1-self.remove_outliers/2)), tf.int32)]
return tf.reduce_mean(queue)
def result(self):
# if the idx is zero, the accumulator is 0 and _filtered_result will not give the correct result
return tf.cond(tf.equal(self.get_state_variable('idx'), 0), self._result, self._filtered_result)
最后,要使过滤器工作,必须使用以下回调。这告诉过滤器它是处于训练模式还是测试模式。
class NotifySmoothMetricsCallback(tf.keras.callbacks.Callback):
"""
Notifies the smooth metric if it is on train or test step.
"""
def on_test_begin(self, logs):
for metric in self.model.metrics:
if isinstance(metric, SmoothMetric):
metric.in_test_step.assign(True)
def on_test_end(self, logs):
for metric in self.model.metrics:
if isinstance(metric, SmoothMetric):
metric.in_test_step.assign(False)
def on_epoch_begin(self, epoch, logs):
for metric in self.model.metrics:
if isinstance(metric, SmoothMetric):
metric.in_test_step.assign(False)
def on_epoch_end(self, epoch, logs):
for metric in self.model.metrics:
if isinstance(metric, SmoothMetric):
metric.in_test_step.assign(False)
def on_train_begin(self, logs):
for metric in self.model.metrics:
if isinstance(metric, SmoothMetric):
metric.in_test_step.assign(False)
以下是如何使用这些类的示例:
metrics=[
SmoothMetricEMA(tf.metrics.MeanSquaredError(), alpha=0.1, name='smooth'),
]
callbacks = [
tf.keras.callbacks.EarlyStopping(monitor='val_smooth', patience=10, verbose=1),
NotifySmoothMetricsCallback(),
]
model.compile(optimizer='adam', loss='mse', metrics=metrics)
model.fit(x_train, y_train, batch_size=32, epochs=100,
validation_data=(x_val, y_val),
callbacks=callbacks,
)