0

In my tests I need to assertEqual 2 tensor values. Sometimes both of them are tensors of zero actually any values, for example:

def test_preprocess_text_single_line(self):
  self.assertEqual(torch.tensor([0, 2, 0, 1, 0, 0, 0]), torch.tensor([0, 0, 2, 0, 0, 8, 0]))

Which leads to RuntimeError:

self.assertEqual(torch.tensor([0, 2, 0, 1, 0, 0, 0]), torch.tensor([0, 0, 2, 0, 0, 8, 0]))

RuntimeError: Boolean value of Tensor with more than one value is ambiguous

How to avoid that?

orkenstein
  • 2,810
  • 3
  • 24
  • 45
  • Maybe the answer to your question is on https://stackoverflow.com/questions/32996281/how-to-check-if-two-torch-tensors-or-matrices-are-equal ? I tested it on my end with two zero tensors and it returns True – bmasri Aug 24 '22 at 13:33
  • @bmasri thank you, but I actually have a dictionary with tensors as values with quite complex structure. There's somewhere a comparison internally, which fails on comparison of tensors full of zeros. – orkenstein Aug 24 '22 at 20:30

0 Answers0