了解 Keras EarlyStopping 的行为
Understanding the behavior of Keras EarlyStopping
我正在使用 tensorflow 2.4.0
,这里是 tf.keras
EarlyStopping 回调的代码,特别是 EarlyStopping
class 的方法,在每个时期结束(on_epoch_end
):
def on_epoch_end(self, epoch, logs=None):
current = self.get_monitor_value(logs).
if self.monitor_op(current - self.min_delta, self.best):
self.best = current
self.wait = 0
if self.restore_best_weights:
self.best_weights = self.model.get_weights()
else:
self.wait += 1
if self.wait >= self.patience:
self.stopped_epoch = epoch
self.model.stop_training = True
if self.restore_best_weights:
if self.verbose > 0:
print('Restoring model weights from the end of the best epoch.')
self.model.set_weights(self.best_weights)
其中,因为在我的例子中,监控的数量是 val_loss
:
self.monitor_op = np.less
本质上,代码执行以下逻辑:
If (current - min_delta) < best:
best = current;
wait = 0
Otherwise:
wait += 1;
if wait >= patience:
stop training
那么,例如:
min_delta = 0.1
current = 0.9
best = 0.85
我们有 (current - min_delta) < best
,因此:
best = current (=0.9)
wait = 0
因此,best
现在关联到一个比之前更差的值(0.9
而不是 0.85
);是 EarlyStopping 的 correct/expected 行为吗?
好像很奇怪
如果您查看 EarlyStopping
class 的 init
method,您应该会看到如下内容:
if self.monitor_op == np.greater:
self.min_delta *= 1
else:
self.min_delta *= -1
因此,由于我们知道 self.monitor_op = np.less
,我认为 min_delta
在您的情况下实际上是 -0.1,并且 if 语句评估类似于:(0.9-(-0.1)) < 0.85
。我假设您有一个 EarlyStopping
回调定义为:
es = EarlyStopping(monitor='val_loss', mode='min')
还要注意 min_delta
实际上 is:
Minimum change in the monitored quantity to qualify as an improvement,
i.e. an absolute change of less than min_delta, will count as no
improvement.
我正在使用 tensorflow 2.4.0
,这里是 tf.keras
EarlyStopping 回调的代码,特别是 EarlyStopping
class 的方法,在每个时期结束(on_epoch_end
):
def on_epoch_end(self, epoch, logs=None):
current = self.get_monitor_value(logs).
if self.monitor_op(current - self.min_delta, self.best):
self.best = current
self.wait = 0
if self.restore_best_weights:
self.best_weights = self.model.get_weights()
else:
self.wait += 1
if self.wait >= self.patience:
self.stopped_epoch = epoch
self.model.stop_training = True
if self.restore_best_weights:
if self.verbose > 0:
print('Restoring model weights from the end of the best epoch.')
self.model.set_weights(self.best_weights)
其中,因为在我的例子中,监控的数量是 val_loss
:
self.monitor_op = np.less
本质上,代码执行以下逻辑:
If (current - min_delta) < best:
best = current;
wait = 0
Otherwise:
wait += 1;
if wait >= patience:
stop training
那么,例如:
min_delta = 0.1
current = 0.9
best = 0.85
我们有 (current - min_delta) < best
,因此:
best = current (=0.9)
wait = 0
因此,best
现在关联到一个比之前更差的值(0.9
而不是 0.85
);是 EarlyStopping 的 correct/expected 行为吗?
好像很奇怪
如果您查看 EarlyStopping
class 的 init
method,您应该会看到如下内容:
if self.monitor_op == np.greater:
self.min_delta *= 1
else:
self.min_delta *= -1
因此,由于我们知道 self.monitor_op = np.less
,我认为 min_delta
在您的情况下实际上是 -0.1,并且 if 语句评估类似于:(0.9-(-0.1)) < 0.85
。我假设您有一个 EarlyStopping
回调定义为:
es = EarlyStopping(monitor='val_loss', mode='min')
还要注意 min_delta
实际上 is:
Minimum change in the monitored quantity to qualify as an improvement, i.e. an absolute change of less than min_delta, will count as no improvement.