Khám phá tác động của kích thước lô (batch size) đối với học sâu. Tối ưu hóa tốc độ huấn luyện, mức sử dụng bộ nhớ và hiệu suất mô hình một cách hiệu quả.
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.
Khi sử dụng Ultralytics Python bưu kiện, setting the batch size
is straightforward. You can specify a fixed integer or use the dynamic batch=-1 cài đặt, sử dụng Tính năng AutoBatch Tự động tính toán kích thước lô tối đa mà phần cứng của bạn có thể xử lý một cách an toàn.
Ví dụ sau đây minh họa cách huấn luyện mô hình YOLO26 — tiêu chuẩn mới nhất về tốc độ và độ chính xác — sử dụng thiết lập batch cụ thể.
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.