I have a matrix A and its a 2 x n tensor and it represents a collection of n points with coordinates (x, y). I have a separate tensor B that is also 2 x m, (n>m) and all points in B is in A, I want a tensor C that is the collection of points in A that is not in B (set difference).
in regular python, if we have a list of points and if we want to find the difference we do the following:
C = set(A) - set(B)
I want to know if there’s a method of doing this on the GPU, I know how to do a one dimensional tensor with a bool tensor and a mask ( non-intersection of 1d tensor ). but if I put in a two dimensional tensor, then the output gets flatten and loses the dimensionality of the points
My code so far:
def set_differ(a, b):
A = torch.tensor(a, device = 'cuda')
B = torch.tensor(b, device = 'cuda')
size = A.size()
# bool_vec is a 1d mask that tells if the point stored at that index is included in the difference or not
bool_vec = torch.ones(1, size[0], dtype = torch.bool, device="cuda")
for point in B:
# update bool_vec if the point (x, y) is in A, change the value at that index to False
bool_vec = bool_vec & (A != point).any(1)
# stack the bool_vec so that it is the same size as A
bool_vec = torch.cat([bool_vec]*size[1], 0)
set_difference = A[torch.t(bool_vec)]
inputs and outputs:
A = tensor([[ 1, 1],
[10, 6],
[ 4, 5],
[ 9, 8],
[ 0, 6],
[ 7, 1],
[17, 16]], device='cuda:0')
B = tensor([[1, 1],
[7, 1]], device='cuda:0')
desired output = tensor([10, 6],
[ 4, 5],
[ 9, 8],
[ 0, 6],
[17, 16]], device='cuda:0')
actual output: tensor([10, 6, 4, 5, 9, 8, 0, 6, 17, 16], device='cuda:0')