Hi lkp411!



 lkp411:

Is there a way to efficiently, in a vectorized manner, compute the combinations along each rows of a 2D tensor individually?


Yes, the idea would be to construct the combinations of a 1D vector
of indices and then index into your multidimensional tensor, swapping
the dimensions around appropriately.
Like this:
>>> import torch
>>> torch.__version__
'1.9.0'
>>> a = torch.tensor([[2, 5, 6], [7, 9, 4]])
>>> a.T[torch.combinations (torch.arange (a.shape[1]), 2)].permute (2, 0, 1)
tensor([[[2, 5],
         [2, 6],
         [5, 6]],

        [[7, 9],
         [7, 4],
         [9, 4]]])

Best.
K. Frank