These two functions help you pack (already) padded sequences and pad (already) packed sequences. I think the purpose is not to help you pad sequences, but to save some spaces. Check out the following example:

import itertools
import torch
import torch.nn as nn
import torch.nn.utils.rnn as rnn_utils
from torch.autograd import Variable, gradcheck
def test_pack_padded_sequence():
def pad(tensor, length):
return torch.cat([tensor, tensor.new(length - tensor.size(0), *tensor.size()[1:]).zero_()])
lengths = [2,1]#[10, 8, 4, 2, 2, 2, 1]
max_length = lengths[0]
batch_sizes = [sum(map(bool, filter(lambda x: x >= i, lengths))) for i in range(1, max_length + 1)]
offset = 0
padded = torch.cat([pad(i * 100 + torch.range(1, 5 * l).view(l, 1, 5), max_length) for i, l in enumerate(lengths, 1)], 1)
padded = Variable(padded, requires_grad=True)
expected_data = [[torch.range(1, 5) + i * 100 for i in range(batch_size)] for batch_size in batch_sizes]
expected_data = list(itertools.chain.from_iterable(expected_data))
expected_data = torch.cat(expected_data)
for batch_first in (True, False):
src = padded
if batch_first:
src = src.transpose(0, 1)
print(“SRCSRCSRC”)
print(src)
print(“LENGTHS”)
print(lengths)
packed = rnn_utils.pack_padded_sequence(src, lengths, batch_first=batch_first)
print(“PACKED”)
print(packed)
unpacked, unpacked_len = rnn_utils.pad_packed_sequence(packed, batch_first=batch_first)
print(“UNPACKED”)
print(unpacked)
print(“UNPACKED_LEN”)
print(unpacked_len)
test_pack_padded_sequence()