Neural Network Epochs, Batches, Steps & One Pass Explained
In this article
An epoch is one complete pass of the entire training dataset through the neural network, a batch is a subset of that data processed together, and a step (or iteration) is one update of the model's weights using a single batch. So, one pass means the network has seen every training example exactly once, whether that happens in one giant batch or many small ones.
Why Training Is Split Into Batches
Neural networks learn by adjusting their internal weights to reduce prediction error. The most common method, gradient descent, requires calculating the error gradient—the direction and magnitude of weight changes—based on the training data. Using the entire dataset for every update is called batch gradient descent. It gives a very accurate gradient but is often impractical because:
- The dataset may not fit in memory (e.g., millions of images).
- Computing gradients over all examples is extremely slow.
- It tends to converge to local minima more readily, which can hurt generalization.
Instead, practitioners use mini-batch gradient descent, where the dataset is divided into small, random subsets called batches. The model updates its weights after each batch, making training faster and adding a degree of noise that often helps the model find better solutions. If a batch size equals the whole dataset, you have batch gradient descent; if it equals one sample, it's called stochastic gradient descent (SGD).
Defining Epoch, Batch, and Step Precisely
These three terms are tightly linked by a simple relationship:
- Epoch: One full pass over the entire training set. The order of samples is typically shuffled before each epoch to prevent the model from learning order-dependent patterns.
- Batch size: The number of training examples used in one forward and backward pass. Common sizes are 32, 64, 128, or 256, though any positive integer works.
- Step (or iteration): One weight update performed after processing one batch. The number of steps per epoch equals the number of batches, which is the total samples divided by the batch size (rounded up if the last batch is smaller).
For example, with 1,000 training samples and a batch size of 100, each epoch contains 10 steps. After 10 epochs, the network has performed 100 weight updates and has seen each sample 10 times.
How to Calculate Steps Per Epoch and Total Steps
To plan training, you need to know how many updates will happen. The formulas are straightforward:
- Steps per epoch = ceil(Number of training samples / Batch size). The ceiling function handles cases where the last batch is incomplete.
- Total steps = Steps per epoch × Number of epochs.
Concrete example: A dataset of 10,000 images, batch size of 64, trained for 50 epochs.
- Steps per epoch = ceil(10000 / 64) = 157 (the last batch has only 16 images).
- Total steps = 157 × 50 = 7,850 weight updates.
Most deep learning frameworks (TensorFlow, PyTorch) will automatically handle the final partial batch, but knowing the count helps you estimate training time and set learning rate schedules that decay based on step number.
Choosing Batch Size and Number of Epochs
There is no universal best value, but the following guidelines reflect common practice and research findings:
- Batch size: Larger batches (e.g., 128–512) speed up training by better utilizing GPU parallelism, but they require more memory and can lead to sharper minima that generalize worse. Smaller batches (e.g., 16–64) converge slower per step but often generalize better and allow larger learning rates. A common starting point is 32 or 64.
- Number of epochs: This is best determined by monitoring validation performance. Train until validation loss stops improving, then stop (early stopping). Too few epochs underfits; too many overfits. There is no fixed number—it depends entirely on the dataset and task.
- Learning rate interaction: Smaller batches have noisier gradients, so you may need to lower the learning rate. Conversely, large batches can often tolerate a higher learning rate, but may require scaling it up with batch size (a technique known as linear scaling).
A practical approach: start with a batch size of 32 and a moderate learning rate (e.g., 0.001 for Adam), then adjust based on training curves. If GPU memory allows, try 64 or 128 and compare validation accuracy.
Common Misconceptions and Pitfalls
Several misunderstandings frequently trip up beginners:
- “One epoch equals one step”—Only true when batch size equals the entire dataset. In most real training, an epoch contains many steps.
- “More epochs always improve accuracy”—False. After a point, the model memorizes training data and performs worse on unseen data (overfitting). Validation metrics are the true guide.
- “Shuffling is optional”—If you don't shuffle before each epoch, the model may learn the order of the data, which can hurt generalization. Always shuffle unless you have a specific reason not to.
- “Batch size must divide the dataset evenly”—Not required. Frameworks handle the last partial batch automatically, though some researchers prefer to drop it for consistency.
- “Larger batches are always better”—They use hardware efficiently, but can degrade model quality if the learning rate isn't adjusted. Research has shown that very large batches often need specially tuned schedules.
Epochs vs. Steps in Practice
When reading training logs or code, you'll see both terms. For example, a training loop in PyTorch might iterate over epochs, and within each epoch, iterate over batches—each inner loop iteration is a step. In TensorFlow's model.fit(), you specify epochs and batch_size, and the framework computes steps automatically. Some libraries also allow you to specify steps_per_epoch manually, which overrides the automatic calculation—useful when you want to cap the number of batches per epoch (e.g., for very large datasets or to speed up debugging).
A key practical difference: epochs are about data coverage, while steps are about weight updates. If you double the batch size while keeping epochs constant, you halve the number of steps, meaning fewer weight updates. This can change the effective learning dynamics, so you often need to adjust the learning rate or number of epochs accordingly.
Frequently Asked Questions
What is the difference between a batch and an epoch?
A batch is a subset of training samples processed together in one forward and backward pass. An epoch is one complete pass through the entire training dataset, which consists of one or more batches. You can think of an epoch as a full lap around the data, and a batch as a single step within that lap.
How many steps are in one epoch?
Steps per epoch equals the number of batches, which is the total number of training samples divided by the batch size, rounded up. For example, 1,000 samples with a batch size of 250 gives 4 steps per epoch. If the batch size is 300, you get 4 steps (the last batch has 100 samples).
Should I use more epochs or a larger batch size?
It depends on your constraints. If you have limited GPU memory, use a smaller batch and more epochs. If training is too slow, increase batch size (if memory allows) and possibly reduce epochs, but monitor validation performance. There's no fixed rule—experiment with both and track validation loss.
What happens if I set batch size to 1?
That's stochastic gradient descent (SGD). The model updates weights after every single sample, which is very noisy and can bounce around the loss landscape. It's slow per epoch (many steps) but can sometimes escape poor local minima. In practice, mini-batches of 16–128 are more common for stability and speed.
Why do I see “steps_per_epoch” in TensorFlow?
It's a manual override for the number of batches per epoch. By default, TensorFlow calculates it as ceil(samples / batch_size). You might set it lower to speed up experimentation or to match a custom data pipeline. If you set it too high, training will repeat data; too low, and some data won't be seen in that epoch.
Does a larger batch size require a higher learning rate?
Often yes, but not automatically. Research suggests that when you increase batch size by a factor of k, you can increase the learning rate by up to k (linear scaling rule), but this isn't always stable. In practice, you should test different learning rates for your chosen batch size, starting with the default and adjusting based on training loss curves.