Gradientenakkumulation für große Batches
Simulieren Sie große Batches auf kleinen GPUs.
Gradientenakkumulation für große Batches ist eine kostenlose Deep Learning Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Deep Learning Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Deep Learning Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
The Big-Batch Problem
Large batches often train more smoothly, but they also need lots of GPU memory. A small card simply cannot hold a giant batch at once.
The Core Trick
Gradient accumulation splits one big batch into small chunks. You add up their gradients and update once, as if the whole batch ran together.
Gradients Already Accumulate
PyTorch adds each backward pass into .grad rather than replacing it. This default behavior is exactly what accumulation relies on.
Pick an Accumulation Count
Choose how many mini-batches make one update. With accum_steps of four, four small batches behave like one batch four times larger.
accum_steps = 4Do Not Zero Every Step
The key change is timing: zero_grad only at the start of an accumulation cycle, not after every single mini-batch.
if step % accum_steps == 0:
optimizer.zero_grad()Scale the Loss
Divide each mini-batch loss by accum_steps before backward. This keeps the average gradient identical to running the full batch at once.
loss = loss_fn(model(x), y) / accum_steps
loss.backward()Step Only When Full
After enough mini-batches pile up, call optimizer.step. The accumulated gradients now reflect the whole large batch.
if (step + 1) % accum_steps == 0:
optimizer.step()The Full Pattern
Put it together: scale the loss, backward every step, but only step and zero once per cycle. The loop stays simple.
for step, (x, y) in enumerate(loader):
loss = loss_fn(model(x), y) / accum_steps
loss.backward()
if (step + 1) % accum_steps == 0:
optimizer.step()
optimizer.zero_grad()Memory Stays Small
You only ever hold one mini-batch in memory at a time. That is why a modest GPU can mimic a batch many times its real capacity.
The Trade-Off
Accumulation trades time for memory: more forward and backward passes per update mean each effective batch takes a little longer to finish.
Mind Batch Norm
Batch norm still sees only the small mini-batch, so its statistics are noisier than a true large batch would produce. Keep that in mind.
Quick Check
You accumulate over 4 mini-batches. When should you call optimizer.step()?
Recap
Split a big batch into chunks, divide the loss by accum_steps, backward every chunk, and step only once per cycle to fake a large batch on small memory. 🧮
Häufig gestellte Fragen
Ist die Lektion „Gradientenakkumulation für große Batches“ kostenlos?
Ja — der vollständige Text von „Gradientenakkumulation für große Batches“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Deep Learning Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Deep Learning Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Gradientenakkumulation für große Batches“?
Simulieren Sie große Batches auf kleinen GPUs. Du übst Deep Learning Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Deep Learning Academy zu starten?
Keine Vorkenntnisse erforderlich. Deep Learning Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Gradientenakkumulation für große Batches“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Deep Learning Academy-Lektion Code schreiben und ausführen?
Ja. Jede Deep Learning Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Gemischte Präzision mit autocast und GradScaler
- Gradientenakkumulation für große Batches
- Den Flaschenhals profilieren
- GPU-Speicherverbrauch senken