Neural Mastery
← Practice
Vectorized Batch Collator with Sequence Paddingmedium

Vectorized Batch Collator with Sequence Padding

medium NumPy & Data Wrangling 15-20 min

NumPy allowed · plain Python earns 10 bonus points

Linear algebra step 1/4

A vector transforms

Matrix multiplication is the first computation behind every model.
Open lesson →
01vector
02matrix
03transform
04result
[2, −1] × [[1, 2], [−.5, 1]] = [0, −2]

Computed matrix × vector transformation

Collate variable-length token sequences into a uniform dense matrix with binary attention masks.

Related Skills

Task

Implement collate_padded_batch(sequences, pad_val): determine max_len = max(len(s)) across input sequences. Pad shorter sequences on the right with pad_val. Concurrently create attention_mask where original tokens are 1 and padding tokens are 0. Return a dict {"padded_batch": ..., "attention_mask": ...}.

Function Signature

collate_padded_batch(sequences: list[list[int]], pad_val: int) -> dict[str, list]

Examples

Example 1: Varying 1D Sequences
Input: sequences = [[1, 2], [3, 4, 5, 6], [7]] pad_val = 0
Output: {'padded_batch': [[1, 2, 0, 0], [3, 4, 5, 6], [7, 0, 0, 0]], 'attention_mask': [[1, 1, 0, 0], [1, 1, 1, 1], [1, 0, 0, 0]]}
Explanation: Collate varying length token lists.
Example 2: Uniform Length
Input: sequences = [[10, 20], [30, 40]] pad_val = -1
Output: {'padded_batch': [[10, 20], [30, 40]], 'attention_mask': [[1, 1], [1, 1]]}
Explanation: No padding needed for uniform length.

Constraints

  • 0 <= len(s) <= 2048
  • 1 <= len(sequences) <= 256
Python3Saved ✓
def collate_padded_batch(sequences: list[list[int]], pad_val: int) -> dict[str, list]:
"""Pad sequences to max length and produce boolean attention mask.
Return {'padded_batch': list[list[int]], 'attention_mask': list[list[int]]}."""
# Implement solution logic here
pass

Case 1: Varying 1D Sequences
Input: sequences = [[1, 2], [3, 4, 5, 6], [7]] pad_val = 0
Expected: {'padded_batch': [[1, 2, 0, 0], [3, 4, 5, 6], [7, 0, 0, 0]], 'attention_mask': [[1, 1, 0, 0], [1, 1, 1, 1], [1, 0, 0, 0]]}
Case 2: Uniform Length
Input: sequences = [[10, 20], [30, 40]] pad_val = -1
Expected: {'padded_batch': [[10, 20], [30, 40]], 'attention_mask': [[1, 1], [1, 1]]}