Deep Learning Academy · レッスン

autocastとGradScalerによる混合精度

半精度計算で大幅に高速化します

レッスン 1/413 ステップ

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

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

What Mixed Precision Means

By default PyTorch does math in 32-bit floats. Mixed precision runs many operations in 16-bit instead, which is faster and uses far less memory.

Why Half Precision Is Faster

Modern GPUs have special tensor cores tuned for 16-bit math. Feeding them half-precision data can speed up training two or three times with little accuracy loss.

The Catch With 16-Bit

Half precision has a tiny range, so very small gradient values can round down to zero. That silent underflow stalls learning if you do nothing about it.

Meet autocast

Wrap your forward pass in autocast and PyTorch picks a safe precision per operation automatically. You never cast tensors by hand.

with torch.autocast(device_type='cuda'):
    out = model(x)

What autocast Wraps

Put only the forward pass and loss inside autocast. The backward call stays outside the block, where PyTorch handles precision for you.

with torch.autocast(device_type='cuda'):
    out = model(x)
    loss = loss_fn(out, y)

Meet GradScaler

A GradScaler fixes underflow by multiplying the loss by a large factor before backward, so small gradients stay big enough to survive in 16-bit.

scaler = torch.cuda.amp.GradScaler()

Scale the Loss

Instead of loss.backward(), call scaler.scale(loss).backward(). The scaler inflates the loss first so the resulting gradients do not vanish.

scaler.scale(loss).backward()

Step Through the Scaler

Run the optimizer with scaler.step, which quietly unscales the gradients back to normal size before applying the update.

scaler.step(optimizer)

Update the Scale Factor

Finish each step with scaler.update(). It grows the scale when things are stable and shrinks it if it ever spots an overflow.

scaler.update()

The Full AMP Step

Together these calls form one clean AMP iteration: zero grads, autocast forward, scaled backward, scaler step, then update.

optimizer.zero_grad()
with torch.autocast(device_type='cuda'):
    loss = loss_fn(model(x), y)
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()

When To Reach For It

Mixed precision shines on a recent NVIDIA GPU with large batches. On a plain CPU it gives little benefit, so save it for real training runs.

Quick Check

You enabled autocast but tiny gradients keep vanishing. What tool fixes that?

Recap

Wrap the forward in autocast for fast 16-bit math, then use a GradScaler to scale, step, and update so tiny gradients survive. ⚡

無料で開始

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

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

コース
30
レッスン
120

よくある質問

「autocastとGradScalerによる混合精度」レッスンは無料ですか?

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

「autocastとGradScalerによる混合精度」で何を学びますか?

半精度計算で大幅に高速化します ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「autocastとGradScalerによる混合精度」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

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