AI Logic

Glossary

Batch Size

Updated:

On the left is the flow of one update, going upward from a mini-batch of B samples, through the model (computes the B samples together), the loss (average over B), and the gradient (backpropagation), to updating the weights once (gradient descent). On the right is a comparison of one pass over 32 training samples: with B = 4 there are 8 mini-batches and 8 updates, and with B = 16 there are 2 mini-batches and 2 updates. Below, it notes that a larger B uses more memory per step and reduces the number of updates, and that B is set to a power of 2 for GPU efficiency.

Explanation

Batch size is the number of samples fed into the network at once during the training of a deep learning model.

The model is usually updated by applying gradient descent during training, and batch size plays an important role in this process. Batch size is regarded as a factor that affects model performance, convergence speed and memory usage.

Performance improves when a large batch size (e.g., 256) is used with a large learning rate (e.g., 10^(-3)), and a small batch size (e.g., 16) is used with a small learning rate (e.g., 10^(-5)).

However, using a large batch size is not recommended.

In practice, it is recommended to start with a small batch size (e.g., 32, 64) and a small learning rate.

For GPU efficiency, the batch size should preferably be a power of two, 2^n.

Reference: The effect of batch size on the generalizability of the convolutional neural networks on a histopathology dataset (2020)

Where It Is Used

Batch size is used in various ways during model training and validation. In training, the batch size is specified so that the training data is processed in groups, and the weights are updated using backpropagation. For validation and evaluation, setting a batch size allows the test data to be processed efficiently and shortens inference time during validation. From the standpoint of memory management, it is important to adjust the batch size to control GPU or TPU memory usage and to train with appropriate computational resources. For these reasons, the best batch size depends on the characteristics of the model and the nature of the data, so it is important to find the optimal size through experiments.

FAQ

Q. What changes when the batch size is changed?
A.

It affects gradient noise, convergence speed, training stability and memory usage, and it also has a large impact on the training results of the model.

Q. What are the advantages of a small batch size?
A.

Gradients tend to contain more noise, which tends to improve generalization performance. Training is also possible with less memory.

Q. What are the advantages of a large batch size?
A.

Computation is more stable, and parallel processing enables fast training. However, care is needed because memory consumption can become too large.