استدعاء backward() للحصول على التدرجات
املأ .grad بسطر واحد
استدعاء backward() للحصول على التدرجات درس مجاني في Deep Learning Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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. 🔁
تعلم Python مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «استدعاء backward() للحصول على التدرجات» مجاني؟
نعم — نص درس «استدعاء backward() للحصول على التدرجات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Deep Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
ماذا ستتعلم في «استدعاء backward() للحصول على التدرجات»؟
املأ .grad بسطر واحد تتمرن على Deep Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Deep Learning Academy؟
لا تُشترط خبرة سابقة. Deep Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «استدعاء backward() للحصول على التدرجات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Deep Learning Academy هذا؟
نعم. كل درس في Deep Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- requires_grad ورسم الحساب
- استدعاء backward() للحصول على التدرجات
- قراءة .grad وتصفيره
- torch.no_grad() للاستدلال