只有在第一个(例如100个)纪元之后,才有办法在Keras中实施提前停止 [英] Is there a way to implement early stopping in Keras ONLY after the first, say, 100 epochs

查看:51
本文介绍了只有在第一个(例如100个)纪元之后,才有办法在Keras中实施提前停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的拟合函数是非凸函数,因此损失函数有时会在恶化之前恶化.鉴于此,我想使用早期停止功能,但只能在前100个或更多个时间段之后使用.到目前为止,我有这个:

My fit function is nonconvex so that the loss function can sometimes worsen before it improves. Given this, I want to use early stopping, but only after the first 100 or more epochs. So far I have this:

# Early stopping
ES = [EarlyStopping(monitor='val_loss',patience=100,verbose=1,mode='auto')]

# fit model
history = model.fit(x_train, y_train, epochs=1000,batch_size=50,verbose=2,shuffle=True,validation_split=.1,callbacks=ES)

不幸的是,在10个左右的时间之后,拟合很快就停止了.我想等到第100个时期开始提早停止.有任何想法吗?除了提早停止以外的任何建议也将受到赞赏.

Unfortunately, the fit stops very early after 10 or so epochs. I want to wait until the 100th epoch to start early stopping. Any ideas? Any suggestions other than early stopping are also appreciated.

推荐答案

如果您使用patience=100,则您的培训不应在第100个时期之前停止.但是,如果您想使用简短的patience,但也希望它开始稍后,您可以使用colllin描述的方法.如果需要进一步的自定义,则始终可以定义自己的回调,这里以EarlyStopping作为父级.出于您的目的,您只需要覆盖初始化程序和

If you use patience=100 your training should not stop before epoch 100. However, if you want to have a short patience but also want it to start later, you could use the method described by colllin. If you want further customization, you can always define your own callback, here with EarlyStopping as a parent. For your purposes you just need to override the initializer and the on_epoch_end method found here:

class CustomStopper(keras.callbacks.EarlyStopping):
    def __init__(self, monitor='val_loss',
             min_delta=0, patience=0, verbose=0, mode='auto', start_epoch = 100): # add argument for starting epoch
        super(CustomStopper, self).__init__()
        self.start_epoch = start_epoch

    def on_epoch_end(self, epoch, logs=None):
        if epoch > self.start_epoch:
            super().on_epoch_end(epoch, logs)

在从父类调用函数之前,只需要将要监视标准的最早时间提供给初始化程序,并检查条件.

You only have to give the earliest epoch from where you want to monitor your criterion to the initializer and check the condition before calling the function from the parent class.

这篇关于只有在第一个(例如100个)纪元之后,才有办法在Keras中实施提前停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