Deep Learning Academy · レッスン

大きなバッチのための勾配累積

小さなGPUで大きなバッチを再現します

レッスン 2/413 ステップ

「大きなバッチのための勾配累積」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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 = 4

Do 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. 🧮

無料で開始

AI チューターと学ぶ Python — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
30
レッスン
120

よくある質問

「大きなバッチのための勾配累積」レッスンは無料ですか?

はい。「大きなバッチのための勾配累積」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。

「大きなバッチのための勾配累積」で何を学びますか?

小さなGPUで大きなバッチを再現します ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Deep Learning Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「大きなバッチのための勾配累積」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このDeep Learning Academyレッスンでコードを書いて実行できますか?

はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. autocastとGradScalerによる混合精度
  2. 大きなバッチのための勾配累積
  3. ボトルネックをプロファイリングする
  4. GPUメモリ使用量を削減する
← Deep Learning Academyに戻る