如何根据损失值告诉Keras停止训练?

如何根据损失值告诉Keras停止训练?,第1张

如何根据损失值告诉Keras停止训练?

我找到了答案。我调查了Keras的资源,并找到了EarlyStopping的代码。我基于此进行了自己的回调:

class EarlyStoppingByLossVal(Callback):    def __init__(self, monitor='val_loss', value=0.00001, verbose=0):        super(Callback, self).__init__()        self.monitor = monitor        self.value = value        self.verbose = verbose    def on_epoch_end(self, epoch, logs={}):        current = logs.get(self.monitor)        if current is None: warnings.warn("Early stopping requires %s available!" % self.monitor, RuntimeWarning)        if current < self.value: if self.verbose > 0:     print("Epoch %05d: early stopping THR" % epoch) self.model.stop_training = True

用法

callbacks = [    EarlyStoppingByLossVal(monitor='val_loss', value=0.00001, verbose=1),    # EarlyStopping(monitor='val_loss', patience=2, verbose=0),    ModelCheckpoint(kfold_weights_path, monitor='val_loss', save_best_only=True, verbose=0),]model.fit(X_train.astype('float32'), Y_train, batch_size=batch_size, nb_epoch=nb_epoch,      shuffle=True, verbose=1, validation_data=(X_valid, Y_valid),      callbacks=callbacks)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5106250.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存