What is the default batch size of pytorch SGD?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
torch.optim.SGD does not have a batch-size setting and therefore has no default batch size of its own. The optimizer simply updates parameters using whatever gradients your training loop has accumulated. In practice, batch size comes from the data pipeline, most often from DataLoader.
Where Batch Size Actually Comes From
In a typical PyTorch training loop, batching happens before the optimizer step. The loader yields a batch, the model computes a loss on that batch, and loss.backward() accumulates gradients for that batch size.
Here, the effective batch size is 32 because that is what the DataLoader emits. The optimizer does not override or redefine that number.
What If You Do Not Set batch_size
If you use DataLoader and omit the batch_size argument, PyTorch defaults the loader batch size to 1. That can create confusion, because people sometimes see single-sample updates and assume the optimizer imposed that behavior. It did not. The loader did.
There is also no requirement to use a DataLoader at all. You can build batches manually, use full-batch training, or accumulate gradients across several mini-batches before stepping the optimizer. SGD is agnostic to all of that.
Why This Distinction Matters
Batch size affects memory use, gradient noise, and update frequency. Learning rate, momentum, and weight decay affect how the optimizer transforms those gradients into parameter updates. Mixing those responsibilities conceptually makes tuning harder.
Once you separate them, debugging becomes clearer:
- change
batch_sizein the input pipeline - change
lr,momentum, orweight_decayin the optimizer - change gradient accumulation logic in the training loop
Those are related decisions, but they live in different parts of the code.
Gradient Accumulation Is Not a Hidden Batch Size
Some training loops simulate a larger effective batch size by accumulating gradients over several mini-batches before calling optimizer.step().
In this pattern, the optimizer still has no built-in batch size. The training loop is controlling the effective update size by deciding when gradients are applied.
Common Pitfalls
The most common mistake is looking for a batch_size argument on torch.optim.SGD. It is not there because batching is not the optimizer's job.
Another mistake is forgetting that DataLoader defaults to 1 when batch_size is omitted. That can make training unexpectedly slow and noisy if you assumed mini-batches were happening automatically.
Be careful when comparing experiments. If you change batch size, you may also need to revisit the learning rate and training schedule. Those settings interact, but they are still different knobs.
Finally, do not confuse batch size with dataset size or epoch length. Batch size is only the number of samples used for one gradient computation step.
Summary
- '
torch.optim.SGDhas no default batch size because it does not own batching.' - Batch size normally comes from
DataLoader, where the default is1if you omit it. - The optimizer only uses the gradients produced by your training loop.
- Gradient accumulation changes the effective update size without changing the optimizer API.
- Tune batch size and optimizer hyperparameters separately so your training logic stays clear.
Related reading
- What is the default variable initializer in Tensorflow?
- What is the definition of a non-trainable parameter?
- What is the difference between an Embedding Layer and a Dense Layer?
- What is the difference between back-propagation and feed-forward Neural Network?
- What is the difference between detach, clone and deepcopy in Pytorch tensors in detail?
- What is the difference between .flatten and .view-1 in PyTorch?
- What is the default variable initializer in Tensorflow?
- What is the difference between a Bayesian network and a naive Bayes classifier?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.