.gradの読み取りとゼロ化
勾配が蓄積され、リセットが必要な理由を学びます
「.gradの読み取りとゼロ化」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
The .grad Attribute
After backward, every trained tensor stores its gradient in .grad. Reading it tells you how the loss responds to changes in that tensor.
print(weight.grad)Gradients Start as None
Before any backward call, .grad is None, not zero. PyTorch only allocates it once the first set of gradients actually arrives.
Gradients Accumulate
Here is the surprise: backward adds to .grad rather than replacing it. Call backward twice without clearing and the numbers pile up.
Why Accumulation Exists
This adding behavior is on purpose. It lets you sum gradients from several mini-batches before one update, handy for simulating a larger batch.
The Hidden Bug
Forget to clear and old gradients poison your next step, so the model trains on the wrong numbers. This silent bug trips up almost everyone once.
Zero Them Each Step
The fix is to reset gradients before each backward. With an optimizer you simply call zero_grad() at the top of every training step.
optimizer.zero_grad()The Right Order
The loop rhythm is fixed: zero_grad, forward, loss, backward, step. Zeroing first guarantees each step uses only this batch's gradients.
optimizer.zero_grad()
loss.backward()
optimizer.step()Clearing Without an Optimizer
No optimizer yet? You can null the gradients yourself by setting each tensor's .grad back to None before the next backward call.
w.grad = Noneset_to_none Is Cheaper
Newer code prefers zero_grad(set_to_none=True). Setting grads to None instead of filling zeros saves a little memory and time.
Reading Grads to Debug
Peeking at .grad is great for debugging. All zeros may mean a dead neuron, and huge values warn of exploding gradients before training blows up.
A Habit Worth Forming
Make zeroing automatic in your head. Every reliable training loop clears gradients first, so the model only ever learns from the current batch.
Quick Check
Recap
Gradients live in .grad and accumulate across backward calls, so you must clear them each step with zero_grad. Forgetting that quietly breaks training. 🧹
よくある質問
「.gradの読み取りとゼロ化」レッスンは無料ですか?
はい。「.gradの読み取りとゼロ化」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。
「.gradの読み取りとゼロ化」で何を学びますか?
勾配が蓄積され、リセットが必要な理由を学びます ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Deep Learning Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「.gradの読み取りとゼロ化」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDeep Learning Academyレッスンでコードを書いて実行できますか?
はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。