Batch Size Explained (Easy AI Tutorial Series)
1. What Is Batch Size?
1.1 Core Concept
Batch size is the number of samples used each time model parameters are updated during training.
1.2 Intuitive Analogy
Like reviewing homework:
- Large batch = doing many problems at once (fast but coarse)
- Small batch = doing one problem at a time (slow but precise)
- Use a small per-device batch size with multiple forward and backward passes
- Accumulate gradients from several small batches
- Update model parameters once at the end
- 💰 Pay 32 yuan at once
- 💾 Requires a lot of cash (GPU memory)
- 💸 Pay in 4 installments of 8 yuan each
- 💾 Only a small amount of cash needed (GPU memory)
- ✅ Same total amount paid in the end
- Start with
batch_size = 1or2 - Use gradient accumulation to increase the effective batch size
- Pair large batch sizes with large learning rates
- Gradient accumulation achieves the "installment payment" effect
- Balance memory usage and training quality
- Adjust flexibly according to hardware conditions
1.3 Two Key Parameters
Effective batch size = per-device batch size × gradient accumulation steps
---
2. Large vs. Small Batch Sizes
2.1 Advantages of Large Batches
| Advantage | Description | |-----------|-------------| | 🎯 More stable training | More accurate gradient estimates, smoother convergence | | 🏆 Easier convergence to global optimum | Reduces the risk of getting stuck in local optima | | ⚡ Higher computational efficiency | Fully utilizes GPU parallel computing |
2.2 Advantages of Small Batches
| Advantage | Description | |-----------|-------------| | 💾 Saves GPU memory | Suitable for memory-constrained scenarios | | 🔍 Captures data details | Gradient noise helps escape local optima | | 🌟 Stronger generalization | Reduces overfitting risk |
---
3. Gradient Accumulation
3.1 What Is Gradient Accumulation?
Gradient accumulation is an "installment payment" technique:
3.2 The "Installment Payment" Analogy
Traditional approach (direct large batch):
Gradient accumulation (installments):
---
4. Parameter Setting Recommendations
4.1 Small Models / Small Datasets
4.2 GPU Memory Optimization Tips
Source: Easy AI Tutorial series