Deep Learning Academy · درس

الدقة المختلطة باستخدام autocast وGradScaler

حسابات بنصف الدقة لتسريع كبير

الدرس 1 من 413 خطوة

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

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

What Mixed Precision Means

By default PyTorch does math in 32-bit floats. Mixed precision runs many operations in 16-bit instead, which is faster and uses far less memory.

Why Half Precision Is Faster

Modern GPUs have special tensor cores tuned for 16-bit math. Feeding them half-precision data can speed up training two or three times with little accuracy loss.

The Catch With 16-Bit

Half precision has a tiny range, so very small gradient values can round down to zero. That silent underflow stalls learning if you do nothing about it.

Meet autocast

Wrap your forward pass in autocast and PyTorch picks a safe precision per operation automatically. You never cast tensors by hand.

with torch.autocast(device_type='cuda'):
    out = model(x)

What autocast Wraps

Put only the forward pass and loss inside autocast. The backward call stays outside the block, where PyTorch handles precision for you.

with torch.autocast(device_type='cuda'):
    out = model(x)
    loss = loss_fn(out, y)

Meet GradScaler

A GradScaler fixes underflow by multiplying the loss by a large factor before backward, so small gradients stay big enough to survive in 16-bit.

scaler = torch.cuda.amp.GradScaler()

Scale the Loss

Instead of loss.backward(), call scaler.scale(loss).backward(). The scaler inflates the loss first so the resulting gradients do not vanish.

scaler.scale(loss).backward()

Step Through the Scaler

Run the optimizer with scaler.step, which quietly unscales the gradients back to normal size before applying the update.

scaler.step(optimizer)

Update the Scale Factor

Finish each step with scaler.update(). It grows the scale when things are stable and shrinks it if it ever spots an overflow.

scaler.update()

The Full AMP Step

Together these calls form one clean AMP iteration: zero grads, autocast forward, scaled backward, scaler step, then update.

optimizer.zero_grad()
with torch.autocast(device_type='cuda'):
    loss = loss_fn(model(x), y)
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()

When To Reach For It

Mixed precision shines on a recent NVIDIA GPU with large batches. On a plain CPU it gives little benefit, so save it for real training runs.

Quick Check

You enabled autocast but tiny gradients keep vanishing. What tool fixes that?

Recap

Wrap the forward in autocast for fast 16-bit math, then use a GradScaler to scale, step, and update so tiny gradients survive. ⚡

البدء مجانًا

تعلم Python مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
30
الدروس
120

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

هل درس «الدقة المختلطة باستخدام autocast وGradScaler» مجاني؟

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

ماذا ستتعلم في «الدقة المختلطة باستخدام autocast وGradScaler»؟

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

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

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

كم من الوقت يستغرق درس «الدقة المختلطة باستخدام autocast وGradScaler»؟

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

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

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

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

  1. الدقة المختلطة باستخدام autocast وGradScaler
  2. تجميع التدرجات للدفعات الكبيرة
  3. تحليل عنق الزجاجة
  4. تقليل استخدام ذاكرة GPU
← العودة إلى Deep Learning Academy