我有两个这样的张量:
1st tensor
[[0,0],[0,1],[0,2],[1,3],[1,4],[2,1],[2,4]]
2nd tensor
[[0,1],[0,2],[1,4],[2,4]]
我希望结果张量像这样:
[[0,0],[1,3],[2,1]] # differences between 1st tensor and 2nd tensor
我尝试使用set,list,torch.where,..,但找不到任何好的方法来实现这一目标。有什么办法可以在两个不同大小的张量之间获得不同的行吗? (需要高效)
答案 0 :(得分:2)
您可以进行成对比较,以查看第二个向量中存在第一个张量的元素。
a = torch.as_tensor([[0,0],[0,1],[0,2],[1,3],[1,4],[2,1],[2,4]])
b = torch.as_tensor([[0,1],[0,2],[1,4],[2,4]])
# Expand a to (7, 1, 2) to broadcast to all b
a_exp = a.unsqueeze(1)
# c: (7, 4, 2)
c = a_exp == b
# Since we want to know that all components of the vector are equal, we reduce over the last fim
# c: (7, 4)
c = c.all(-1)
print(c)
# Out: Each row i compares the ith element of a against all elements in b
# Therefore, if all row is false means that the a element is not present in b
tensor([[False, False, False, False],
[ True, False, False, False],
[False, True, False, False],
[False, False, False, False],
[False, False, True, False],
[False, False, False, False],
[False, False, False, True]])
non_repeat_mask = ~c.any(-1)
# Apply the mask to a
print(a[non_repeat_mask])
tensor([[0, 0],
[1, 3],
[2, 1]])
如果感觉凉爽,可以做一支衬里:)
a[~a.unsqueeze(1).eq(b).all(-1).any(-1)]
答案 1 :(得分:0)
如果有人正在寻找一个向量为dim=1的解决方案,这是@Guillem解决方案的改编
a = torch.tensor(list(range(0, 10)))
b = torch.tensor(list(range(5,15)))
a[~a.unsqueeze(1).eq(b).any(1)]
输出:
tensor([0, 1, 2, 3, 4])
这是另一种解决方案,当您想要绝对差异时,而不仅仅是将第一个与第二个进行比较。使用时要小心,因为这里的顺序无关紧要
combined = torch.cat((a, b))
uniques, counts = combined.unique(return_counts=True)
difference = uniques[counts == 1]
输出
tensor([ 0, 1, 2, 3, 4, 10, 11, 12, 13, 14])