Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Keras自定义回调:网格点训练终止条件_Python_Callback_Keras_Deep Learning_Grid Search - Fatal编程技术网

Python Keras自定义回调:网格点训练终止条件

Python Keras自定义回调:网格点训练终止条件,python,callback,keras,deep-learning,grid-search,Python,Callback,Keras,Deep Learning,Grid Search,我正在使用scikit learn中的GridSearchCV在keras中对我的神经网络进行网格搜索。我想定制回调,这样每次在一个网格点上完成网络训练时,我都可以打印出拟合完成 假设我将网格定义如下: param_grid = dict(epochs=[50, 100, 500, 1000], learn_rate=[0.1, 0.2, 0.3], momentum=[0.01, 0.1], dropout

我正在使用
scikit learn
中的
GridSearchCV
keras
中对我的神经网络进行网格搜索。我想定制
回调
,这样每次在一个网格点上完成网络训练时,我都可以打印出拟合完成

假设我将网格定义如下:

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

我的问题是,考虑到我还有
earlystoping
回调,我无法确定如何检测网格点上的训练已终止。

使用
earlystoping
回调时,确定在哪个历元训练停止,可以使用

还是利用历史

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