تجميع التدرجات للدفعات الكبيرة
حاكِ دفعات كبيرة على وحدات GPU صغيرة
تجميع التدرجات للدفعات الكبيرة درس مجاني في Deep Learning Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Deep Learning Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The Big-Batch Problem
Large batches often train more smoothly, but they also need lots of GPU memory. A small card simply cannot hold a giant batch at once.
The Core Trick
Gradient accumulation splits one big batch into small chunks. You add up their gradients and update once, as if the whole batch ran together.
Gradients Already Accumulate
PyTorch adds each backward pass into .grad rather than replacing it. This default behavior is exactly what accumulation relies on.
Pick an Accumulation Count
Choose how many mini-batches make one update. With accum_steps of four, four small batches behave like one batch four times larger.
accum_steps = 4Do Not Zero Every Step
The key change is timing: zero_grad only at the start of an accumulation cycle, not after every single mini-batch.
if step % accum_steps == 0:
optimizer.zero_grad()Scale the Loss
Divide each mini-batch loss by accum_steps before backward. This keeps the average gradient identical to running the full batch at once.
loss = loss_fn(model(x), y) / accum_steps
loss.backward()Step Only When Full
After enough mini-batches pile up, call optimizer.step. The accumulated gradients now reflect the whole large batch.
if (step + 1) % accum_steps == 0:
optimizer.step()The Full Pattern
Put it together: scale the loss, backward every step, but only step and zero once per cycle. The loop stays simple.
for step, (x, y) in enumerate(loader):
loss = loss_fn(model(x), y) / accum_steps
loss.backward()
if (step + 1) % accum_steps == 0:
optimizer.step()
optimizer.zero_grad()Memory Stays Small
You only ever hold one mini-batch in memory at a time. That is why a modest GPU can mimic a batch many times its real capacity.
The Trade-Off
Accumulation trades time for memory: more forward and backward passes per update mean each effective batch takes a little longer to finish.
Mind Batch Norm
Batch norm still sees only the small mini-batch, so its statistics are noisier than a true large batch would produce. Keep that in mind.
Quick Check
You accumulate over 4 mini-batches. When should you call optimizer.step()?
Recap
Split a big batch into chunks, divide the loss by accum_steps, backward every chunk, and step only once per cycle to fake a large batch on small memory. 🧮
الأسئلة الشائعة
هل درس «تجميع التدرجات للدفعات الكبيرة» مجاني؟
نعم — نص درس «تجميع التدرجات للدفعات الكبيرة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Deep Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
ماذا ستتعلم في «تجميع التدرجات للدفعات الكبيرة»؟
حاكِ دفعات كبيرة على وحدات GPU صغيرة تتمرن على Deep Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Deep Learning Academy؟
لا تُشترط خبرة سابقة. Deep Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «تجميع التدرجات للدفعات الكبيرة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Deep Learning Academy هذا؟
نعم. كل درس في Deep Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الدقة المختلطة باستخدام autocast وGradScaler
- تجميع التدرجات للدفعات الكبيرة
- تحليل عنق الزجاجة
- تقليل استخدام ذاكرة GPU