As explained in Wikipedia, the pocket algorithm is a very simple variant/addition of/to ANN which keeps a copy of the best model seen so far and returns that one as the trained model (instead of the actual final state of the model). Implementing it is very simple and straightforward.

I was wondering if this algorithm is implemented in Keras.


1 Answer 1


Yes this is usually part of the early stopping algorithm, where you supply a cross-validation data set, and a limit on number of epochs since best result so far.

In Keras, you can use an instance of the EarlyStopping class, choosing the metric that you want the best model for, and setting the patience parameter to limit the number of epochs to test after any best so far result. The instance is supplied to the fit method as a callback.

See http://parneetk.github.io/blog/neural-networks-in-keras/ for an example (last section)

  • $\begingroup$ Thanks for the answer but I don't think they are the same (please correct me if I'm wrong). The pocket algorithm does not stop the process in any way. It just returns the best-found result during the training process. In other words, the process goes all the way through every time. Because you'll never know if down the road some better metric is found or now unless you actually go all the way. $\endgroup$
    – Mehran
    Commented Aug 7, 2018 at 13:54
  • 1
    $\begingroup$ @Mehran That is what the patience parameter is for -to decide how much more training to do before it is enough. But perhaps I am missing some important detail of the pocket algorithm? $\endgroup$ Commented Aug 7, 2018 at 15:16
  • $\begingroup$ I confess that I didn't see that parameter. One question, if I'm training for 100 epochs and I set patience to 100 as well, and the best accuracy is found at epoch 50, will the training stop at 100 or 150? $\endgroup$
    – Mehran
    Commented Aug 7, 2018 at 16:55
  • $\begingroup$ @Mehran I am not sure. I think that it will stop at 100 in Keras. $\endgroup$ Commented Aug 7, 2018 at 17:01
  • $\begingroup$ I will definitely check this. I just hope that it will stop at 100 and also since it is stopping at 100 (and not 150) Early-stopping will still return the best solution and not the latest one. Thanks anyway. $\endgroup$
    – Mehran
    Commented Aug 7, 2018 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.