Pad and Collate Variable-Length Batches

easy
Unit 6 · Augmentation0/3 to milestone

Sequences in a batch have different lengths, but tensors must be rectangular. A collate function pads them and builds the attention mask so the model ignores padding.

Implement collate(seqs, pad_id=0, max_len=None, side="right"):

  • Truncate any sequence longer than max_len (keep the first tokens). Without max_len, pad to the longest sequence.
  • Pad on the right, or on the left when side="left" (decoder models generating text need the real tokens to end at the same position).
  • Return a dict of NumPy integer arrays: input_ids, attention_mask (1 for real tokens) and position_ids, which count real tokens from 0 and are 0 at padding positions.

Then implement padding_waste(seqs): the fraction of the padded batch made of padding, which is what sorting by length (bucketing) reduces.

#data-loading#nlp#batching2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Pad and Collate Variable-Length Batches · TechGradify