How to transform a 2D and index tensors for torch.nn.utils.rnn.pack_sequence
I have a sequences collection in the following form:

sequences = torch.tensor([[2,1],[5,6],[3,0])
indexes = torch.tensor([1,0,1])


that is, the sequence 0 is made of just [5,6], and the sequence 1 is made of [2,1] , [3,0]. Mathematically sequence[i] = { sequences[j] such that i = indexes[j] }

I need to feed these sequences into an LSTM. Since these are variable-length sequences, pytorch documentation states to use something like torch.nn.utils.rnn.pack_sequence.

Sadly, this method and its like want, as input, a list of tensors where each of them is a L x *, with L being the length of the single sequence.

How can build something that can be fed into a pytorch LSTM?

P.s. throughout the code I work with these tensors using scatter and gather functionalities but I can't find a way to use them to achieve this goal.

I found an alternative and more efficient way to separate the sequences:

sequences = torch.tensor([[2,1],[5,6],[3,0]])
indexes = torch.tensor([1,0,1])

sorted_src = src[indexes.argsort()]
indexes_count = torch.unique(indexes, return_counts=True)[1]

splitted = torch.split(sorted_src, indexes_count.tolist(), dim=0)


This method is almost 3 times faster then the one proposed by @Mercury. 


  Measured using timeit module with sequences being a (5000,256) tensor and indexes being (1500)

