What is the “torchaic” way of reducing a tensor with a logical and?
t = torch.tensor([True,True,True])
f = torch.tensor([True,False,True])
# if t:
# this is the ideal syntax I want but it
# throws RuntimeError: Boolean value of Tensor with more than one value is ambiguous
# if torch.and(t):
# this could also be the ideal syntax I want but it
# throws SyntaxError: invalid syntax
# this works, but do I really have to write my own function?
def is_all_true(t):
r = torch.tensor([True])
for i in range(len(t)):
r = torch.bitwise_and(r, t[i])
return r
if is_all_true(t):
print("t is all True") # outputs "t is all True"
if not is_all_true(f):
print("f is not all True") # outputs "f is not all True"