我在Keras中训练一个神经网络模型。我希望监控验证损失,并在达到特定条件时停止训练。
我知道我可以使用EarlyStopping
在训练没有改善的情况下,经过给定的patience
轮次后停止训练。
但我想做一些不同的事情。我希望在val_loss
超过某个值,比如x
,并且经过n
轮次后停止训练。
为了说明清楚,假设x
是0.5
,n
是50
。我希望只有在epoch
数大于50
并且val_loss
超过0.5
时才停止模型的训练。
我该如何在Keras中实现这一点?
回答:
你可以通过继承Keras的EarlyStopping
回调并用你自己的逻辑重写它来定义你自己的回调:
from keras.callbacks import EarlyStopping # use as base classclass 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, Densefrom keras.models import Modelimport numpy as np# Generate some random datafeatures = 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 modelinput_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 callbackcallbacks = [MyCallBack(threshold=0.001, min_epochs=10, verbose=1)] model.fit(features, labels, validation_data=(validation_feat, validation_labels), callbacks=callbacks, epochs=100)