tensorflow_probability/python/math/numeric.py
def log1psquare(x, name=None):
  """Numerically stable calculation of `log(1 + x**2)` for small or large `|x|`.

  For sufficiently large `x` we use the following observation:

  ```none
  log(1 + x**2) =   2 log(|x|) + log(1 + 1 / x**2)
                --> 2 log(|x|)  as x --> inf
  ```

  Numerically, `log(1 + 1 / x**2)` is `0` when `1 / x**2` is small relative to
  machine epsilon.

  Args:
    x: Float `Tensor` input.
    name: Python string indicating the name of the TensorFlow operation.
      Default value: `'log1psquare'`.

  Returns:
    log1psq: Float `Tensor` representing `log(1. + x**2.)`.
  """
  with tf.compat.v1.name_scope(name, 'log1psquare', [x]):
    x = tf.convert_to_tensor(value=x, dtype_hint=tf.float32, name='x')
    dtype = x.dtype.as_numpy_dtype

    eps = np.finfo(dtype).eps.astype(np.float64)
    is_large = tf.abs(x) > (eps**-0.5).astype(dtype)

    # Mask out small x's so the gradient correctly propagates.
    abs_large_x = tf.where(is_large, tf.abs(x), tf.ones_like(x))
    return tf.where(is_large, 2. * tf.math.log(abs_large_x),
                    tf.math.log1p(tf.square(x)))
def log1psquare(x, name=None):
  """Numerically stable calculation of `log(1 + x**2)` for small or large `|x|`.

  For sufficiently large `x` we use the following observation:

  ```none
  log(1 + x**2) =   2 log(|x|) + log(1 + 1 / x**2)
                --> 2 log(|x|)  as x --> inf
  ```

  Numerically, `log(1 + 1 / x**2)` is `0` when `1 / x**2` is small relative to
  machine epsilon.

  Args:
    x: Float `Tensor` input.
    name: Python string indicating the name of the TensorFlow operation.
      Default value: `'log1psquare'`.

  Returns:
    log1psq: Float `Tensor` representing `log(1. + x**2.)`.
  """
  with tf.compat.v1.name_scope(name, 'log1psquare', [x]):
    x = tf.convert_to_tensor(value=x, dtype_hint=tf.float32, name='x')
    dtype = x.dtype.as_numpy_dtype

    eps = np.finfo(dtype).eps.astype(np.float64)
    is_large = tf.abs(x) > (eps**-0.5).astype(dtype)

    # Mask out small x's so the gradient correctly propagates.
    abs_large_x = tf.where(is_large, tf.abs(x), tf.ones_like(x))
    return tf.where(is_large, 2. * tf.math.log(abs_large_x),
                    tf.math.log1p(tf.square(x)))
def log1psquare ( x , name = None ) : with tf . compat . v1 . name_scope ( name , 'log1psquare' , [ x ] ) : x = tf . convert_to_tensor ( value = x , dtype_hint = tf . float32 , name = 'x' ) dtype = x . dtype . as_numpy_dtype eps = np . finfo ( dtype ) . eps . astype ( np . float64 ) is_large = tf . abs ( x ) > ( eps ** - 0.5 ) . astype ( dtype ) # Mask out small x's so the gradient correctly propagates. abs_large_x = tf . where ( is_large , tf . abs ( x ) , tf . ones_like ( x ) ) return tf . where ( is_large , 2. * tf . math . log ( abs_large_x ) , tf . math . log1p ( tf . square ( x ) ) )
Numerically stable calculation of `log(1 + x**2)` for small or large `|x|`.

  For sufficiently large `x` we use the following observation:

  ```none
  log(1 + x**2) =   2 log(|x|) + log(1 + 1 / x**2)
                --> 2 log(|x|)  as x --> inf
  ```

  Numerically, `log(1 + 1 / x**2)` is `0` when `1 / x**2` is small relative to
  machine epsilon.

  Args:
    x: Float `Tensor` input.
    name: Python string indicating the name of the TensorFlow operation.
      Default value: `'log1psquare'`.

  Returns:
    log1psq: Float `Tensor` representing `log(1. + x**2.)`.
Numerically stable calculation of log ( 1 + x ** 2 ) for small or large |x| .