在指定次数后尽早停止Keras

时间:2020-07-12 10:04:32

标签: keras neural-network training-data early-stopping

是否可以在Keras中指定的时期数后应用“提前停止”。 例如,我想训练我的NN 45个纪元,然后开始使用EarlyStopping。

到目前为止,我是这样做的:

var myHtml = 'Aenean lacinia bibendum <a href="/life">life</a> sed consectetur. <a href="/work">Work</a> quis risus eget urna mollis ornare <a href="/about">about</a> leo.'; var result = myHtml.search(/<\s*a[^>]*>(.*?)<\s*\/\s*a>/g); if (result >-1) { alert('right!'); } else { alert("wrong"); }

early_stop = EarlyStopping(monitor='val_loss', mode='min', verbose=1, baseline = 0.1, patience = 3)

但是这样做,只会产生一些训练的步骤图

enter image description here

有没有办法我可以一起写这些?任何帮助,不胜感激!

1 个答案:

答案 0 :(得分:-1)

我认为我们可以使用Keras库中的自定义回调来执行此操作。如下定义您的自定义回调:

# Custom Callback
class myCallback(keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs={}):
    if epoch == 45:
      self.model.stop_training = True
callback = myCallback()

opt = Adam(lr = .00001, beta_1=0.9, beta_2=0.999, epsilon=1e-8)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics = ['accuracy'])
# Include myCallback in callbacks so that the model stops training after 45th epoch
mod = model.fit_generator(train_batches, steps_per_epoch = 66, validation_data = valid_batches, validation_steps = 22, epochs = 50, callbacks = [early_stop, myCallback], verbose = 1)