Pad Sequences
Pad Sequences
In NLP, batches often need sequences of equal length. Given a list of token ID sequences (lists of ints), pad them with a special pad_value to match the length of the longest sequence.
Examples
Input: seqs = [[1,2,3], [4,5], [6]], pad_value=0
Output: [[1,2,3], [4,5,0], [6,0,0]]
max_len = 3 (auto-detected)
Input: seqs = [[1,2,3,4], [5,6]], pad_value=-1, max_len=3
Output: [[1,2,3], [5,6,-1]]
First sequence truncated
Hint 1
If max_len is None, compute it as max(len(seq) for seq in seqs) (handle empty case).
Hint 2
Use np.full() to initialize the result, then copy each sequence.
Requirements
- If max_len is None, use the maximum length among sequences
- If some sequences are shorter, pad them at the end with pad_value
- If some sequences are longer than max_len, truncate them at the end
- If seqs is empty, return an array of shape (0, 0)
- Output must be a NumPy array of dtype int
Constraints
- Number of sequences N ≤ 10,000
- Each sequence length ≤ 1,000
- NumPy required
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: number
Accepts: any
Pad Sequences
Pad Sequences
In NLP, batches often need sequences of equal length. Given a list of token ID sequences (lists of ints), pad them with a special pad_value to match the length of the longest sequence.
Examples
Input: seqs = [[1,2,3], [4,5], [6]], pad_value=0
Output: [[1,2,3], [4,5,0], [6,0,0]]
max_len = 3 (auto-detected)
Input: seqs = [[1,2,3,4], [5,6]], pad_value=-1, max_len=3
Output: [[1,2,3], [5,6,-1]]
First sequence truncated
Hint 1
If max_len is None, compute it as max(len(seq) for seq in seqs) (handle empty case).
Hint 2
Use np.full() to initialize the result, then copy each sequence.
Requirements
- If max_len is None, use the maximum length among sequences
- If some sequences are shorter, pad them at the end with pad_value
- If some sequences are longer than max_len, truncate them at the end
- If seqs is empty, return an array of shape (0, 0)
- Output must be a NumPy array of dtype int
Constraints
- Number of sequences N ≤ 10,000
- Each sequence length ≤ 1,000
- NumPy required
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: number
Accepts: any