pack_padded_sequence(input: Tensor, lengths: Tensor, batch_first: bool = False, enforce_sorted: bool = True) → PackedSequence¶
Packs a Tensor containing padded sequences of variable length.
inputcan be of size
T x B x *where T is the length of the longest sequence (equal to
Bis the batch size, and
*is any number of dimensions (including 0). If
B x T x *
For unsorted sequences, use enforce_sorted = False. If
True, the sequences should be sorted by length in a decreasing order, i.e.
input[:,0]should be the longest sequence, and
input[:,B-1]the shortest one. enforce_sorted = True is only necessary for ONNX export.
This function accepts any input that has at least two dimensions. You can apply it to pack the labels, and use the output of the RNN with them to compute the loss directly. A Tensor can be retrieved from a
PackedSequenceobject by accessing its
input (Tensor) – padded batch of variable length sequences.
lengths (Tensor) – list of sequences lengths of each batch element.
batch_first (bool, optional) – if
True, the input is expected in
B x T x *format.
enforce_sorted (bool, optional) – if
True, the input is expected to contain sequences sorted by length in a decreasing order. If
False, the input will get sorted unconditionally. Default: