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.
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
If max_len is None, compute it as max(len(seq) for seq in seqs) (handle empty case).
Use np.full() to initialize the result, then copy each sequence.
Sign in to take notes on this problem
Accepts: array
Accepts: number
Accepts: any
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.
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
If max_len is None, compute it as max(len(seq) for seq in seqs) (handle empty case).
Use np.full() to initialize the result, then copy each sequence.
Sign in to take notes on this problem
Accepts: array
Accepts: number
Accepts: any