Remove rows from tensor by matching
I'm trying to do some operation like if there is tensor in pytorch
a = torch.tensor([[1,0]
                  ,[0,1]
                  ,[2,0]
                  ,[3,2]])


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

I want to remove the rows [0,1], [2,0] which are the rows of b from a.
Is there any way to do this?
# result
a = torch.tensor([[1,0]
                  ,[3,2]])


You could do it if the tensor shapes were broadcastable.
For a tensor a of shape (?, d) and a tensor b of shape (d,), you could write something like:
cmp = a.eq(b).all(dim=1).logical_not(), i.e. compare each d-dimensional row of a with b and give me the indices where the comparison is False.
From these you can then easily your new tensor like so:
a = a[cmp]
I doubt you'll find an elegant way of doing this when b itself contains a batch dimension; your best bet would be to write a for loop.
Full example:
&gt;&gt;&gt; xs = torch.tensor([[1,0], [0,1], [2,0], [3,2]])
&gt;&gt;&gt; ys = torch.tensor([[0,1],[2,0]])
&gt;&gt;&gt; for y in ys:
...     xs = xs[xs.eq(y).all(dim=1).logical_not()]
&gt;&gt;&gt; xs
tensor([[1, 0],
        [3, 2]])



