I actually think I did this incorrectly, What it does is select the part of the tensor in the given tensor at indexes given.
But you want to select one output for each batch.  I think this would select all the lengths for all the batches.  You could do something similar by combining the batch dim and the sequence dim then selecting from that…
lengths = [5, 3, 1]
#batch_size = 3
#max_sequence_length = 5
#classes = 2
a = torch.arange(30).view(3, 5, 2)
a = a.view(3*5, 2)
a.index_select(0, (torch.LongTensor(lengths)-1)*3)

The link to the docs is here 26