Hi all,
Goal: select the last item in a PackedSequence for each sequence in the batch.
I have a tensor of shape [16,80,300] (batch_size X sequence_len X features), which is the output of an LSTM. The sequences are zero-padded so I also have a list of indexes of length [16] which specify their length.
I basically want to select the last item in the sequence for each sequence in the batch (e.g. first seq may be of length 40, next one is of length 60, etc.), so I would end up with a [16,300] tensor. But, since the sequences are of variable length I cant just do input[:, -1, :], because every sequences has its own length.
I saw this thread and tried to replicate but having some trouble:
x = torch.FloatTensor(16,80,300)  # rnn output
idx = torch.LongTensor([random.randint(10,79) for i in xrange(16)])  # list of lengths
x.gather(1, idx.view(-1, 1, 1))

but I get: RuntimeError: Expected tensor [16 x 1 x 1], src [16 x 80 x 300] and index [16 x 1 x 1] to have the same size in dimension 1 at /Users/soumith/miniconda2/conda-bld/pytorch_1501999754274/work/torch/lib/TH/generic/THTensorMath.c:445.