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'])