torch.nn.utils.rnn.
pack_padded_sequence()的用法:
targets = pack_padded_sequence(captions, lengths, batch_first=True)[0]
输入的caption是长度不同的句子 返回的targets是打包成一条后的
captions
tensor([[ 1, 8, 1704, 47, 12, 56, 158, 3, 11, 344, 6, 20,
143, 151, 5, 2],
[ 1, 4, 3600, 9, 4, 332, 706, 10, 4, 304, 9, 8,
162, 5, 2, 0],
[ 1, 4, 36, 7, 21, 19, 109, 4, 1060, 664, 5, 2,
0, 0, 0, 0],
[ 1, 4, 13, 48, 4, 950, 10, 190, 31, 5, 2, 0,
0, 0, 0, 0],
[ 1, 4, 13, 84, 4, 157, 6, 4, 562, 78, 2, 0,
0, 0, 0, 0],
[ 1, 4, 53, 15, 6, 4, 62, 133, 5, 2, 0, 0,
0, 0, 0, 0]], device='cuda:0')
captions.shape
torch.Size([6, 16])
targets
tensor([ 1, 1, 1, 1, 1, 1, 8, 4, 4, 4, 4, 4,
1704, 3600, 36, 13, 13, 53, 47, 9, 7, 48, 84, 15,
12, 4, 21, 4, 4, 6, 56, 332, 19, 950, 157, 4,
158, 706, 109, 10, 6, 62, 3, 10, 4, 190, 4, 133,
11, 4, 1060, 31, 562, 5, 344, 304, 664, 5, 78, 2,
6, 9, 5, 2, 2, 20, 8, 2, 143, 162, 151, 5,
5, 2, 2], device='cuda:0')
targets.shape
torch.Size([75])