وضعا Train وEval
لماذا يختلف model.train() عن model.eval()
وضعا Train وEval درس مجاني في Deep Learning Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Deep Learning Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
A Model Has Two Faces
Your network behaves differently while learning than while being tested. PyTorch calls these two states train mode and eval mode. 🎭
Flip Into Train Mode
Before training you call model.train. This tells every layer to behave the way it should while the model is actively learning from data.
model.train()Flip Into Eval Mode
Before validating or predicting you call model.eval. Now the network switches to its steady, deterministic behavior for honest measurement.
model.eval()Dropout Cares
Dropout randomly zeros neurons during training to prevent overfitting. In eval mode it turns fully off so every neuron contributes to the prediction.
Batch Norm Cares Too
Batch norm uses each batch's statistics while training but switches to stored running averages at eval time for stable, consistent outputs.
It Is Only a Switch
These calls do not train or test anything by themselves. They simply flip a flag that tells dropout and norm layers which behavior to use.
Eval Is Not no_grad
A common mix-up: eval changes layer behavior, while no_grad stops gradient tracking. They solve different problems, so you usually use both together.
model.eval()
with torch.no_grad():
pred = model(x)Forgetting eval Hurts
If you validate without eval, dropout and batch norm keep their training behavior. Your scores turn noisy and look worse than the model really is.
Forgetting train Hurts
The reverse bites too: leave the model in eval and dropout never activates, so its regularization silently vanishes during training.
Build It Into the Loop
Make the switch a habit: set train before the training pass and eval before validation, every single epoch, so the model never stays in the wrong mode.
for epoch in range(epochs):
model.train()
# ... train ...
model.eval()
# ... validate ...Small Call, Big Effect
Two tiny calls, yet they decide whether your metrics are trustworthy. Treat the mode switch as a non-negotiable part of every loop.
Quick Check
What does calling model.eval() actually change?
Recap
You learned the two modes: train activates dropout and batch stats, eval makes them deterministic. Switch every epoch and pair eval with no_grad. 🎉
تعلم Python مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «وضعا Train وEval» مجاني؟
نعم — نص درس «وضعا Train وEval» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Deep Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
ماذا ستتعلم في «وضعا Train وEval»؟
لماذا يختلف model.train() عن model.eval() تتمرن على Deep Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Deep Learning Academy؟
لا تُشترط خبرة سابقة. Deep Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «وضعا Train وEval»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Deep Learning Academy هذا؟
نعم. كل درس في Deep Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- المرور الأمامي والخسارة والمرور العكسي والخطوة
- كتابة حلقة تدريب مصغّرة
- تتبّع الدقة أثناء التدريب
- وضعا Train وEval