How will I stop Keras Training when the accuracy already reached 1.0? I tried monitoring loss value, but I haven't tried stopping the training when the accuracy is already 1.
I tried the code below with no luck:
stopping_criterions =[
EarlyStopping(monitor='loss', min_delta=0, patience = 1000),
EarlyStopping(monitor='acc', base_line=1.0, patience =0)
]
model.summary()
model.compile(Adam(), loss='binary_crossentropy', metrics=['accuracy'])
model.fit(scaled_train_samples, train_labels, batch_size=1000, epochs=1000000, callbacks=[stopping_criterions], shuffle = True, verbose=2)
UPDATE:
The training immediately stops at first epoch, even if the accuracy is still not 1.0
.
Please help.
Therefore, the epoch when the validation error starts to increase is precisely when the model is overfitting to the training set and does not generalize new data correctly. This is when we need to stop our training.
You can use model. stop_training parameter to stop the training.
Stop training when the validation error is the minimum. This means that the nnet can generalise to unseen data. If you stop training when the training error is minimum then you will have over fitted and the nnet cannot generalise to unseen data.
There are three elements to using early stopping; they are: Monitoring model performance. Trigger to stop training. The choice of model to use.
Update: tested in keras 2.4.3 (Dec.2020)
I don't know why EarlyStopping
does not work in this case. Instead, I defined a custom callback that stops training when acc
(or val_acc
) reaches a specified baseline:
from keras.callbacks import Callback
class TerminateOnBaseline(Callback):
"""Callback that terminates training when either acc or val_acc reaches a specified baseline
"""
def __init__(self, monitor='accuracy', baseline=0.9):
super(TerminateOnBaseline, self).__init__()
self.monitor = monitor
self.baseline = baseline
def on_epoch_end(self, epoch, logs=None):
logs = logs or {}
acc = logs.get(self.monitor)
if acc is not None:
if acc >= self.baseline:
print('Epoch %d: Reached baseline, terminating training' % (epoch))
self.model.stop_training = True
You can use it like this:
callbacks = [TerminateOnBaseline(monitor='accuracy', baseline=0.8)]
callbacks = [TerminateOnBaseline(monitor='val_accuracy', baseline=0.95)]
Note: This solution does not work.
If you want to stop training when the training (or validation) accuracy exactly reaches 100%, then use EarlyStopping
callback and set the baseline
argument to 1.0 and patience
to zero:
EarlyStopping(monitor='acc', baseline=1.0, patience=0) # use 'val_acc' instead to monitor validation accuarcy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With