Descubra o impacto do tamanho do lote no aprendizado profundo. Otimize a velocidade de treinamento, o uso da memória e o desempenho do modelo de forma eficiente.
In the realm of machine learning and particularly deep learning, Batch Size refers to the number of training examples utilized in one iteration of model training. Rather than feeding the entire training data into the neural network at once—which is often computationally impossible due to memory constraints—the dataset is divided into smaller subsets called batches. The model processes one batch, calculates the error, and updates its internal model weights via backpropagation before moving on to the next batch. This hyperparameter plays a pivotal role in determining both the speed of training and the stability of the learning process.
The choice of batch size fundamentally alters how the optimization algorithm, typically a variant of stochastic gradient descent, navigates the loss landscape.
Practitioners must often select a batch size based on hardware limitations rather than purely theoretical preference. Deep learning models, especially large architectures like transformers or advanced convolutional networks, are stored in the VRAM of a GPU.
When utilizing NVIDIA CUDA for acceleration, the VRAM must hold the model parameters, the batch of input data, and the intermediate activation outputs needed for gradient calculation. If the batch size exceeds the available memory, the training will crash with an "Out of Memory" (OOM) error. Techniques like mixed precision training are often employed to reduce memory usage, allowing for larger batch sizes on the same hardware.
To configure training effectively, it is essential to distinguish batch size from other temporal terms in the training loop.
Adjusting the batch size is a routine necessity when deploying computer vision solutions across various industries.
Ao utilizar o PacotePython Ultralytics, setting the batch size
is straightforward. You can specify a fixed integer or use the dynamic batch=-1 configuração, que utiliza
o Funcionalidade AutoBatch para calcular automaticamente
o tamanho máximo do lote que o seu hardware pode processar com segurança.
O exemplo a seguir demonstra como treinar um modelo YOLO26— o mais recente padrão em velocidade e precisão — usando uma configuração de lote específica.
from ultralytics import YOLO
# Load the YOLO26n model (nano version for speed)
model = YOLO("yolo26n.pt")
# Train on the COCO8 dataset
# batch=16 is manually set.
# Alternatively, use batch=-1 for auto-tuning based on available GPU memory.
results = model.train(data="coco8.yaml", epochs=5, batch=16)
For managing large-scale experiments and visualizing how different batch sizes affect your training metrics, tools like the Ultralytics Platform provide a comprehensive environment for logging and comparing runs. Proper hyperparameter tuning of the batch size is often the final step in squeezing the best performance out of your model.