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_sequenceunstacks 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
- Returns
a list of
Tensorobjects- Return type
On this page