0

how to check for example if:

torch.tensor([1, 3]) belongs to torch.tensor([3, 1], [1, 1], [3, 1])

a in b methods compare element-wise and thus here not correct

what I want to compute is whether the whole tensor [1, 3] is in the bigger one

thanks

yuri
  • 99
  • 8
  • How do you define that `[1, 3]` is bigger than `[3, 1]`? – Ivan Feb 10 '21 at 18:52
  • please refer to my answer to this [question](https://stackoverflow.com/questions/66036375/how-to-check-whether-tensor-values-in-a-different-tensor-pytorch/66144121#66144121) – yuri Feb 10 '21 at 20:06
  • So you want to check whether your tensor is a subtensor of the bigger one up to a permutation, is that right ? Like in the example above, do you want the output to be positive (it is in the bigger one, but permuted) or not (it is not in the bigger one) ? – trialNerror Feb 10 '21 at 20:32
  • the task to do is to check if every sub-tensor from tensor a, exist in tensor b. this has nothing to do with which is bigger and smaller – yuri Feb 11 '21 at 07:38

1 Answers1

0

we illustreate the answer with the following example:

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

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

output will show: [False, True, False]

yuri
  • 99
  • 8
  • Okay then what is wrong with this example ? How is that any different from the question you linked yourself in the comments above ? Because here the result IS `[False, True, False]` – trialNerror Feb 11 '21 at 09:14
  • nothing is wrong here I am just answering my own question I was looking for a predefined function in PyTorch that performs this. once I had not find any, I implemented this solution – yuri Feb 11 '21 at 09:19