Mit backward() Gradienten berechnen
Füllen Sie .grad mit einer einzigen Zeile.
Mit backward() Gradienten berechnen 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.
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. 🔁
Häufig gestellte Fragen
Ist die Lektion „Mit backward() Gradienten berechnen“ kostenlos?
Ja — der vollständige Text von „Mit backward() Gradienten berechnen“ 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 „Mit backward() Gradienten berechnen“?
Füllen Sie .grad mit einer einzigen Zeile. 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 „Mit backward() Gradienten berechnen“?
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
- requires_grad und der Berechnungsgraph
- Mit backward() Gradienten berechnen
- .grad auslesen und auf null setzen
- torch.no_grad() für Inferenz