In deep learning, the batch size and gradient accumulation serve different purposes and are used in different scenarios.
When using a batch size greater than one, the gradients for each element in the batch are calculated separately and then added together before performing a weight update. This is known as mini-batch gradient descent. By using a batch size greater than one, we can take advantage of parallelism and computational efficiency, as we can calculate the gradients for multiple elements simultaneously. Additionally, using a batch size greater than one can help reduce the variance of the gradients and lead to more stable training.
On the other hand, gradient accumulation is a technique used when the batch size is too large to fit into memory, or when the computational resources are limited. In gradient accumulation, the gradients for multiple mini-batches are calculated and accumulated before performing a weight update. This allows us to effectively train on larger batch sizes without running out of memory or exceeding our computational resources. However, instead of simply adding the gradients together like in mini-batch gradient descent, the gradients are typically averaged to ensure that each element in the larger batch contributes equally to the weight update.
So, in summary, the difference between batch size and gradient accumulation is that batch size adds the gradients of multiple elements together before performing a weight update, while gradient accumulation averages the gradients of multiple mini-batches together before performing a weight update. This is because the goals of the two techniques are different: batch size is used to improve computational efficiency and reduce variance, while gradient accumulation is used to train on larger batch sizes when memory or computational resources are limited.