【问题标题】:Keras customized callback: Grid point training termination conditionKeras 自定义回调:网格点训练终止条件
【发布时间】:2019-01-24 04:22:45
【问题描述】:

我正在使用来自scikit-learnGridSearchCVkeras 中对我的神经网络进行网格搜索。我想自定义callback,所以每次在一个网格点上的网络训练完成时,我可以打印出那个拟合已经完成。

假设我将我的网格定义如下:

param_grid = dict(epochs=[50, 100, 500, 1000],
              learn_rate=[0.1, 0.2, 0.3], 
              momentum=[0.01, 0.1], 
              dropout_rate=[0.05, 0.1, 0.15, 0.2])

我将网格上的可能性总数计算为:

grid_size = reduce(lambda x,y: x*y,[len(param_grid_[key]) for key in param_grid])

回调是:

from keras.callbacks import ModelCheckpoint, EarlyStopping
# checkpoint
filepath="best_model.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, 
save_best_only=True, mode='max')
# Early stoping
monitor = EarlyStopping(monitor='val_loss', min_delta=1e-5, patience=200, 
verbose=1, mode='auto')

callbacks_list = [checkpoint, monitor, LiveGridReport()]

LiveGridReport() 是我自定义的回调,它打印有关在网格点上完成训练的消息。

class LiveGridReport(keras.callbacks.Callback):

    def __init__(self, grid_size):
        grid_size_ = grid_size

    def on_train_begin(self, logs={}):
        return

    def on_train_end(self, logs={}):
        return

我的问题是,考虑到我还有EarlyStopping 回调,我无法弄清楚如何检测网格点上的训练已经终止。

【问题讨论】:

    标签: python callback keras deep-learning grid-search


    【解决方案1】:

    可以使用stopped_epoch 回调确定在哪个时期训练停止使用EarlyStopping 回调

    EarlyStopping.stopped_epoch
    

    或使用历史记录

    history = model.fit(....)
    number_of_epochs_it_ran = len(history.history['loss'])
    

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 2017-11-10
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      相关资源
      最近更新 更多