根据时代安排提前停止

时间:2018-07-05 21:55:48

标签: python tensorflow keras

我在Keras中建立了一个神经网络模型,该神经网络在时间段1000(使用的超参数)达到1的火车设定精度,但验证精度始终在0.78和0.8之间波动。

我想要一个“早期停止”命令来监视验证准确性,但是该命令仅在第1000个纪元之后才开始,因为在第1000个纪元之前,验证准确性一直在高度波动。因此,我的策略是最大程度地提高训练集的准确性,然后在验证准确性获得很高价值时立即停止学习。 (理论上最好的训练是1.0,验证的是0.8)

这样的回调函数可能吗?

1 个答案:

答案 0 :(得分:2)

我再次更新了答案,对不起,我错过了纪元部分。您需要定义自己的提前停止功能。这个answer到另一个问题可以有所帮助。 使用该答案并使用“ epoch_threshold”进行一些更改:

class EarlyStoppingByLossVal(Callback):
    def __init__(self, monitor='val_loss', value=0.00001, verbose=0):
        super(Callback, self).__init__()
        self.monitor = monitor
        self.value = value
        self.verbose = verbose
        self.epoch_threshold = 1000
    def on_epoch_end(self, epoch, logs={}):
        current = logs.get(self.monitor)
        if current is None:
            warnings.warn("Early stopping requires %s available!" % self.monitor, RuntimeWarning)

        if current < self.value and epoch > self.epoch_threshold:
            if self.verbose > 0:
                print("Epoch %05d: early stopping THR" % epoch)
            self.model.stop_training = True