Why do we average the cost across a mini-batch instead of summing it?

Quick Overview

Asks why training losses are averaged over a mini-batch instead of summed. Tests how the reduction affects gradient scale, learning rate behavior, regularization balance, and edge cases like gradient accumulation and padded sequences.

Why do we average the cost across a mini-batch instead of summing it?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Why do we average the cost across a mini-batch instead of summing it? ```hint Scale of the gradient Write the gradient of a summed loss over B examples and see what happens to its size, and to the step the optimizer takes, when B doubles. ``` ### What a Strong Answer Covers - How the gradient's scale depends on batch size under a sum and under a mean - What that does to the meaning of the learning rate and to training stability - What the batch mean estimates in statistical terms - Cases where the reduction needs care, such as gradient accumulation and padded sequences ### Follow-up Questions - With mean reduction, should the learning rate change when you double the batch size? - Does sum versus mean matter when the optimizer is Adam? - How should the loss be averaged over sequences of different lengths? - How does the choice interact with an L2 penalty added to the loss?

Overview: Asks why training losses are averaged over a mini-batch instead of summed. Tests how the reduction affects gradient scale, learning rate behavior, regularization balance, and edge cases like gradient accumulation and padded sequences.

PracHub logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Why do we average the cost across a mini-batch instead of summing it?

What a Strong Answer Covers Guidance

  • How the gradient's scale depends on batch size under a sum and under a mean
  • What that does to the meaning of the learning rate and to training stability
  • What the batch mean estimates in statistical terms
  • Cases where the reduction needs care, such as gradient accumulation and padded sequences

Follow-up Questions Guidance

  • With mean reduction, should the learning rate change when you double the batch size?
  • Does sum versus mean matter when the optimizer is Adam?
  • How should the loss be averaged over sequences of different lengths?
  • How does the choice interact with an L2 penalty added to the loss?