التجميع والخلط وnum_workers
اضبط DataLoader لزيادة السرعة
التجميع والخلط وnum_workers درس مجاني في Deep Learning Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Deep Learning Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Meet the DataLoader
A dataset hands over one sample at a time, but training wants groups. The DataLoader wraps your dataset and serves it in convenient batches. 📦
from torch.utils.data import DataLoader
loader = DataLoader(ds)Batching Saves Time
Set batch_size and the loader stacks that many samples into one tensor. Bigger batches use your hardware better and smooth out noisy updates.
loader = DataLoader(ds, batch_size=32)One Batch, Stacked Together
Each batch adds a new first dimension. Thirty-two samples of shape 784 become a single tensor shaped 32 by 784, ready for the model.
Loop Over Batches
You iterate the loader like any Python sequence. Each turn of the loop yields one batch of inputs and labels for your training step.
for xb, yb in loader:
pred = model(xb)Shuffle Every Epoch
Setting shuffle to True reorders samples each epoch. This breaks accidental ordering so the model cannot memorize the sequence of your data.
loader = DataLoader(ds, batch_size=32, shuffle=True)Shuffle Train, Not Test
Turn shuffling on for the training set but off for validation and test. Evaluation just measures performance, so a stable order is fine there.
num_workers Loads in Parallel
Reading and decoding data can stall the GPU. Setting num_workers above zero spawns helper processes that prepare the next batch while the model trains.
loader = DataLoader(ds, batch_size=32, num_workers=4)Pick a Sensible Worker Count
A common start for num_workers is the number of CPU cores you have. Too many can thrash memory, so measure rather than guess blindly.
pin_memory Speeds GPU Copies
When training on a GPU, set pin_memory to True. It places batches in page-locked memory so transfers to the device run noticeably faster.
loader = DataLoader(ds, batch_size=32, pin_memory=True)Handle the Last Batch
The final batch is often smaller than the rest. Use drop_last True to discard it when your model needs every batch the same size.
loader = DataLoader(ds, batch_size=32, drop_last=True)One Loader Per Split
In practice you build a separate loader for train, validation, and test. Each gets its own settings, like shuffle on only for training.
Quick Check
What does setting num_workers above zero actually do?
Recap
A DataLoader batches your dataset, shuffles training data, and uses num_workers to load batches in parallel. It keeps your model fed and fast. 🎉
تعلم Python مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «التجميع والخلط وnum_workers» مجاني؟
نعم — نص درس «التجميع والخلط وnum_workers» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Deep Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
ماذا ستتعلم في «التجميع والخلط وnum_workers»؟
اضبط DataLoader لزيادة السرعة تتمرن على Deep Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Deep Learning Academy؟
لا تُشترط خبرة سابقة. Deep Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «التجميع والخلط وnum_workers»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Deep Learning Academy هذا؟
نعم. كل درس في Deep Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- كتابة فئة Dataset مخصّصة
- التجميع والخلط وnum_workers
- collate_fn للمدخلات متفاوتة الطول
- تطبيع المدخلات وتوحيدها