【问题标题】:Capturing epoch count when using EarlyStopping feature with Keras Model使用带有 Keras 模型的 EarlyStopping 功能时捕获 epoch 计数
【发布时间】:2021-10-19 17:16:18
【问题描述】:

我一直在使用 Keras 建模,并认为我现在已经考虑了如何使用回调功能来捕获最佳拟合并防止过度拟合;一切似乎都很好。虽然我可以理解详细参数将显示我需要的信息,但它会使输出变得混乱,我更喜欢将其设置为零。我仍然想以某种方式捕获能够提供最佳结果的“纪元”计数,以将其合并到我自己的显示器中;有什么办法可以解决这个问题吗?谢谢

    model.compile(optimizer='adam', loss='mse' )] 
    cbfile = 'best_model.h5'
    calls = [
    EarlyStopping(monitor='val_loss', mode='auto', verbose=0, patience=10),\
    ModelCheckpoint(cbfile, monitor = 'val_loss', mode = 'auto',\
            save_best_only = True ) ]
    history = model.fit(Xvect, Yvect, epochs=mcycl, batch_size=32,\
            validation_split=dsplit, verbose=0, callbacks = calls )
    saved = load_model('best_model.h5')        
    score = saved.evaluate(Xvect, Yvect, verbose=0)
    print('"Overall loss for best fit":',np.round(score,4)) 

【问题讨论】:

    标签: python tensorflow keras model callback


    【解决方案1】:

    编写自己的自定义EarlyStopping 回调怎么样? Tensorflow 文档提供了一个很好的入门示例:

    import numpy as np
    
    
    class EarlyStoppingAtMinLoss(keras.callbacks.Callback):
        """Stop training when the loss is at its min, i.e. the loss stops decreasing.
    
      Arguments:
          patience: Number of epochs to wait after min has been hit. After this
          number of no improvement, training stops.
      """
    
        def __init__(self, patience=0):
            super(EarlyStoppingAtMinLoss, self).__init__()
            self.patience = patience
            # best_weights to store the weights at which the minimum loss occurs.
            self.best_weights = None
    
        def on_train_begin(self, logs=None):
            # The number of epoch it has waited when loss is no longer minimum.
            self.wait = 0
            # The epoch the training stops at.
            self.stopped_epoch = 0
            # Initialize the best as infinity.
            self.best = np.Inf
    
        def on_epoch_end(self, epoch, logs=None):
            current = logs.get("loss")
            if np.less(current, self.best):
                self.best = current
                self.wait = 0
                # Record the best weights if current results is better (less).
                self.best_weights = self.model.get_weights()
            else:
                self.wait += 1
                if self.wait >= self.patience:
                    self.stopped_epoch = epoch
                    self.model.stop_training = True
                    print("Restoring model weights from the end of the best epoch.")
                    self.model.set_weights(self.best_weights)
    
        def on_train_end(self, logs=None):
            if self.stopped_epoch > 0:
                print("Epoch %05d: early stopping" % (self.stopped_epoch + 1))
    
    

    注意示例中的self.stopped_epoch 变量。通过这种方式,您可以完全控制显示的内容以及提前停止逻辑的工作方式。此外,使用logs 字典,您可以访问时期 x 的当前损失和准确度。另一方面,如果您只想在训练模型后使用简单的打印语句,您可以获取回调的最后一个时期并打印它:

    model.compile(optimizer='adam', loss='mse' )] 
    cbfile = 'best_model.h5'
    early_stopping = EarlyStopping(monitor='val_loss', mode='auto', verbose=0, patience=10)
    
    calls = [early_stopping,
    ModelCheckpoint(cbfile, monitor = 'val_loss', mode = 'auto',\
                save_best_only = True ) ]
    history = model.fit(Xvect, Yvect, epochs=mcycl, batch_size=32,\
                validation_split=dsplit, verbose=0, callbacks = calls )
    saved = load_model('best_model.h5')        
    score = saved.evaluate(Xvect, Yvect, verbose=0)
    
    print('"Overall loss for best fit":',np.round(score,4)) 
    print("Epoch %05d: early stopping" % (early_stopping.stopped_epoch + 1))
    

    【讨论】:

    • 非常感谢尝试并且似乎工作正常;我目前遇到的问题是知道“.stopped_epoch”甚至是“EarlyStopping”对象的一个​​属性。我可以搜索并找到 EarlyStopping 的参数,但是如何找到属性的列表和文档,我搜索时似乎只是空白?
    • 我通常会直接去source code查看。从文档开始,经常会省略属性。
    猜你喜欢
    • 2018-10-30
    • 2019-11-08
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多