Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/torch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ Comparison Ops
.. autofunction:: equal
.. autofunction:: ge
.. autofunction:: gt
.. autofunction:: isinf
.. autofunction:: isnan
.. autofunction:: kthvalue
.. autofunction:: le
Expand Down
4 changes: 4 additions & 0 deletions test/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4533,6 +4533,10 @@ def test_isnan(self):
x = torch.Tensor([1, float('nan'), 2])
self.assertEqual(torch.isnan(x), torch.ByteTensor([0, 1, 0]))

def test_isinf(self):
x = torch.Tensor([1, float('inf'), 2, float('-inf'), float('nan')])
self.assertEqual(torch.isinf(x), torch.ByteTensor([0, 1, 0, 1, 0]))

def test_RNGState(self):
state = torch.get_rng_state()
stateCloned = state.clone()
Expand Down
22 changes: 21 additions & 1 deletion torch/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'argmin',
'btrifact',
'btriunpack',
'isinf',
'isnan',
'split',
'unbind',
Expand Down Expand Up @@ -148,6 +149,25 @@ def btriunpack(LU_data, LU_pivots, unpack_data=True, unpack_pivots=True):
return P, L, U


def isinf(tensor):
r"""Returns a new tensor with boolean elements representing if each element is `+/-INF` or not.

Arguments:
tensor (Tensor): A tensor to check

Returns:
Tensor: A ``torch.ByteTensor`` containing a 1 at each location of `+/-INF` elements and 0 otherwise

Example::

>>> torch.isinf(torch.Tensor([1, float('inf'), 2, float('-inf'), float('nan')]))
tensor([ 0, 1, 0, 1, 0], dtype=torch.uint8)
"""
if not isinstance(tensor, torch.Tensor):
raise ValueError("The argument is not a tensor", str(tensor))
return tensor.abs() == float('inf')


def isnan(tensor):
r"""Returns a new tensor with boolean elements representing if each element is `NaN` or not.

Expand All @@ -163,7 +183,7 @@ def isnan(tensor):
tensor([ 0, 1, 0], dtype=torch.uint8)
"""
if not isinstance(tensor, torch.Tensor):
raise ValueError("The argument is not a tensor")
raise ValueError("The argument is not a tensor", str(tensor))
return tensor != tensor


Expand Down