What is an epoch and batch size in machine learning?
An epoch is one full pass of a model through its entire training dataset. The batch size is the number of examples the model processes before each weight update. One epoch therefore contains the dataset size divided by the batch size updates, rounded up, and each update is called an iteration or step.
Epochs and batch size together set how many updates training makes and how noisy each one is. Deep learning frameworks such as PyTorch and TensorFlow organize training as a loop over epochs, with an inner loop over shuffled mini-batches. Choosing these two values affects training time, GPU memory use, and how well the final model generalizes.
Key ideas
- An epoch is one complete pass through the training data.
- The batch size is how many examples are averaged for each weight update.
- Updates per epoch equal the dataset size divided by the batch size, rounded up.
- Smaller batches give more, noisier updates; larger batches give fewer, smoother ones.
- Shuffling every epoch keeps mini-batches representative of the whole dataset.
- The number of epochs is usually set by watching the validation loss.
Common questions
How many epochs are enough?
There is no fixed number. Track the validation loss after each epoch and stop when it has not improved for several epochs, keeping the best checkpoint. This is called early stopping, and the patience setting controls how many epochs without improvement to allow.
Does a larger batch size make training faster?
Each epoch usually finishes faster, because a GPU processes a large batch more efficiently than many small ones. The model also gets fewer updates per epoch, so it may need more epochs, a larger learning rate, or both to reach the same loss. Past a certain size, extra batch size stops reducing the number of updates needed.
What is gradient accumulation?
Gradient accumulation adds up the gradients from several small batches and applies one update after the last of them. It reproduces the effect of a larger batch when the larger batch does not fit in GPU memory. Training with 4 accumulated batches of 8 behaves much like training with batches of 32, though batch normalization statistics still use the smaller batches.
Should validation and test data be shuffled?
It is not necessary. Evaluation does not update the weights, so the order of examples does not change the metrics. Shuffling matters for training only, where the order decides which examples share a batch and in what sequence the updates happen.
Related explainers
- Gradient Descent — Optimizing by following the slope downhill
- Hyperparameters — The knobs you tune before training begins
- Optimizers — SGD, momentum, and Adam — smarter gradient descent
- Learning Rate Scheduling — Step decay, cosine annealing, warmup, and the LR range test
- Overfitting & Underfitting — The bias-variance tradeoff and generalization
- Train, Validation & Test Sets — Splitting data to measure generalization and avoid overfitting
References
- Goodfellow, Bengio & Courville (2016), "Deep Learning", section 8.1.3: Batch and Minibatch Algorithms
- Bottou (2010), "Large-Scale Machine Learning with Stochastic Gradient Descent", COMPSTAT
- Masters & Luschi (2018), "Revisiting Small Batch Training for Deep Neural Networks"
- Devlin et al. (2018), "BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding"
Last updated