Batch size 2026-11-05
Batch size counts the examples combined in a mini-batch. In the simple stochastic gradient descent loop where each group produces one parameter update, it controls how many examples contribute to that update. It differs from a training epoch, which counts a pass over the available training data.[ref]
Optimizing Model Parameters in the PyTorch tutorials. Hyperparameters and Optimization Loop
For an original counting example, partition ten examples with nominal batch size and retain the remainder. The actual sizes are , giving updates per training epoch. Discarding the remainder instead gives two updates and uses only eight examples. More generally the retained-remainder update count is for examples, provided there is one update per group.
Batch size also changes gradient sampling noise. If independently sampled component gradients at a fixed parameter have scalar variance , their sample mean hasFor an original exact example, suppose one component gradient is equally likely to be 0 or 2. Its mean is 1 and variance is 1. A two-example mini-batch gradient takes values with probabilities , giving variance . This calculation holds parameters fixed and assumes independent sampling; sampling without replacement has a finite population correction.
With gradient accumulation, several groups may contribute before an update. Two full four-example groups then involve eight examples per update if their gradients are normalized consistently. State whether a reported size counts the processed group or all examples contributing to an update. Lower sampling variance alone does not establish faster training for a fixed computation budget.
Mini-batch 2026-11-05
A mini-batch is a group of training examples processed together in one iteration of a learning algorithm. Its batch size counts examples, while a training epoch usually counts one pass over the available training data. In stochastic gradient descent, a mini-batch gradient combines the component gradients from the selected examples; the group of examples and the resulting numerical estimate are distinct objects.[ref]
Optimizing Model Parameters in the PyTorch tutorials. Hyperparameters and Optimization Loop: batch size, epochs and updates.
For an original counting example, partition eight examples into mini-batches with nominal batch size three and retain the final partial group. The sizes are , giving three updates in one training epoch if each group produces one update. Discarding the final partial group changes which observations participate.
Unequal group sizes also matter when reporting a loss function. At fixed model parameters, suppose the three mini-batch mean losses are . Their unweighted mean is , but the mean over all eight examples isThe smaller last group must not receive the same weight as a larger group when the intended statistic is a per-example sample mean. This bookkeeping issue is separate from the randomness used to select a mini-batch.
PyTorch 2026-11-05
PyTorch is a software framework for numerical tensor operations and machine learning, including automatic differentiation and tools for training neural networks. Its array operations provide the calculations; its differentiation machinery records how a result depends on selected inputs. A computed gradient is distinct from the later optimization step that uses it.[ref]
Automatic Differentiation with torch.autograd in the PyTorch tutorials. Tensors, Functions and Computational graph; Computing Gradients
import torch
x = torch.tensor(2.0, requires_grad=True)
y = x * x + 3 * x
y.backward()
print(x.grad) # tensor(7.)The result agrees with . The call to
backward() computes and accumulates derivatives; it does not itself change x to minimize y. In a training loop, parameter updates and clearing old gradients are separate operations.[ref]Optimizing Model Parameters in the PyTorch tutorials. Optimizing with torch.optim; zero_grad, backward and step
A useful consequence of its dynamically constructed computation is that executed control flow determines the recorded graph. Recording a new graph on the next execution makes it possible to differentiate the computation that actually ran. Disabling gradient tracking or detaching a value changes that connection and must be deliberate.[ref]
Automatic Differentiation with torch.autograd in the PyTorch tutorials. More on Computational Graphs; Disabling Gradient Tracking
Training epoch 2026-11-05
A training epoch is conventionally one pass through the training data used by an iterative learning procedure. Several parameter updates can occur within that pass. A mini-batch supplies a group of examples for an update, while the epoch counts progress through the data collection rather than counting individual updates.[ref]
Optimizing Model Parameters in the PyTorch tutorials. Hyperparameters, number of epochs and batch size; Optimization Loop, outer epoch and training loop
Suppose there are training examples. Visit each exactly once per pass, form mini-batches of at most examples and retain the smaller final group. Their sizes are . With one update for every mini-batch, an epoch hasupdates. Five epochs therefore make fifteen updates and fifty example visits. Fifty visits do not mean fifty distinct examples: the same ten examples are reused.
Changing the mini-batch size changes the update count. Taking all ten examples together gives one update per epoch under the same rule, while groups of two give five. An epoch count alone consequently does not specify the number of stochastic gradient descent steps, the amount of computation or the final fitted model.
The finite-pass assumption matters. Dropping an incomplete final group in this example gives two updates and only eight visits during that iterator traversal. Reordering the examples on later passes can change which examples are omitted. Sampling with replacement can repeat some examples before visiting others, so a chosen number of sampled updates need not be a literal once-per-example pass. Report those conventions rather than assuming that every loop named epoch guarantees full coverage.
The learning rate schedule needs a declared clock too: changing the learning rate once per epoch differs from changing it once per mini-batch. The optimization tutorial separates the outer loop from the operations that clear gradients, perform backpropagation and update parameters inside the training loop.[ref]
Optimizing Model Parameters in the PyTorch tutorials. Optimizer, zero_grad/backward/step; Full Implementation, nested training and epoch loops
Keeping data passes and update steps distinct makes training runs easier to compare.