简体   繁体   English

如何检查张量值是否在不同的张量 pytorch?

[英]How to check whether tensor values in a different tensor pytorch?

I have 2 tensors of unequal size我有 2 个大小不等的张量

a = torch.tensor([[1,2], [2,3],[3,4]])
b = torch.tensor([[4,5],[2,3]])

I want a boolean array of whether each value exists in the other tensor without iterating.我想要一个 boolean 数组,其中每个值是否存在于另一个张量中而不进行迭代。 something like就像是

a in b

and the result should be结果应该是

[False, True, False]

as only the value of a[1] is in b因为只有 a[1] 的值在 b

I think it's impossible without using at least some type of iteration.我认为不使用至少某种类型的迭代是不可能的。 The most succinct way I can manage is using list comprehension:我能管理的最简洁的方法是使用列表理解:

[True if i in b else False for i in a]

Checks for elements in b that are in a and gives [False, True, False].检查 b 中 a 中的元素并给出 [False, True, False]。 Can also be reversed to get elements a in b [False, True].也可以反过来得到 b [False, True] 中的元素 a。

this should work这应该工作

result = []
for i in a:
    try: # to avoid error for the case of empty tensors
        result.append(max(i.numpy()[1] == b.T.numpy()[1,i.numpy()[0] == b.T.numpy()[0,:]]))
    except:
        result.append(False)
result

If you need to compare all subtensors across the first dimension of a , use in :如果您需要比较 a 的第一个维度上a所有子张量,请使用in

>>> [i in b for i in a]
[False, True, False]

Neither of the solutions that use tensor in tensor work in all cases for the OP.在张量中使用tensor in tensor的解决方案都不适用于 OP。 If the tensors contain elements/tuples that match in at least one dimension, the aforementioned operation will return True for those elements, potentially leading to hours of debugging.如果张量包含至少在一个维度上匹配的元素/元组,则上述操作将为这些元素返回True ,这可能会导致数小时的调试。 For example:例如:

torch.tensor([2,5]) in torch.tensor([2,10]) # returns True
torch.tensor([5,2]) in torch.tensor([5,10]) # returns True

A solution for the above could be forcing the check for equality in each dimension, and then applying a Tensor Boolean add.上述解决方案可能是强制检查每个维度的相等性,然后应用张量 Boolean 添加。 Note, the following 2 methods may not be very efficient because Tensors are rather slow for iterating and equality checking, so converting to numpy may be needed for large data:请注意,以下 2 种方法可能效率不高,因为Tensors在迭代和相等检查方面相当慢,因此对于大数据可能需要转换为numpy

[all(torch.any(i == b, dim=0)) for i in a] # OR
[any((i[0] == b[:, 0]) & (i[1] == b[:, 1])) for i in a]

That being said, @yuri's solution also seems to work for these edge cases, but it still seems to fail occasionally, and it is rather unreadable.话虽如此,@yuri 的解决方案似乎也适用于这些边缘情况,但它似乎仍然偶尔会失败,而且相当难以理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM