code: def _log_xexp_ratio(x):
      """Compute log(x * (exp(x) + 1) / (exp(x) - 1)) in a numerically stable way."""
      with tf.name_scope("log_xexp_ratio"):
          x = tf.convert_to_tensor(x)
          dtype = dtype_util.as_numpy_dtype(x.dtype)
          eps = np.finfo(dtype).eps
  
          # This function is even, hence we use abs(x) everywhere.
          x = tf.math.abs(x)
  
          # For x near zero, we have the Taylor series:
          # log(2) + x**2 / 12 - 7 x**4 / 1440 + 31 x**6 / 90720 + O(x**8)
          # whose coefficients decrease in magnitude monotonically
  
          # For x large in magnitude, the ratio (exp(x) + 1) / (exp(x) - 1)) tends to
          # sign(x), so thus this function should tend to log(abs(x))
  
          # Finally for x medium in magnitude, we can use the naive expression. Thus,
          # we generate 2 cutofs.
  
          # Use the first 3 non-zero terms of the Taylor series when
          # |x| < small_cutoff.
          small_cutoff = np.power(eps * 90720.0 / 31, 1 / 6.0)
  
          # Use log(abs(x)) when |x| > large_cutoff
          large_cutoff = -np.log(eps)
  
          x_squared = tf.math.square(x)
  
          result = (
              dtype(np.log(2.0))
              + x_squared / 12.0
              - 7 * tf.math.square(x_squared) / 1440.0
          )
          middle_region = (x > small_cutoff) & (x < large_cutoff)
          safe_x_medium = tf.where(middle_region, x, dtype(1.0))
          result = tf.where(
              middle_region,
              (
                  tf.math.log(safe_x_medium)
                  + tf.math.softplus(safe_x_medium)
                  - tf.math.log(tf.math.expm1(safe_x_medium))
              ),
              result,
          )
  
          # We can do this by choosing a cutoff when x > log(1 / machine eps)
          safe_x_large = tf.where(x >= large_cutoff, x, dtype(1.0))
          result = tf.where(
              x >= large_cutoff, tf.math.log(safe_x_large), result
          )
          return result
  
imports: import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.bijectors import sigmoid
  from tensorflow_probability.python.distributions import distribution
  from tensorflow_probability.python.distributions import kullback_leibler
  from tensorflow_probability.python.internal import assert_util
  from tensorflow_probability.python.internal import dtype_util
  from tensorflow_probability.python.internal import parameter_properties
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.internal import reparameterization
  from tensorflow_probability.python.internal import samplers
  from tensorflow_probability.python.internal import tensor_util
  from tensorflow_probability.python.math import generic
label_desc: Use image node_id 1 for calling a global function with example usage: _log_xexp_ratio(x) and returns: result