How to get unique value along 1 dim of a 2d array in pytorch?
I have a 2d tensor now, which may have repeated elements along a dim, like

tmp = torch.tensor([[1,2,3,2,4],[0,5,6,7,2],[3,4,5,3,5],[7,5,6,7,7]])


I hope to get unique elements along dim=1, the result should be like this

result = [[1,2,3,4],[0,5,6,7,2],[3,4,5],[5,6,7]]


Is there a way could get the result without using for- loop?

I tried using torch.unique,  like this, 

result=[]
for i in range(tmp.shape[0]):
    t = tmp[i,:]
    result.append(torch.unique(t))


It works, but time-consuming.

You cannot call unique on n-rank tensor when n>=2.
This is because in PyTorch there are no jagged array tensors.

tmp = torch.tensor([[1,2,3,2,4],[0,5,6,7,2],[3,4,5,3,5],[7,5,6,7,7]])
%timeit tmpt =torch.unbind(tmp); [torch.unique(t) for t in tmpt]


This returned 39.3 µs, while your original loop took 

tmp = torch.tensor([[1,2,3,2,4],[0,5,6,7,2],[3,4,5,3,5],[7,5,6,7,7]])
result=[]
%timeit for i in range(tmp.shape[0]):    t = tmp[i,:] ;    result.append(torch.unique(t))


54 µs on average
