pad_sequence#
- ivy.pad_sequence(sequences, *, batch_first=False, padding_value=0)[source]#
Pad a list of variable-length sequences to the same length.
- Parameters:
sequences (
Iterable
[Union
[Array
,NativeArray
]]) – A list of input sequences (arrays) to pad and stack. Each sequence should have shape (seq_len, feature_size), where seq_len can vary between sequences.batch_first (
bool
, default:False
) – If True, the output tensor will have shape (batch_size, max_len, feature_size), where batch_size is the number of sequences and max_len is the length of the longest sequence. If False, the output tensor will have shape (max_len, batch_size, feature_size). Default is False.padding_value (
Union
[Iterable
[Tuple
[Number
]],Number
], default:0
) – The value to use for padding shorter sequences to the maximum length. This value is broadcasted to all padded areas of the sequences. Default is 0.
- Return type:
- Returns:
ret – Padded tensor with shape determined by batch_first. The length of each sequence is padded to match the length of the longest sequence in sequences.
Examples
With
ivy.Array
input:>>> seq1 = ivy.array([[1, 2], [3, 4]]) >>> seq2 = ivy.array([[5, 6]]) >>> y = ivy.pad_sequence([seq1, seq2], batch_first=True, padding_value=-1) >>> print(y) ivy.array([[[ 1, 2], [ 3, 4]], [[ 5, 6], [-1, -1]]])
>>> y = ivy.pad_sequence([seq1, seq2], batch_first=False, padding_value=0) >>> print(y) ivy.array([[[1, 2], [5, 6]], [[3, 4], [0, 0]]])
With
ivy.NativeArray
input:>>> seq1 = ivy.native_array([[1, 2], [3, 4]]) >>> seq2 = ivy.native_array([[5, 6]]) >>> y = ivy.pad_sequence([seq1, seq2], batch_first=True) >>> print(y) ivy.array([[[1, 2], [3, 4]], [[5, 6], [0, 0]]])