Non-intersection of two n-dimentional pytorch tensors
Thanks everyone in advance for your help! What I'm trying to do in PyTorch is to compute non-intersection ( lets call it torch.nonintersection) for tensors of many dimenstions (without for loops because I want it to be effectively performed on GPU). So here is the example how it should work:
a = torch.tensor([[ 0.,  0.], [ 0.,  1.], [ 0.,  2.], [ 1.,  0.], [ 1.,  1.], [ 1.,  2.], [ 1.,  3.], 
                  [ 2.,  0.], [ 2.,  1.], [ 2.,  2.]])
b = torch.tensor([[ 2.,  0.], [ 2.,  1.], [ 2.,  2.], [ 1.,  0.], [ 1.,  1.], [ 1.,  2.], [ 1.,  3.]])

torch.spec_unique(a,b) = torch.tensor([ 0.,  0.], [ 0.,  1.], [ 0.,  2.])

I have analogs with for loops, but they take too much time right now. Any Ideas how it can be done? Much appreciated!

Shape of a and b is different in your case (a is (10, 2), while b is (7, 2)), so you have to &quot;cut&quot; the bigger tensor to the size of the smaller one. Function below handles it with simple if (it won't slow down your computations):
def non_intersection(a, b):
    if a.shape[0] &gt; b.shape[0]:
        return torch.nonzero(a[: b.shape[0]] != b, as_tuple=False)
    return torch.nonzero(b[: a.shape[0]] != a, as_tuple=False)

This would return:
tensor([[0, 0],
        [1, 0],
        [2, 0]])

So the columns are in opposite order. If you wish to get columns just like in your example you can do this on non_intersection output:
torch.index_select(non_intersection(a, b), 1, torch.tensor([1, 0])))

