You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/10/26 20:38:35 UTC

[GitHub] apeforest commented on a change in pull request #12967: [MXNET-1173] Debug operators - isfinite, isinf and isnan

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

 ##########
 File path: python/mxnet/ndarray/contrib.py
 ##########
 @@ -460,3 +461,96 @@ def _to_python_scalar(inputs, type_, name):
         return then_func()
     else:
         return else_func()
+
+def isinf(data, ctx=None):
+    """Performs an element-wise check to determine if the NDArray contains an infinite element
+    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 same shape as input, where 1 indicates the array element is
+        equal to positive or negative infinity and 0 otherwise.
+
+    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()
+    return data.abs() == np.inf
+
+def isfinite(data, ctx=None):
+    """Performs an element-wise check to determine if the NDArray contains an infinite element
+    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 same shape as input, where 1 indicates the array element is
+        finite i.e. not equal to positive or negative infinity and 0 in places where it is
+        positive or negative infinity.
+
+    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()
+    is_data_not_nan = data == data
+    is_data_not_infinite = ndarray.NDArray.abs(data) != np.inf
+    return ndarray.logical_and(is_data_not_infinite, is_data_not_nan)
+
+def isnan(data, ctx=None):
+    """Performs an element-wise check to determine if the NDArray contains a NaN element
+    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 same shape as input, where 1 indicates the array element is
+        NaN i.e. Not a Number and 0 otherwise.
+
+    Examples
+    --------
+    >>> data = mx.nd.array([np.nan, -1])
+    >>> output = mx.nd.contrib.isnan(data)
+    >>> output
+    [1. 0.]
+    <NDArray 2 @cpu(0)>
+    """
+    if ctx is None:
+        ctx = current_context()
 
 Review comment:
   same here, why ctx is passed in while not used.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services