ChaiBapchya commented on a change in pull request #12967: [MXNET-1173] Debug 
operators - isfinite and isinf
URL: https://github.com/apache/incubator-mxnet/pull/12967#discussion_r228015017
 
 

 ##########
 File path: python/mxnet/ndarray/contrib.py
 ##########
 @@ -460,3 +461,65 @@ def _to_python_scalar(inputs, type_, name):
         return then_func()
     else:
         return else_func()
+
+def isinf(data, ctx=None):
+    """Checks whether a given NDArray has infinity or not
+
+
+    Parameters
+    ----------
+    input : NDArray
+        An N-D NDArray.
+    ctx : Context
+        Device context of output. Default is current context.
+
+    Returns
+    -------
+    output: NDArray
+        The output NDarray with 1 and 0 indicating whether infinite or not.
+
+    Examples
+    --------
+    >>> data = mx.nd.array([np.inf, -np.inf, np.NINF, -1])
+    >>> output = mx.nd.contrib.isinf(data)
+    >>> output
+    [1. 1. 1. 0.]
+    <NDArray 4 @cpu(0)>
+    """
+    if ctx is None:
+        ctx = current_context()
+    # any(x in data for x in [np.inf, -np.inf])
+    # abs(data) == np.inf > TypeError: bad operand type for abs(): 'NDArray'
+    # [abs(x) == np.inf for x in data] > TypeError: bad operand type for 
abs(): 'NDArray'
+    return ndarray.NDArray.abs(data) == np.inf
+
+def isfinite(data, ctx=None):
+    """Checks whether a given NDArray has finit value or not
+
+
+    Parameters
+    ----------
+    input : NDArray
+        An N-D NDArray.
+    ctx : Context
+        Device context of output. Default is current context.
+
+    Returns
+    -------
+    output: NDArray
+        The output NDarray with 1 and 0 indicating whether finite or not.
+
+    Examples
+    --------
+    >>> data = mx.nd.array([np.inf, -np.inf, np.NINF, -1])
+    >>> output = mx.nd.contrib.isfinite(data)
+    >>> output
+    [0. 0. 0. 1.]
+    <NDArray 4 @cpu(0)>
+    """
+    if ctx is None:
+        ctx = current_context()
+    # any(x in data for x in [np.inf, -np.inf])
+    # abs(data) == np.inf > TypeError: bad operand type for abs(): 'NDArray'
+    # [abs(x) == np.inf for x in data] > TypeError: bad operand type for 
abs(): 'NDArray'
+    return ndarray.NDArray.abs(data) != np.inf
 
 Review comment:
   for isinf it was returning 0 for NaN (the way it was written)
   however for is_finite it wasn't returning 0
   so now worked on it. Thanks
   ultimately both isinf and isfinite return 0 for NaN

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to