Epochs, Batches and Training Time Explained
Three words come up the moment you start training a model: epochs, batches and steps. They are simple ideas, and once you have them you can estimate how long a training run will take using nothing more than division. This guide explains each term, works through real numbers, and shows why the one number you must measure yourself is how fast your own setup goes.
The words
- Training set: the examples the model learns from (not the validation or test examples; see the train, validation and test split).
- Epoch: one complete pass over the training set, so the model has seen every training example once.
- Batch: the group of examples used for one update. Instead of adjusting the model after every single example, or only once after all of them, most training looks at a batch (say 64 examples), works out how to adjust the model to reduce its error on that batch, and makes one small update.
- Step (or iteration): one update, which is one batch.
- Batch size: how many examples are in each batch.
Models are usually trained for many epochs, because one pass is rarely enough for the weights to settle. The guide to linear regression and gradient descent shows what one update actually does, with a small example you can follow.
Steps per epoch
steps per epoch = training examples / batch size, rounded up
It is rounded up because the last batch can be smaller than the rest. With 50,000 training examples and batches of 64, 50,000 / 64 = 781.25. That means 781 full batches of 64 (49,984 examples) plus one last batch of the remaining 16. So there are 782 steps per epoch.
total steps = steps per epoch x epochs
For 20 epochs that is 782 x 20 = 15,640 steps.
Training time
The time estimate does not use the step count at all. It uses how many examples get processed in total, and how many your setup can process per second:
total examples processed = training examples x epochs
seconds = total examples processed / examples per second
"Examples per second" is called throughput. It depends on the model, the size of each example, the batch size, the software and the hardware, so there is no general figure for it. You have to measure it on your own setup (see below).
The Training Time Estimator does exactly this arithmetic. It shows seconds to one decimal place and a duration in whole hours and whole minutes, rounded down. Its pre-filled throughput of 2,000 examples per second is only a placeholder for you to replace with your measured figure.
Worked examples
| Training examples | Epochs | Batch size | Examples per second | Steps per epoch | Total steps | Seconds | Shown as |
|---|---|---|---|---|---|---|---|
| 50,000 | 20 | 64 | 2,000 (placeholder) | 782 | 15,640 | 500 | 0h 8m |
| 60,000 | 10 | 32 | 500 | 1,875 | 18,750 | 1,200 | 0h 20m |
| 60,000 | 10 | 128 | 500 | 469 | 4,690 | 1,200 | 0h 20m |
| 1,000,000 | 100 | 256 | 1,000 | 3,907 | 390,700 | 100,000 | 27h 46m |
Check the first row by hand: 50,000 x 20 = 1,000,000 examples processed; 1,000,000 / 2,000 = 500 seconds, which is 8 minutes and 20 seconds, shown as 0h 8m because minutes are rounded down. The last row is 100,000 seconds, a little over a day, which the tool shows as 27 hours and 46 minutes rather than in days.
The batch size puzzle
Look at rows two and three. Batch 32 needs 18,750 steps and batch 128 needs only 4,690, yet both show 20 minutes. That is not a mistake: the calculator holds throughput fixed at the 500 examples per second you typed in, so the same 600,000 examples take the same time whatever the batch size.
Real training does not work like that. Changing the batch size usually changes throughput, often in ways that are hard to guess: bigger batches can let the hardware do more work at once, until they run out of memory or stop helping. Batch size also changes how training behaves (how noisy each update is and how many updates happen), which can change how many epochs you need. So whenever you change the batch size, measure throughput again rather than reusing the old number.
How to measure your throughput
You do not need special tools. Time a short run:
- Set up training exactly as you plan to run it: same model, same data, same batch size, same computer.
- Start training and let a few batches go by first. The first steps are often slower while things load and warm up.
- Note the time, let a fixed number of batches run (for example 200), and note the time again.
- Throughput = (batches x batch size) / seconds taken.
A made-up example: suppose 200 batches of 64 (12,800 examples) took 16 seconds. Throughput is 12,800 / 16 = 800 examples per second. With 48,000 training examples and 10 epochs at that speed, the estimator gives 750 steps per epoch, 7,500 steps in total and 600 seconds: 0h 10m.
If your real run also scores the validation set or saves the model regularly, either time a stretch that includes those or add their time separately.
Why the estimate is a floor
The calculation counts only time spent on training examples. Real runs also spend time on:
- Loading and preparing data, such as reading files from disk or resizing images.
- Validation: scoring the model on the validation set, often after every epoch.
- Saving checkpoints: copies of the model written to disk so you do not lose progress.
- Starting up: loading libraries and the model before the first step.
So treat the result as the least time the run could take, and expect it to take longer.
Early stopping: you may not need every epoch
Choosing "100 epochs" is a guess. Often the validation score improves for a while, then levels off, and then starts getting worse as the model begins to memorize the training set. That last part is overfitting, explained in overfitting and underfitting.
Early stopping handles this. After each epoch, score the model on the validation set. Keep a copy of the version with the highest validation score so far. If the score has not improved for a set number of epochs in a row (often called the patience, for example 5), stop and use the saved copy. That saves time and usually gives a model that does better on new data.
Putting it together
- Count your training examples. The Dataset Size Calculator splits your rows into training, validation and test counts if you have not done that yet.
- Pick a batch size and a maximum number of epochs.
- Measure throughput with a short timed run at that batch size.
- Estimate the time, then add room for data loading, validation and saving.
- Use early stopping so you do not spend time on epochs that do not help.
Small models on small datasets, like the ones in the guides on this site, often train in seconds on an ordinary laptop or school computer. The arithmetic matters most when runs get long enough that a wrong guess costs you an afternoon.