oneflow.nn.utils.rnn.pack_sequence(sequences: List[oneflow.Tensor], enforce_sorted: bool = True)oneflow.nn.utils.rnn.PackedSequence

Packs a list of variable length Tensors

Consecutive call of the next functions: pad_sequence, pack_padded_sequence.

sequences should be a list of Tensors of size L x *, where L is the length of a sequence and * is any number of trailing dimensions, including zero.

For unsorted sequences, use enforce_sorted = False. If enforce_sorted is True, the sequences should be sorted in the order of decreasing length. enforce_sorted = True is only necessary for ONNX export.

  • sequences (list[Tensor]) – A list of sequences of decreasing length.

  • enforce_sorted (bool, optional) – if True, checks that the input contains sequences sorted by length in a decreasing order. If False, this condition is not checked. Default: True.


a PackedSequence object

For example:

>>> from oneflow.nn.utils.rnn import pack_sequence
>>> import oneflow as flow

>>> a = flow.tensor([1,2,3])
>>> b = flow.tensor([4,5])
>>> c = flow.tensor([6])
>>> packed = pack_sequence([a, b, c])
tensor([1, 4, 6, 2, 5, 3], dtype=oneflow.int64)
>>> packed.batch_sizes
tensor([3, 2, 1], dtype=oneflow.int64)