我想在达到一定百分比(例如98%)时停止训练。我尝试了多种方法,但在Google上进行搜索都没有运气。我所做的是像这样使用EarlyStopping
:
es = EarlyStopping(monitor='val_acc', baseline=0.98, verbose=1)
model.fit(tr_X, tr_y, epochs=1000, batch_size=1000, validation_data=(ts_X, ts_y), verbose=1, callbacks=[es])
_, train_acc = model.evaluate(tr_X, tr_y, verbose=0)
_, test_acc = model.evaluate(ts_X, ts_y, verbose=0)
print('>> Train: %.3f, Test: %.3f' % (train_acc, test_acc))
这是不正确的。如果有人可以提出实现此目标的方法,我将不胜感激。
谢谢
答案 0 :(得分:2)
您可以这样创建一个新的回调:
class EarlyStoppingValAcc(Callback):
def __init__(self, monitor='val_acc', value=0.98, verbose=1):
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 = [
EarlyStoppingByValAcc(monitor='val_acc', value=0.98, verbose=1),
]
model.fit(tr_X, tr_y, epochs=1000, batch_size=1000, validation_data=(ts_X, ts_y), verbose=1, callbacks=callbacks)