Early Stopping with Keras

Steps to train a model

There are a number of things you have to do when working on a Machine Learning problem.

  • Import your data and transform it as appropriate
  • Define  your model using something like Keras - this includes your chosen hyperparameter values
  • Run the training phase
  • Evaluate the model
If at the end of this process the evaluation shows that your model is not sufficient for your needs you will need to make some changes and rerun the training. 

Ideally you should have a way to spot that training is not going as you want and stop it. This is one of the uses of early stopping. The other is if your model has reached a sufficient quality standard for you to use and further improvements have slowed, stopped altogether or an unnecessary. 

Early Stopping

Keras provides a way of doing this using the EarlyStopping callback. There are a range of callbacks in Keras and you can define your own. Details are here.

The EarlyStopping callback works by choosing a metric that you want to watch. Indicating what level of change in it is unacceptable and how many epochs you are prepared to tolerate that performance before the training should be abandoned. 

Sample Code

First the import

from keras.callbacks import EarlyStopping

Then create an instance of the EarlyStopping class. In this case we are monitoring the accuracy. The metrics available to you are just those you defined when you compiled your model. patience is the number of epochs to tolerate a below standard change in the metric.

early_stopping = EarlyStopping(monitor='acc', patience=10, verbose=1)

Note that you can also define a min_delta, which is the amount of difference that the callback should notice, but the default of 0 is fine for our needs. 

Finally add the callbacks parameter when you are fitting your model. Note that it takes a list so that you can pass other callbacks too.

model.fit(X, Y, epochs=50, batch_size=5, verbose=1, callbacks=[early_stopping])

So in this case if the accuracy of your model has not improved for 10 consecutive epochs, training will be abandoned. Setting verbose to 1 is a good idea so that you get a better message that the callback ended training. Otherwise everything will just appear to stop.

A more sophisticated use of this callback could adjust a hyperparameter and try again. However a better way to achieve this may be with grid search. 



Comments

Popular posts from this blog

Building a choropleth map for Irish agricultural data

AutoCompleteTextView backed with data from SQLite in Android