Function torch::nn::utils::rnn::pack_padded_sequence¶
Defined in File rnn.h
Function Documentation¶
-
inline PackedSequence torch::nn::utils::rnn::pack_padded_sequence(Tensor input, Tensor lengths, bool batch_first = false, bool enforce_sorted = true)¶
Packs a Tensor containing padded sequences of variable length.
input
can be of sizeT x B x *
whereT
is the length of the longest sequence (equal tolengths[0]
),B
is the batch size, and*
is any number of dimensions (including 0). Ifbatch_first
istrue
,B x T x *
input
is expected.For unsorted sequences, use
enforce_sorted = false
. Ifenforce_sorted
istrue
, the sequences should be sorted by length in a decreasing order, i.e.input[:,0]
should be the longest sequence, andinput[:,B-1]
the shortest one.Note: 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
PackedSequence
object by calling its.data()
function.Arguments: 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 inB x T x *
format. Default:false
. enforce_sorted (bool, optional): iftrue
, the input is expected to contain sequences sorted by length in a decreasing order. Iffalse
, this condition is not checked. Default:true
.Returns: a
PackedSequence
object