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, max_len = None
Output: [[1, 2, 0], [3, 4, 5], [6, 0, 0]]
Explanation: The longest sequence has length 3, so shorter sequences are padded on the right.
Input: seqs = [[1, 2, 3], [4]], pad_value = -1, max_len = None
Output: [[1, 2, 3], [4, -1, -1]]
When max_len is None, derive it from the longest sequence and use zero for an empty collection.
Initialize the output with np.full, then copy each truncated sequence into its row.
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, max_len = None
Output: [[1, 2, 0], [3, 4, 5], [6, 0, 0]]
Explanation: The longest sequence has length 3, so shorter sequences are padded on the right.
Input: seqs = [[1, 2, 3], [4]], pad_value = -1, max_len = None
Output: [[1, 2, 3], [4, -1, -1]]
When max_len is None, derive it from the longest sequence and use zero for an empty collection.
Initialize the output with np.full, then copy each truncated sequence into its row.
Sign in to take notes on this problem
Accepts: array
Accepts: number
Accepts: any