0Pricing
Deep Learning Academy · درس

‏torch.no_grad() للاستدلال

تجاوز تتبّع الرسم لتوفير الذاكرة

‏torch.no_grad() للاستدلال درس مجاني في Deep Learning Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Deep Learning Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Not Every Pass Needs Grads

You only need gradients while training. When you just want predictions, tracking the graph is wasted work, so PyTorch lets you turn it off. 🛑

Meet torch.no_grad

Wrap code in a torch.no_grad() block and autograd stops recording inside it. No graph is built, and no gradients are stored.

with torch.no_grad():
    preds = model(x)

Why Skip the Graph

Building the graph costs memory to remember every step for a backward pass that will never come. During inference that overhead is pure waste.

Faster and Lighter

Inside no_grad your forward pass uses less memory and runs a little faster. On big models this lets you fit larger batches when only predicting.

Outputs Are Detached

Tensors created inside the block have requires_grad false. They are plain results you can print, save, or turn into NumPy without complaint.

Use It for Evaluation

Validation and test loops should always run under no_grad. You are scoring the model, not training it, so there is no reason to track gradients.

with torch.no_grad():
    for x, y in val_loader:
        out = model(x)

Pair It With eval Mode

For inference, set model.eval() and wrap calls in no_grad together. eval fixes layers like dropout; no_grad stops the graph. They solve different problems.

It Is a Context Manager

no_grad only affects code inside the with block. Once you leave it, autograd switches tracking back on automatically for your next training step.

The decorator form

You can also tag a whole function with @torch.no_grad(). Every tensor op inside that function then runs without building a graph.

@torch.no_grad()
def predict(x):
    return model(x)

Detach for a Single Tensor

Need to free just one tensor from the graph instead of a whole block? Call .detach() on it to get a copy that carries no gradient history.

frozen = output.detach()

A Safe, Common Habit

Reach for no_grad any time you are not learning: inference, metrics, or saving outputs. It is a tiny change that quietly saves memory and speed.

Quick Check

Confirm when to use no_grad.

Recap

Wrap inference in torch.no_grad() to skip graph building, saving memory and time. Pair it with model.eval() whenever you predict instead of train. 🚀

الأسئلة الشائعة

هل درس «‏torch.no_grad() للاستدلال» مجاني؟

نعم — نص درس «‏torch.no_grad() للاستدلال» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Deep Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.

ماذا ستتعلم في «‏torch.no_grad() للاستدلال»؟

تجاوز تتبّع الرسم لتوفير الذاكرة تتمرن على Deep Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Deep Learning Academy؟

لا تُشترط خبرة سابقة. Deep Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «‏torch.no_grad() للاستدلال»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Deep Learning Academy هذا؟

نعم. كل درس في Deep Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. requires_grad ورسم الحساب
  2. استدعاء backward() للحصول على التدرجات
  3. قراءة ‎.grad‎ وتصفيره
  4. ‏torch.no_grad() للاستدلال
← العودة إلى Deep Learning Academy