Ejemplo n.º 1
0
 def compute_global_xbetainc():
     global_mean = tf.reduce_mean(class_means, axis=0, name='global_mean')
     class_diffs = (class_means - global_mean)**2.
     class_counts = tf.reshape(tf.cast(unique_counts,tf.float32), (-1, 1) )
     between = tf.reduce_sum(tf.multiply(class_counts, class_diffs), axis=0)
     within = tf.reduce_sum(within_diffs, axis=0)
     d1 = n_classes - 1
     d2 = tf.cast(tf.reduce_sum(unique_counts), tf.float32) - n_classes
     s = (between / d1) / (within / d2)
     xmin = 1.0e-20
     xmax = 1. - xmin
     return tf.clip_by_value(1.-tf.betainc(d1/2., d2/2., (d1*s) / (d1*s + d2) ), xmin, xmax)
Ejemplo n.º 2
0
 def compute_pair_f(item):
     between,within,pair_count,r_mean = item
     d1 = 1.0 #(two classes) - 1
     d2 = tf.cast(pair_count, tf.float32) - 2.0 #n - n_classes
     x = (between / (between + within))
     # Clip the probabilities to prevent numerical instability
     xmin = 1.0e-37
     xmax = 1. - 1.0e-5
     x_limit = tf.clip_by_value(x, xmin, xmax)
     xbetainc = tf.betainc(d1/2.0, d2/2.0, x_limit)
     d = model.params.f_statistic_d
     top_k, top_k_ind = tf.nn.top_k(xbetainc, k=d, sorted=False)
     result = tf.reduce_sum(tf.log(top_k))
     return result
Ejemplo n.º 3
0
def _bdtr(k, n, p):
  """The binomial cumulative distribution function.

  Args:
    k: floating point `Tensor`.
    n: floating point `Tensor`.
    p: floating point `Tensor`.

  Returns:
    `sum_{j=0}^k p^j (1 - p)^(n - j)`.
  """
  # Trick for getting safe backprop/gradients into n, k when
  #   betainc(a = 0, ..) = nan
  # Write:
  #   where(unsafe, safe_output, betainc(where(unsafe, safe_input, input)))
  ones = tf.ones_like(n - k)
  k_eq_n = tf.equal(k, n)
  safe_dn = tf.where(k_eq_n, ones, n - k)
  dk = tf.betainc(a=safe_dn, b=k + 1, x=1 - p)
  return tf.where(k_eq_n, ones, dk)
Ejemplo n.º 4
0
def _bdtr(k, n, p):
  """The binomial cumulative distribution function.

  Args:
    k: floating point `Tensor`.
    n: floating point `Tensor`.
    p: floating point `Tensor`.

  Returns:
    `sum_{j=0}^k p^j (1 - p)^(n - j)`.
  """
  # Trick for getting safe backprop/gradients into n, k when
  #   betainc(a = 0, ..) = nan
  # Write:
  #   where(unsafe, safe_output, betainc(where(unsafe, safe_input, input)))
  ones = tf.ones_like(n - k)
  k_eq_n = tf.equal(k, n)
  safe_dn = tf.where(k_eq_n, ones, n - k)
  dk = tf.betainc(a=safe_dn, b=k + 1, x=1 - p)
  return tf.where(k_eq_n, ones, dk)
