Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if PyTorch tensors are equal within epsilon

Tags:

pytorch

How do I check if two PyTorch tensors are semantically equal?

Given floating point errors, I want to know if the the elements differ only by a small epsilon value.

like image 903
Tom Hale Avatar asked Nov 19 '18 12:11

Tom Hale


1 Answers

At the time of writing, this is a undocumented function in the latest stable release (0.4.1), but the documentation is in the master (unstable) branch.

torch.allclose() will return a boolean indicating whether all element-wise differences are equal allowing for a margin of error.

Additionally, there's the undocumented isclose():

>>> torch.isclose(torch.Tensor([1]), torch.Tensor([1.00000001]))
tensor([1], dtype=torch.uint8)
like image 100
Tom Hale Avatar answered Sep 28 '22 01:09

Tom Hale