torch.nn.utils.pad_sequence
function pad_sequence(sequences: Tensor[], options?: PadSequenceOptions): Tensorfunction pad_sequence(sequences: Tensor[], batch_first: boolean, padding_value: number, padding_side: 'right' | 'left', options?: PadSequenceOptions): TensorPad a list of variable length Tensors with padding_value.
pad_sequence stacks a list of Tensors along a new dimension, and pads them to equal length.
Parameters
sequencesTensor[]- List of variable length sequences
optionsPadSequenceOptionsoptional- Optional settings for padding
Returns
Tensor– Padded tensorExamples
const a = torch.tensor([1, 2, 3]);
const b = torch.tensor([4, 5]);
const c = torch.tensor([6]);
const padded = torch.nn.utils.rnn.pad_sequence([a, b, c], { batch_first: true });
// padded shape: [3, 3]