Rate this Page

torch.nn.utils.rnn.unpad_sequence#

torch.nn.utils.rnn.unpad_sequence(padded_sequences,lengths,batch_first=False)[source]#

Unpad padded Tensor into a list of variable length Tensors.

unpad_sequence unstacks padded Tensor into a list of variable length Tensors.

Example

>>>fromtorch.nn.utils.rnnimportpad_sequence,unpad_sequence>>>a=torch.ones(25,300)>>>b=torch.ones(22,300)>>>c=torch.ones(15,300)>>>sequences=[a,b,c]>>>padded_sequences=pad_sequence(sequences)>>>lengths=torch.as_tensor([v.size(0)forvinsequences])>>>unpadded_sequences=unpad_sequence(padded_sequences,lengths)>>>torch.allclose(sequences[0],unpadded_sequences[0])True>>>torch.allclose(sequences[1],unpadded_sequences[1])True>>>torch.allclose(sequences[2],unpadded_sequences[2])True
Parameters
  • padded_sequences (Tensor) – padded sequences.

  • lengths (Tensor) – length of original (unpadded) sequences.

  • batch_first (bool,optional) – whether batch dimension first or not. Default:False.

Returns

a list ofTensor objects

Return type

list[torch.Tensor]