Search code examples
python-3.xtensorflowkeras

tensorflow earlystopping does not work properly


I'm dealing with bunch of image dataset
However it takes a lot of time to learn, so I used earlystopping in tensorflow
This is my callback option & fit option
(I know monitoring acc is not a good option, but just wanted to see how earlystopping works)

tf.keras.callbacks.EarlyStopping(
  monitor='accuracy', 
  patience=3, 
  #mode='max',
  verbose=2, 
  baseline=0.98
)

model.fit(x, y, batch_size=16, epochs=10, verbose=2, validation_split=0.2, callbacks=callbacks)

However, this is the result:

101/101 - 42s - loss: 6.9557 - accuracy: 6.2461e-04 - val_loss: 6.9565 - val_accuracy: 0.0000e+00
Epoch 2/10

101/101 - 39s - loss: 6.9549 - accuracy: 0.0019 - val_loss: 6.9558 - val_accuracy: 0.0000e+00
Epoch 3/10

101/101 - 37s - loss: 6.9537 - accuracy: 0.0037 - val_loss: 6.9569 - val_accuracy: 0.0000e+00
Epoch 00003: early stopping

since monitoring value 'accuracy' kept increasing, expected it not to stop.
plus, I want earlystopping to monitor acc like this

acc=0, acc=0.1....acc=0.5, acc=0.4, acc=0.5, acc=0.6 #dont stop if increases again in patience epoch 

acc=0, acc=0.1....acc=0.5, acc=0.3, acc=0.4, acc=0.35 #stop if acc does not increases again in patience epoch 

How should I do that?


Solution

  • The issue is with the use of Baseline

    As per the documentation it is defined as :

    Baseline value for the monitored quantity. Training will stop if the model doesn't show improvement over the baseline.
    

    By setting Baseline to 98% you are stating that the model's accuracy starts at 98% and it it does not improve over the baseline over 3 epochs stop training.

    Instead do the following as per your use case:

    tf.keras.callbacks.EarlyStopping(
        monitor='accuracy', 
        min_delta=0.001,
        patience=3,
        mode='auto',
        verbose=2,
        baseline=None
    )