1

So, I followed this answer on SO

I'm trying to equate two tensors

torch.equal(x_valid[0], x_valid[:1]) returns False whereas torch.all(torch.eq(x_valid[0], x_valid[:1])) returns tensor(1, dtype=torch.uint8)

I know for a fact that both tensors are same like first value of x_valid so why torch.equal returns False?

except the fact that x_valid[0] returns ([0, 0, ...,0]) & x_valid[:1] returns ([[0, 0, ...,0]])

but type of both of them is still tensor. So I can't really understand why the output of first query is False

abhimanyuaryan
  • 3,882
  • 5
  • 41
  • 83

1 Answers1

1

torch.equal(tensor1, tensor2) return True if two tensors have the same size and elements, False otherwise. Check here.

Example:

y = torch.tensor([[0, 0, 0]])
print(y[0], y[0].shape)
print(y[:1], y[:1].shape)
print(torch.equal(y[0], y[:1]))
print(torch.equal(y[0], y[:1][0])) # (torch.Size([3]), torch.Size([3]))

output:

tensor([0, 0, 0]) torch.Size([3])
tensor([[0, 0, 0]]) torch.Size([1, 3])
False
True

Whereas, torch.eq(input, other, out=None) computes element-wise equality. Here, one important to note is that the second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Anubhav Singh
  • 8,321
  • 4
  • 25
  • 43