Skip to content

RNN sequence padding with batch_first #1176

@MaximumEntropy

Description

@MaximumEntropy

I noticed that using batch_first with pack_padded_Sequence and pad_packed_sequence with an LSTM/GRU doesn't give me the expected output shape.

import torch.nn as nn
from torch.autograd import Variable
from torch.nn.utils.rnn import pad_packed_sequence, pack_padded_sequence

x = Variable(torch.randn(10, 20, 30))
lens = range(20)
lens = [z + 1 for z in lens]
lens = lens[::-1][:10]
x = pack_padded_sequence(x, lens, batch_first=True)

lstm = nn.LSTM(30, 50, batch_first=True)
h0 = Variable(torch.zeros(1, 10, 50))
c0 = Variable(torch.zeros(1, 10, 50))

packed_h, (packed_h_t, packed_c_t) = lstm(x, (h0, c0))
h, _ = pad_packed_sequence(packed_h)
print h.size() # Size 20 x 10 x 50 instead of 10 x 20 x 50


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions