collate_fn للمدخلات متفاوتة الطول
أضف الحشو واجمع العينات غير المنتظمة
collate_fn للمدخلات متفاوتة الطول درس مجاني في Deep Learning Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Deep Learning Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
When Samples Don't Match
Stacking into a batch needs every sample the same shape. But sentences and audio clips have different lengths, so the default collate step fails. 🧩
What collate_fn Does
The DataLoader gathers a list of samples and passes them to collate_fn, which merges them into one batch. By default it simply stacks tensors.
Ragged Inputs Break Stacking
Try to stack a length-5 and a length-8 sequence and PyTorch raises a shape error. Ragged lengths are exactly the case a custom collate must handle.
Write Your Own collate_fn
You pass a function to the DataLoader's collate_fn argument. It receives a list of samples and returns whatever batch shape your model expects.
loader = DataLoader(ds, batch_size=4, collate_fn=my_collate)Step One: Split the List
Inside your function, unzip the list of pairs into separate sequences and labels. Now you can treat each group on its own before merging.
def my_collate(batch):
seqs, labels = zip(*batch)Pad to the Longest
The trick for variable lengths is padding: extend every sequence to the longest one with a filler value, so they finally share a shape.
pad_sequence Does It for You
PyTorch ships pad_sequence, which pads a list of tensors to equal length and stacks them. Set batch_first so the batch dimension comes first.
from torch.nn.utils.rnn import pad_sequence
padded = pad_sequence(seqs, batch_first=True)Remember the Real Lengths
Padding adds fake tokens, so also return each sequence's true length. Your model uses these to ignore the padded positions during the forward pass.
lengths = torch.tensor([len(s) for s in seqs])Stack the Labels
Labels are usually fixed size, so a normal stack works for them. Return the padded inputs, the lengths, and the stacked labels together.
labels = torch.stack(labels)
return padded, lengths, labelsMask Out the Padding
Later you build a mask from the lengths so the loss and attention skip padded slots. Padding fills shape without polluting the gradients.
One Function, Any Shape
With a custom collate_fn, the same DataLoader handles text, audio, and graphs. You control exactly how loose samples become one tidy batch.
Quick Check
Why do variable-length sequences need a custom collate_fn?
Recap
A custom collate_fn turns a list of uneven samples into one batch, usually by padding sequences to equal length and tracking their real sizes. 🎉
الأسئلة الشائعة
هل درس «collate_fn للمدخلات متفاوتة الطول» مجاني؟
نعم — نص درس «collate_fn للمدخلات متفاوتة الطول» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Deep Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
ماذا ستتعلم في «collate_fn للمدخلات متفاوتة الطول»؟
أضف الحشو واجمع العينات غير المنتظمة تتمرن على Deep Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Deep Learning Academy؟
لا تُشترط خبرة سابقة. Deep Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «collate_fn للمدخلات متفاوتة الطول»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Deep Learning Academy هذا؟
نعم. كل درس في Deep Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- كتابة فئة Dataset مخصّصة
- التجميع والخلط وnum_workers
- collate_fn للمدخلات متفاوتة الطول
- تطبيع المدخلات وتوحيدها