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