Ejemplo n.º 5
0
    def _verifySampleAndPdfConsistency(self, vmf, rtol=0.075):
        """Verifies samples are consistent with the PDF using importance sampling.

    In particular, we verify an estimate the surface area of the n-dimensional
    hypersphere, and the surface areas of the spherical caps demarcated by
    a handful of survival rates.

    Args:
      vmf: A `VonMisesFisher` distribution instance.
      rtol: Relative difference tolerable.
    """
        dim = vmf.event_shape[-1].value
        nsamples = 50000
        samples = vmf.sample(sample_shape=[nsamples])
        samples = tf.check_numerics(samples, 'samples')
        log_prob = vmf.log_prob(samples)
        log_prob = tf.check_numerics(log_prob, 'log_prob')
        log_importance = -log_prob
        sphere_surface_area_estimate, samples, importance, conc = self.evaluate(
            [
                tf.exp(
                    tf.reduce_logsumexp(log_importance, axis=0) -
                    tf.log(tf.to_float(nsamples))), samples,
                tf.exp(log_importance), vmf.concentration
            ])
        true_sphere_surface_area = 2 * (np.pi)**(dim / 2) * self.evaluate(
            tf.exp(-tf.lgamma(dim / 2)))
        # Broadcast to correct size
        true_sphere_surface_area += np.zeros_like(sphere_surface_area_estimate)
        # Highly concentrated distributions do not get enough coverage to provide
        # a reasonable full-sphere surface area estimate. These are covered below
        # by CDF-based hypersphere cap surface area estimates.
        self.assertAllClose(true_sphere_surface_area[np.where(conc < 3)],
                            sphere_surface_area_estimate[np.where(conc < 3)],
                            rtol=rtol)

        # Assert surface area of hyperspherical cap For some CDFs in [.05,.45],
        # (h must be greater than 0 for the hypersphere cap surface area
        # calculation to hold).
        for survival_rate in 0.95, .9, .75, .6:
            cdf = (1 - survival_rate)
            mean_dir = self.evaluate(vmf.mean_direction)
            dotprods = np.sum(samples * mean_dir, -1)
            # Empirical estimate of the effective dot-product of the threshold that
            # selects for a given CDF level, that is the cosine of the largest
            # passable angle, or the minimum cosine for a within-CDF sample.
            dotprod_thresh = np.percentile(dotprods,
                                           100 * survival_rate,
                                           axis=0,
                                           keepdims=True)
            dotprod_above_thresh = np.float32(dotprods > dotprod_thresh)
            sphere_cap_surface_area_ests = (
                cdf * (importance * dotprod_above_thresh).sum(0) /
                dotprod_above_thresh.sum(0))
            h = (1 - dotprod_thresh)
            self.assertGreaterEqual(h.min(),
                                    0)  # h must be >= 0 for the eqn below
            true_sphere_cap_surface_area = (
                0.5 * true_sphere_surface_area *
                self.evaluate(tf.betainc((dim - 1) / 2, 0.5, 2 * h - h**2)))
            if dim == 3:  # For 3-d we have a simpler form we can double-check.
                self.assertAllClose(2 * np.pi * h,
                                    true_sphere_cap_surface_area)

            self.assertAllClose(true_sphere_cap_surface_area,
                                sphere_cap_surface_area_ests +
                                np.zeros_like(true_sphere_cap_surface_area),
                                rtol=rtol)
Ejemplo n.º 6
0
import tensorflow as tf
"""tf.betainc(a,b,x,name=None)
功能:计算I_x(a, b)。I_x(a, b) = {B(x; a, b)}/{B(a, b)}。
                    B(x; a, b) = \intergral_from_0_to_x t^{a-1} (1 - t)^{b-1} dt。
                    B(a, b) = \intergral_from_0_to_1 t^{a-1} (1 - t)^{b-1} dt。即完全beta函数。          
输入:x为张量,可以为`float32`, `float64`类型。a,b与x同类型。"""
a = tf.constant(1, tf.float64)
b = tf.constant(1, tf.float64)
x = tf.constant([[0, 0.5, 1]], tf.float64)
z = tf.betainc(a, b, x)
sess = tf.Session()
print(sess.run(z))
sess.close()
# z==>[[0. 0.5 1.]]
Ejemplo n.º 7
0
 def test_Betainc(self):
     t = tf.betainc(*self.random((3, 3), (3, 3), (3, 3)))
     self.check(t)
Ejemplo n.º 8
0
 def _cdf(self, x):
   return tf.betainc(self.concentration1, self.concentration0, x)
Ejemplo n.º 9
0
 def _cdf(self, x):
   if self.validate_args:
     x = distribution_util.embed_check_nonnegative_integer_form(x)
   return tf.betainc(self.total_count, 1. + x, tf.sigmoid(-self.logits))
Ejemplo n.º 10
0
 def _cdf(self, x):
     return tf.betainc(self.concentration1, self.concentration0, x)
Ejemplo n.º 11
0
 def _cdf(self, x):
   # Take Abs(scale) to make subsequent where work correctly.
   y = (x - self.loc) / tf.abs(self.scale)
   x_t = self.df / (y**2. + self.df)
   neg_cdf = 0.5 * tf.betainc(0.5 * self.df, 0.5, x_t)
   return tf.where(tf.less(y, 0.), neg_cdf, 1. - neg_cdf)
