In PyTorch we can do comparisons between elements in tensors like so:
import torch
a = torch.tensor([[1,2], [2,3], [3,4]])
b = torch.tensor([[3,4], [1,2], [2,3]])
print(a.size())
# torch.Size([3, 2])
print(b.size())
# torch.Size([3, 2])
c = a[:, 0] < b[:, 0]
print(c)
# tensor([ True, False, False])
However, when we try to add a condition, the snippet fails:
c = a[:, 0] < b[:, 1] < b[:, 0]
The expected output is
tensor([ False, False, False])
So, for each element in a, compare its first element with the second element of the corresponding item in b, and compare that element with the first element of the same item in b.
Traceback (most recent call last):
File “scratch_12.py”, line 9, in
c = a[:, 0] < b[:, 1] < b[:, 0]
RuntimeError: bool value of Tensor with more than one value is ambiguous
Why is that, and how can we solve it?