torch.nn.utils.unpack_sequence
function unpack_sequence(packed_sequence: PackedSequence): Tensor[]Unpack PackedSequence into a list of variable length Tensors.
This is an inverse of pack_sequence.
- Inverse of pack_sequence: This reverses the packing operation, recovering original variable-length sequences
- Removes padding: Unlike pad_packed_sequence, this removes padding and returns original lengths
- Preserves order: Sequences are returned in the same order as they were packed
Parameters
packed_sequencePackedSequence- The packed sequence to unpack into variable-length tensors
Returns
Tensor[]– Array of tensors, one per sequence, with padding removed and trimmed to original lengthsExamples
// Unpack a PackedSequence back into individual sequences
const sequences = [
torch.tensor([[1, 2], [3, 4], [5, 6]]), // length 3
torch.tensor([[7, 8], [9, 10]]), // length 2
];
const packed = torch.nn.utils.rnn.pack_sequence(sequences);
const unpacked = torch.nn.utils.rnn.unpack_sequence(packed);
// unpacked[0].shape: [3, 2]
// unpacked[1].shape: [2, 2]See Also
- PyTorch torch.nn.utils.rnn.unpack_sequence()
- pack_sequence - Pack variable-length sequences for RNN processing
- pad_packed_sequence - Unpack to padded tensor (keeps padding)