Stochastic Gradient
A stochastic gradient estimates the full training gradient using one example or a mini-batch. It is the reason large models can be trained without evaluating the entire dataset at every step.
For empirical risk:
the full gradient is:
For a mini-batch :
is a stochastic estimate of the full gradient at step .
Noise and Batch Size
If mini-batches are sampled uniformly:
but the variance depends on batch size and data heterogeneity. This connects directly to batch size, learning rate, and training stability.
An informal variance relationship is:
where is the per-example gradient covariance. Larger batches usually reduce gradient noise, but they also change memory use, throughput, optimizer dynamics, and sometimes generalization.
Biased Estimates
The stochastic gradient is unbiased only under the sampling distribution assumed by the objective. If the sampler uses weights while the intended empirical risk is uniform, the expected gradient becomes:
This is a different objective unless importance weights correct it:
Class-balanced sampling, hard-example mining, padding masks, token weighting, and multi-task sampling all change what the gradient estimates.
Distributed Averaging
With workers, each worker computes a local mini-batch gradient . Synchronous data parallel training usually averages:
This is equivalent to a larger global batch only if workers use compatible data shards, loss normalization, mixed-precision scaling, and accumulation boundaries.
Checks
- Is the mini-batch sampled from the intended distribution?
- Are class weights, masks, or sampling weights changing the gradient estimate?
- Is gradient accumulation equivalent to the intended batch size?
- Are distributed workers averaging gradients correctly?
- Is gradient noise helping generalization or causing instability?
- Is the loss normalized by examples, tokens, positive labels, or valid elements?
- Are rare classes oversampled for training but evaluated under natural prevalence?
- Are compared runs using the same effective batch and learning-rate schedule?