backward()を呼び出して勾配を取得する
1行で.gradを埋めます
「backward()を呼び出して勾配を取得する」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
One Line to Differentiate
Once your graph is built, a single call does all the calculus. Run backward() on your output and gradients flow back to every input.
Start From a Scalar
You call backward on the final number you want to minimize, usually the loss. It must be a single scalar so autograd knows where to start.
y = x ** 2
y.backward()Gradients Land in .grad
After backward runs, each leaf tensor's .grad holds the derivative of the output with respect to that tensor. The math is already done.
print(x.grad)A Quick Check by Hand
For y equal to x squared, calculus says the derivative is 2x. At x equal to 2 that is 4, and PyTorch fills x.grad with exactly 4.0.
Backward Walks the Graph
backward() travels the graph from output to inputs, applying the chain rule at every node. You wrote only the forward math and got all of this free.
Gradients for Many Inputs
If your output depends on several tracked tensors, one backward call fills the .grad of each of them at once. That is how whole models update.
loss = (a * b + c).sum()
loss.backward()The Graph Is Spent
By default the graph is freed after backward to save memory, so calling it twice errors. You build a fresh graph on the next forward pass.
Keep It If You Must
Need to call backward again on the same graph? Pass retain_graph equals true. You rarely need this, so skip it unless an error tells you to.
loss.backward(retain_graph=True)Non-Scalar Outputs
If your output is a vector, backward needs a gradient argument saying how to weight each element. Most of the time you sum to a scalar instead.
This Is the Backward Pass
That single backward call is the famous backward pass of training. Forward computes predictions, backward computes how to fix the weights.
From Gradients to Learning
The numbers in .grad tell each weight which way to move. An optimizer reads them and nudges the weights to lower the loss a little.
Quick Check
Recap
Call backward() on a scalar loss and autograd applies the chain rule across the graph, filling each tensor's .grad with its derivative. That is the backward pass. 🔁
AI チューターと学ぶ Python — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 30
- レッスン
- 120
よくある質問
「backward()を呼び出して勾配を取得する」レッスンは無料ですか?
はい。「backward()を呼び出して勾配を取得する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。
「backward()を呼び出して勾配を取得する」で何を学びますか?
1行で.gradを埋めます ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Deep Learning Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「backward()を呼び出して勾配を取得する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDeep Learning Academyレッスンでコードを書いて実行できますか?
はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- requires_gradと計算グラフ
- backward()を呼び出して勾配を取得する
- .gradの読み取りとゼロ化
- 推論のためのtorch.no_grad()