title: Get each sequence's last item from packed sequence
body: <p>I am trying to put a packed and padded sequence through a GRU, and retrieve the output of the last item of each sequence. Of course I don't mean the <code>-1</code> item, but the actual last, not-padded item. We know the lengths of the sequences in advance, so it should be as easy as to extract for each sequence the <code>length-1</code> item. </p>
  
  <p>I tried the following</p>
  
  <pre><code>import torch
  from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
  
  # Data
  input = torch.Tensor([[[0., 0., 0.],
                         [1., 0., 1.],
                         [1., 1., 0.],
                         [1., 0., 1.],
                         [1., 0., 1.],
                         [1., 1., 0.]],
  
                        [[1., 1., 0.],
                         [0., 1., 0.],
                         [0., 0., 0.],
                         [0., 1., 0.],
                         [0., 0., 0.],
                         [0., 0., 0.]],
  
                        [[0., 0., 0.],
                         [1., 0., 0.],
                         [1., 1., 1.],
                         [0., 0., 0.],
                         [0., 0., 0.],
                         [0., 0., 0.]],
  
                        [[1., 1., 0.],
                         [0., 0., 0.],
                         [0., 0., 0.],
                         [0., 0., 0.],
                         [0., 0., 0.],
                         [0., 0., 0.]]])
  
  lengths = [6, 4, 3, 1]
  p = pack_padded_sequence(input, lengths, batch_first=True)
  
  # Forward
  gru = torch.nn.GRU(3, 12, batch_first=True)
  packed_output, gru_h = gru(p)
  
  # Unpack
  output, input_sizes = pad_packed_sequence(packed_output, batch_first=True)
  
  last_seq_idxs = torch.LongTensor([x-1 for x in input_sizes])
  
  last_seq_items = torch.index_select(output, 1, last_seq_idxs) 
  
  print(last_seq_items.size())
  # torch.Size([4, 4, 12])
  </code></pre>
  
  <p>But the shape is not what I expect. I had expected to get <code>4x12</code>, i.e. <code>last item of each individual sequence x hidden</code>.`</p>
  
  <p>I could loop through the whole thing, and build a new tensor containing the items I need, but I was hoping for a built-in approach that took advantage of some smart math. I fear that manually looping and building, will result in very poor performance.</p>