generate 1D tensor as unique index of rows of an 2D tensor
Let's say we transform a 2D tensor to a 1D tensor by giving each, different row a different index, from 0 to the number of rows - 1.
[[1,2],[1,3],[1,4]] -&gt; [0,1,2]

But if there are same rows, then we repeate the index, like this below.
[[1,2],[1,2],[1,4]] -&gt; [0,0,2]

[[1,2],[1,3],[1,2]] -&gt; [0,1,0]

How to implement this on PyTorch?

You can do so using torch.Tensor.unique and returning the inverse which provides the indices out of the box:
&gt;&gt;&gt; _, i = x.unique(dim=0, return_inverse=True)

&gt;&gt;&gt; i # first example
tensor([0, 1, 2])

