Home Knowledge Base Epoch, Batch, and Iteration

Epoch, Batch, and Iteration are the fundamental time-keeping units of neural network training — defining how training data is organized, processed, and used to update model parameters. Understanding their relationship is essential for configuring training runs, interpreting loss curves, setting learning rate schedules, and comparing results across different research papers and implementations.

Core Definitions

Epoch — one complete pass through the entire training dataset.

Mini-batch (Batch) — a subset of training samples processed together in a single forward-backward pass.

Iteration (Step) — one weight update from one mini-batch.

The Mathematical Relationship

$$\text{Iterations per epoch} = \left\lceil \frac{N_{\text{train}}}{B} \right\rceil$$
$$\text{Total iterations} = \text{Epochs} \times \text{Iterations per epoch}$$

Example: ImageNet (1.28M images), batch size 256, 90 epochs:

Training Loop Structure

for epoch in range(num_epochs):           # outer loop: dataset passes
    dataloader.shuffle()                   # randomize order each epoch
    for batch_x, batch_y in dataloader:   # inner loop: mini-batches
        optimizer.zero_grad()              # clear previous gradients
        predictions = model(batch_x)       # forward pass
        loss = criterion(predictions, batch_y)  # compute loss
        loss.backward()                    # backpropagate gradients
        optimizer.step()                   # update weights
        iteration += 1                     # count step
    validate(model)                        # evaluate after each epoch

This triple structure — dataset → epoch → batch → iteration — is the heartbeat of all neural network training.

LLM Pre-training: Token-Based Counting

Large language models redefine these concepts around tokens rather than samples:

Learning Rate Scheduling and Steps

Learning rate schedules operate on steps, not epochs:

Schedule TypeStep BehaviorUsed In
Linear warmupLR increases from 0 to $\eta_{max}$ over first $T_{warmup}$ stepsLLMs, transformers
Cosine decayLR follows cosine from $\eta_{max}$ to $\eta_{min}$ over $T$ stepsGPT, LLaMA, most modern LLMs
Step decayMultiply by 0.1 at milestone steps/epochsResNet ImageNet training
ConstantFixed LR throughoutSimple baselines, evaluation

Standard LLM training: 1-2% warmup steps, then cosine decay for remainder.

Shuffling and Data Order

Shuffle training data before each epoch:

Gradient Accumulation and Virtual Batch Size

When GPU memory limits batch size, gradient accumulation enables larger virtual (effective) batches:

$$B_{\text{effective}} = B_{\text{micro}} \times N_{\text{accum}} \times N_{\text{GPUs}}$$

One iteration in terms of weight updates corresponds to $N_{\text{accum}}$ forward-backward micro-steps. Training logs typically count optimizer steps (weight updates), not micro-steps.

Practical Guidance

Epoch, batch, and iteration are the vocabulary of training — every training script, research paper, and debugging conversation uses these terms, and their precise relationship determines how learning rate, regularization, and compute budget interact.

epochiterationbatchmini-batchtraining looptraining stepsdeep learning training

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.