Ejemplo n.º 12
0
    def _testBetaInc(self, dtype):
        try:
            from scipy import special  # pylint: disable=g-import-not-at-top
            np_dt = dtype.as_numpy_dtype

            # Test random values
            a_s = np.abs(np.random.randn(10, 10) * 30).astype(
                np_dt)  # in (0, infty)
            b_s = np.abs(np.random.randn(10, 10) * 30).astype(
                np_dt)  # in (0, infty)
            x_s = np.random.rand(10, 10).astype(np_dt)  # in (0, 1)
            with self.test_session(use_gpu=self.use_gpu):
                tf_a_s = tf.constant(a_s, dtype=dtype)
                tf_b_s = tf.constant(b_s, dtype=dtype)
                tf_x_s = tf.constant(x_s, dtype=dtype)
                tf_out = tf.betainc(tf_a_s, tf_b_s, tf_x_s).eval()
            scipy_out = special.betainc(a_s, b_s, x_s).astype(np_dt)

            # the scipy version of betainc uses a double-only implementation.
            # TODO(ebrevdo): identify reasons for (sometime) precision loss
            # with doubles
            tol = 1e-4 if dtype == tf.float32 else 5e-5
            self.assertAllCloseAccordingToType(scipy_out,
                                               tf_out,
                                               rtol=tol,
                                               atol=tol)

            # Test out-of-range values (most should return nan output)
            combinations = list(
                itertools.product([-1, 0, 0.5, 1.0, 1.5], repeat=3))
            a_comb, b_comb, x_comb = np.asarray(list(zip(*combinations)),
                                                dtype=np_dt)
            with self.test_session(use_gpu=self.use_gpu):
                tf_comb = tf.betainc(a_comb, b_comb, x_comb).eval()
            scipy_comb = special.betainc(a_comb, b_comb, x_comb).astype(np_dt)
            self.assertAllCloseAccordingToType(scipy_comb, tf_comb)

            # Test broadcasting between scalars and other shapes
            with self.test_session(use_gpu=self.use_gpu):
                self.assertAllCloseAccordingToType(special.betainc(
                    0.1, b_s, x_s).astype(np_dt),
                                                   tf.betainc(0.1, b_s,
                                                              x_s).eval(),
                                                   rtol=tol,
                                                   atol=tol)
                self.assertAllCloseAccordingToType(special.betainc(
                    a_s, 0.1, x_s).astype(np_dt),
                                                   tf.betainc(a_s, 0.1,
                                                              x_s).eval(),
                                                   rtol=tol,
                                                   atol=tol)
                self.assertAllCloseAccordingToType(special.betainc(
                    a_s, b_s, 0.1).astype(np_dt),
                                                   tf.betainc(a_s, b_s,
                                                              0.1).eval(),
                                                   rtol=tol,
                                                   atol=tol)
                self.assertAllCloseAccordingToType(special.betainc(
                    0.1, b_s, 0.1).astype(np_dt),
                                                   tf.betainc(0.1, b_s,
                                                              0.1).eval(),
                                                   rtol=tol,
                                                   atol=tol)
                self.assertAllCloseAccordingToType(special.betainc(
                    0.1, 0.1, 0.1).astype(np_dt),
                                                   tf.betainc(0.1, 0.1,
                                                              0.1).eval(),
                                                   rtol=tol,
                                                   atol=tol)

            with self.assertRaisesRegexp(ValueError,
                                         "Shapes .* are not compatible"):
                tf.betainc(0.5, [0.5], [[0.5]])

            with self.test_session(use_gpu=self.use_gpu):
                with self.assertRaisesOpError("Shapes of .* are inconsistent"):
                    a_p = tf.placeholder(dtype)
                    b_p = tf.placeholder(dtype)
                    x_p = tf.placeholder(dtype)
                    tf.betainc(a_p, b_p, x_p).eval(feed_dict={
                        a_p: 0.5,
                        b_p: [0.5],
                        x_p: [[0.5]]
                    })

        except ImportError as e:
            tf.logging.warn("Cannot test special functions: %s" % str(e))
