验证损失满足某些条件时提早停止

时间:2018-08-16 10:18:02

标签: python machine-learning keras deep-learning

我正在用Keras训练神经网络模型。我想监视验证损失并在达到特定条件时停止训练。

我知道,对于给定数量的EarlyStopping回合,如果训练没有任何改善,我可以使用patience停止训练。

我想要不同的东西。当val_loss回合后,x超过n这样的值时,我想停止训练。

为了清楚起见,假设x中的0.5n50。我只想在epoch数大于50并且val_loss大于0.5时停止模型的训练。

我如何在Keras中做到这一点??

1 个答案:

答案 0 :(得分:1)

您可以通过继承Keras EarlyStopping回调并使用自己的逻辑覆盖它来定义自己的回调:

from keras.callbacks import EarlyStopping # use as base class

class MyCallBack(EarlyStopping):
    def __init__(self, threshold, min_epochs, **kwargs):
        super(MyCallBack, self).__init__(**kwargs)
        self.threshold = threshold # threshold for validation loss
        self.min_epochs = min_epochs # min number of epochs to run

    def on_epoch_end(self, epoch, logs=None):
        current = logs.get(self.monitor)
        if current is None:
            warnings.warn(
                'Early stopping conditioned on metric `%s` '
                'which is not available. Available metrics are: %s' %
                (self.monitor, ','.join(list(logs.keys()))), RuntimeWarning
            )
            return

        # implement your own logic here
        if (epoch >= self.min_epochs) & (current >= self.threshold):
            self.stopped_epoch = epoch
            self.model.stop_training = True

一个小例子说明它应该工作:

from keras.layers import Input, Dense
from keras.models import Model
import numpy as np

# Generate some random data
features = np.random.rand(100, 5)
labels = np.random.rand(100, 1)

validation_feat = np.random.rand(100, 5)
validation_labels = np.random.rand(100, 1)

# Define a simple model
input_layer = Input((5, ))
dense_layer = Dense(10)(input_layer)
output_layer = Dense(1)(dense_layer)
model = Model(inputs=input_layer, outputs=output_layer)
model.compile(loss='mse', optimizer='sgd')

# Fit with custom callback
callbacks = [MyCallBack(threshold=0.001, min_epochs=10, verbose=1)] 
model.fit(features, labels, validation_data=(validation_feat, validation_labels), callbacks=callbacks, epochs=100)