Gnox AI / Blog

,

Epochs in Machine Learning: Batches, Steps and Stopping

Abstract diagram illustrating machine learning

An epoch in machine learning is usually one complete pass through the training dataset. If you train on 1,030 examples in batches of 64 and keep the final partial batch, one epoch contains 17 batches. Sixteen hold 64 examples each, and the last holds six.

Epochs tell you how much data the training loop has covered. To see how often the model’s weights change, you also need the batch size and update schedule.

For the calculations that take place inside each batch, see how neural networks turn inputs into predictions.

Epoch, batch, and training step

A batch is a group of examples processed together. In a basic training loop, the model computes a gradient from a batch and the optimizer uses that gradient to update the weights. A training step often refers to that batch or the resulting update, depending on how the framework logs it. An epoch groups those steps into a pass through the data.

If you use gradient accumulation, the model processes multiple batches before the optimizer updates the weights. For example, four batches of 16 can contribute to a single update. This matters when a learning-rate schedule is measured in optimizer updates and a progress bar is counting batches.

For language models specifically, see what LLM parameters are and how the trained model reuses those weights when it processes new tokens.

How to calculate steps per epoch

Divide the number of training examples by the batch size. Round up if the input loader keeps the final partial batch. Round down if it drops that batch. For the 1,030-example run above, keeping it gives 17 batches per epoch and dropping it gives 16. Over five epochs, that is 85 or 80 batches respectively. With one optimizer update per batch, those are also the update counts.

Use the training split for the calculation. If you started with 1,200 examples and held out 170 for validation, the training loop uses the remaining 1,030. The held-out examples measure how the model performs on data it did not train on. The AI-ready data guide covers how to prepare and separate these sets.

When an epoch is a fixed number of steps

A repeating or streaming dataset may have no natural endpoint. Training frameworks can define an epoch as a fixed number of batches in that case. In Keras, steps_per_epoch=500 tells the fit loop to complete an epoch after 500 batches. If validation is enabled and scheduled every epoch, Keras then runs validation and logs the results.

Two runs with the same number of epochs may therefore process different amounts of data. Record the batch size, the number of batches per epoch, and the update schedule when you compare their results.

How many epochs should you train for?

Choose an initial training budget and monitor a validation metric that reflects the task. For example, you might cap a first run at 20 epochs and monitor validation loss. If training loss keeps falling while validation loss rises over several epochs, the model may be overfitting. If both curves have flattened at poor performance, review the data, model, or learning rate before simply adding epochs.

Suppose validation loss is 0.52 at epoch 3, 0.46 at epoch 4, 0.48 at epoch 5, and 0.50 at epoch 6. With a patience of two epochs and any decrease counted as an improvement, the run stops after epoch 6. Epoch 4 has the best measured validation loss. In Keras, set restore_best_weights=True if you want early stopping to restore that earlier set of weights.

Use validation data to choose the checkpoint. Save the test set for the final estimate of performance. The machine learning app tutorial shows how the trained model fits into an application, and the AI evals guide explains how to design repeatable checks.

When training on time-ordered data for a forecast, use chronological backtesting to keep the evaluation period after the training period.

These training concepts are useful when you need to compare deep learning with other machine learning approaches on the same task.

Frequently asked questions

Does an epoch include validation data?

A training epoch refers to the training data. A framework can run validation after the epoch and show both sets of metrics in the same log.

Does shuffling change the number of steps?

Shuffling changes the order of examples. The step count stays the same if the number of examples, batching, and the handling of incomplete batches stay the same.

Can a model train for just one epoch?

Yes. One epoch may be enough for some tasks or too little for others. Compare the validation result with a baseline before deciding whether to train longer.

Are a training step and an optimizer update the same?

They often match in a basic loop. With gradient accumulation, the optimizer may update only after several batches. Check how your framework labels the step counter.