Model Pipeline - Gradient accumulation
This pipeline shows how gradient accumulation helps train a model with small batches by adding gradients over multiple steps before updating the model weights.
This pipeline shows how gradient accumulation helps train a model with small batches by adding gradients over multiple steps before updating the model weights.
Loss
1.0 |*
0.8 | **
0.6 | ***
0.4 | ****
0.2 | ***
+--------
1 2 3 4 5 Epochs| Epoch | Loss ↓ | Accuracy ↑ | Observation |
|---|---|---|---|
| 1 | 0.85 | 0.60 | Loss starts high, accuracy moderate |
| 2 | 0.65 | 0.72 | Loss decreases, accuracy improves |
| 3 | 0.50 | 0.80 | Loss continues to decrease, accuracy rises |
| 4 | 0.40 | 0.85 | Model converging well |
| 5 | 0.35 | 0.88 | Loss low, accuracy high |
optimizer.zero_grad()
for i, (inputs, labels) in enumerate(dataloader):
outputs = model(inputs)
loss = criterion(outputs, labels) / 2
loss.backward()
if (i + 1) % 2 == 0:
optimizer.step()
optimizer.zero_grad()
print(f"Batch {i+1} loss: {loss.item()}")optimizer.zero_grad()
for i, (inputs, labels) in enumerate(dataloader):
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
if i % 4 == 0:
optimizer.step()
optimizer.zero_grad()Pass the test and this pattern moves unknown