Solved – K-fold cross validation for choosing number of epochs

cross-validationhyperparametermachine learningneural networksvalidation

I'm trying to figure out the optimal number of epochs I need to train a convolutional network for, using 4 fold cross validation. I divided the dataset into 4 parts, trained the model on 3 parts, while keeping out one for validation. Repeated this thrice with the other parts as validation set.

After this experiment I found that, models on first two folds achieved 100% accuracy on validation set. The number of epochs it takes for each fold to achieve the highest possible accuracy on validation fold is wide apart viz. 21(100% on validation fold), 34(100%),36(98.97%) , 40(99.65%). My idea was to find the optimal number of epochs and then train the model on all the data for that many epochs. How do I choose the number of epochs from this experiment?
If this approach is inadequate, how should I change it?

Best Answer

Think of accuracy on the validation set as an estimate of accuracy on future data, given the value of some hyperparameter. In this case, the hyperparameter of interest is the number of training epochs. So: for each CV fold, train the network (e.g. up to some maximum number of epochs). After each epoch, record accuracy on the validation set. Compute the average validation set accuracy (across CV folds) for each number of epochs. Choose the number of epochs that maximizes this value.