Ejemplo n.º 13
0
 def _cdf(self, x):
   # Take Abs(scale) to make subsequent where work correctly.
   y = (x - self.loc) / tf.abs(self.scale)
   x_t = self.df / (y**2. + self.df)
   neg_cdf = 0.5 * tf.betainc(0.5 * self.df, 0.5, x_t)
   return tf.where(tf.less(y, 0.), neg_cdf, 1. - neg_cdf)
Ejemplo n.º 14
0
  def _testBetaInc(self, dtype):
    try:
      from scipy import special  # pylint: disable=g-import-not-at-top
      np_dt = dtype.as_numpy_dtype

      # Test random values
      a_s = np.abs(np.random.randn(10, 10) * 30).astype(np_dt)  # in (0, infty)
      b_s = np.abs(np.random.randn(10, 10) * 30).astype(np_dt)  # in (0, infty)
      x_s = np.random.rand(10, 10).astype(np_dt)  # in (0, 1)
      with self.test_session(use_gpu=self.use_gpu):
        tf_a_s = tf.constant(a_s, dtype=dtype)
        tf_b_s = tf.constant(b_s, dtype=dtype)
        tf_x_s = tf.constant(x_s, dtype=dtype)
        tf_out = tf.betainc(tf_a_s, tf_b_s, tf_x_s).eval()
      scipy_out = special.betainc(a_s, b_s, x_s).astype(np_dt)

      # the scipy version of betainc uses a double-only implementation.
      # TODO(ebrevdo): identify reasons for (sometime) precision loss
      # with doubles
      tol = 1e-4 if dtype == tf.float32 else 5e-5
      self.assertAllCloseAccordingToType(scipy_out, tf_out, rtol=tol, atol=tol)

      # Test out-of-range values (most should return nan output)
      combinations = list(itertools.product([-1, 0, 0.5, 1.0, 1.5], repeat=3))
      a_comb, b_comb, x_comb = np.asarray(
          list(zip(*combinations)), dtype=np_dt)
      with self.test_session(use_gpu=self.use_gpu):
        tf_comb = tf.betainc(a_comb, b_comb, x_comb).eval()
      scipy_comb = special.betainc(a_comb, b_comb, x_comb).astype(np_dt)
      self.assertAllCloseAccordingToType(scipy_comb, tf_comb)

      # Test broadcasting between scalars and other shapes
      with self.test_session(use_gpu=self.use_gpu):
        self.assertAllCloseAccordingToType(
            special.betainc(0.1, b_s, x_s).astype(np_dt),
            tf.betainc(0.1, b_s, x_s).eval(), rtol=tol, atol=tol)
        self.assertAllCloseAccordingToType(
            special.betainc(a_s, 0.1, x_s).astype(np_dt),
            tf.betainc(a_s, 0.1, x_s).eval(), rtol=tol, atol=tol)
        self.assertAllCloseAccordingToType(
            special.betainc(a_s, b_s, 0.1).astype(np_dt),
            tf.betainc(a_s, b_s, 0.1).eval(), rtol=tol, atol=tol)
        self.assertAllCloseAccordingToType(
            special.betainc(0.1, b_s, 0.1).astype(np_dt),
            tf.betainc(0.1, b_s, 0.1).eval(), rtol=tol, atol=tol)
        self.assertAllCloseAccordingToType(
            special.betainc(0.1, 0.1, 0.1).astype(np_dt),
            tf.betainc(0.1, 0.1, 0.1).eval(), rtol=tol, atol=tol)

      with self.assertRaisesRegexp(ValueError, "Shapes .* are not compatible"):
        tf.betainc(0.5, [0.5], [[0.5]])

      with self.test_session(use_gpu=self.use_gpu):
        with self.assertRaisesOpError("Shapes of .* are inconsistent"):
          a_p = tf.placeholder(dtype)
          b_p = tf.placeholder(dtype)
          x_p = tf.placeholder(dtype)
          tf.betainc(a_p, b_p, x_p).eval(
              feed_dict={a_p: 0.5, b_p: [0.5], x_p: [[0.5]]})

    except ImportError as e:
      tf.logging.warn("Cannot test special functions: %s" % str(e))
Ejemplo n.º 15
0
 def _cdf(self, x):
   if self.validate_args:
     x = distribution_util.embed_check_nonnegative_integer_form(x)
   return tf.betainc(self.total_count, 1. + x, tf.sigmoid(-self.logits))