What is an epoch in machine learning?
An epoch in machine learning is one full pass through the whole training dataset, where the model sees every training example exactly once.
Models rarely learn enough from seeing the data a single time. So training repeats the process: epoch 1, epoch 2, epoch 3 and so on. In each pass the model makes predictions, measures how wrong it was (the loss), and nudges its internal weights, also called parameters, to do a little better next time.
The number of epochs is a hyperparameter. That means you choose it before training, just like the learning rate or the batch size. It is not something the model learns by itself.
What is an epoch in simple words?
In simple words, an epoch is one complete revision of your full syllabus.
Say you are revising a 10 chapter science book before your exam. You do not read the whole book in one sitting. You read 2 chapters per sitting, so finishing the book takes 5 sittings. Finishing the book once is one epoch. Each sitting is one iteration, and the 2 chapters you read per sitting are your batch.
Most students revise the book more than once, because each round makes the concepts clearer. A model is the same. But revising the same book 50 times can turn into pure memorising, which is the machine learning problem called overfitting.
What is epoch and batch size in machine learning?
Batch size is how many training examples the model looks at before it updates its weights once, while an epoch is the full pass over all examples, made up of many such batches.
The three terms fit together with one simple formula:
iterations per epoch = number of training samples / batch size total iterations = iterations per epoch x number of epochs
Here is a worked example with 1,000 training samples, a batch size of 100 and 20 epochs:
| Term | Meaning | Value in the example |
|---|---|---|
| Sample | One training example, such as one image or one sentence | 1,000 samples |
| Batch size | Samples processed before one weight update | 100 |
| Iteration (step) | One weight update using one batch | 1,000 / 100 = 10 per epoch |
| Epoch | One full pass over all samples | 10 iterations |
| Total training | All epochs together | 10 x 20 = 200 iterations |
If the numbers do not divide evenly, say 1,050 samples with a batch size of 100, most libraries run 11 iterations and the last batch simply has 50 samples, unless you tell them to drop the incomplete batch.
Epoch, batch and iteration questions show up often in machine learning interviews, usually as a quick calculation like the one above.
What happens during one epoch of training?
During one epoch, the model goes through every batch of the training data, learns from each one, and is then usually checked on separate validation data.
For a neural network, each iteration inside the epoch follows the same loop:
- Forward pass: the model makes predictions for the batch.
- Loss: a loss function measures how far those predictions are from the correct answers.
- Backward pass: backpropagation works out how each weight contributed to the error.
- Update: an optimiser such as SGD or Adam adjusts the weights slightly.
At the end of the epoch, most training scripts print the average training loss and the validation loss. That is why searches like "epoch and loss" go together: you watch the loss fall epoch by epoch to judge whether learning is working. Many setups also shuffle the data before each epoch so the model does not learn the order of examples.
How many epochs should you use?
No single number of epochs is correct; you should train until validation performance stops improving and then stop.
The right number depends on the dataset size, the model and the learning rate. Some rough patterns, which vary a lot by project:
- Small datasets and simple models: often tens to a few hundred epochs.
- Large image or text models trained from scratch: fewer passes, because each epoch already contains a huge amount of data.
- Fine-tuning a pretrained model: often only a few epochs, since the model already knows a lot. When you fine-tune an LLM on a small dataset, too many epochs can make it parrot your examples.
The practical answer is early stopping. You set a generous maximum, monitor validation loss, and stop when it has not improved for a few epochs (called patience). In Keras it looks like this:
early_stop = EarlyStopping(monitor="val_loss", patience=3, restore_best_weights=True)
model.fit(x_train, y_train, epochs=100, batch_size=32,
validation_data=(x_val, y_val), callbacks=[early_stop])I would never pick the epoch count by guessing. Plot the training and validation loss once, and the curve will tell you where to stop.
How are epochs related to overfitting and underfitting?
Too few epochs lead to underfitting and too many epochs can lead to overfitting.
- Too few epochs: the model has not had enough rounds to learn the pattern, so both training and validation accuracy stay low.
- The right range: training and validation accuracy improve together.
- Too many epochs: training accuracy keeps climbing, but validation accuracy flattens or drops because the model starts memorising noise.
The epoch where validation loss is lowest is usually the best version of your model, which is exactly what early stopping with restored best weights saves for you.