即使未达到 min_delta 条件,Keras 模型也会提前停止

Keras Model early stops even though min_delta condition is not achieved

我正在按如下方式训练 Keras 序列模型。它适用于 5 个数字的 mnist 数据集。将 28x28 的图像压平,然后输出它们所属的 class 的一个热符号。

model = keras.Sequential([
keras.layers.InputLayer(input_shape = (784, )),
keras.layers.Dense(32, activation='relu'),
keras.layers.Dense(15, activation='relu'),
keras.layers.Dense(3, activation='relu'),
keras.layers.Dense(5, activation='softmax')

])

optzr = keras.optimizers.SGD(learning_rate=0.001, momentum=0.0, nesterov=False)
es = keras.callbacks.EarlyStopping(monitor='loss', min_delta=0.0001, verbose=2)
model.compile(optimizer=optzr, loss='categorical_crossentropy', metrics=['accuracy'])
out = model.fit(xtrain, ytrain, validation_data=(xval, yval), batch_size=32, verbose=2, epochs=20, callbacks=[es])

在 运行 模型上,这就是输出结果

Epoch 1/20
356/356 - 2s - loss: 1.7157 - accuracy: 0.1894 - val_loss: 1.6104 - val_accuracy: 0.1997 - 2s/epoch - 5ms/step
Epoch 2/20
356/356 - 1s - loss: 1.6094 - accuracy: 0.1946 - val_loss: 1.6102 - val_accuracy: 0.1997 - 1s/epoch - 3ms/step
Epoch 00002: early stopping

这里,即使loss减少了0.1以上,模型也宣告满足提前停止的条件,停止训练。

您应该在回调定义中将 patience 设置为 1。如果你不这样做,它默认为 0.

es = keras.callbacks.EarlyStopping(monitor='loss', min_delta=1e-4, verbose=2, patience=1)

Keras 通过保留名为 wait 的内部变量来实现 EarlyStopping。如果性能没有提高 min_delta,则此变量每个 epoch 增加一个,否则重置为 0。如果 wait 大于 或等于 patience.

,则训练停止
# Only check after the first epoch.
if self.wait >= self.patience and epoch > 0:
  self.stopped_epoch = epoch
  self.model.stop_training = True

因为 patience 默认为 0,所以 self.wait >= self.patience 总是 True 一旦第一个纪元过去(一旦 epoch > 0)。

要在性能停止提高时立即停止,您实际上想要将 patience 设置为 1 而不是 0