Exemplo n.º 1
0
 def testBijector(self):
   with self.test_session():
     self.assertEqual("sigmoid", Sigmoid().name)
     x = np.linspace(-10., 10., 100).reshape([2, 5, 10]).astype(np.float32)
     y = special.expit(x)
     ildj = -np.log(y) - np.log1p(-y)
     self.assertAllClose(y, Sigmoid().forward(x).eval(), atol=0., rtol=1e-2)
     self.assertAllClose(x, Sigmoid().inverse(y).eval(), atol=0., rtol=1e-4)
     self.assertAllClose(ildj, Sigmoid().inverse_log_det_jacobian(y).eval(),
                         atol=0., rtol=1e-6)
     self.assertAllClose(-ildj, Sigmoid().forward_log_det_jacobian(x).eval(),
                         atol=0., rtol=1e-4)
Exemplo n.º 2
0
 def testBijector(self):
   with self.cached_session():
     self.assertEqual("sigmoid", Sigmoid().name)
     x = np.linspace(-10., 10., 100).reshape([2, 5, 10]).astype(np.float32)
     y = special.expit(x)
     ildj = -np.log(y) - np.log1p(-y)
     bijector = Sigmoid()
     self.assertAllClose(y, bijector.forward(x).eval(), atol=0., rtol=1e-2)
     self.assertAllClose(x, bijector.inverse(y).eval(), atol=0., rtol=1e-4)
     self.assertAllClose(ildj, bijector.inverse_log_det_jacobian(
         y, event_ndims=0).eval(), atol=0., rtol=1e-6)
     self.assertAllClose(-ildj, bijector.forward_log_det_jacobian(
         x, event_ndims=0).eval(), atol=0., rtol=1e-4)
Exemplo n.º 3
0
 def testBijectiveAndFinite(self):
     with self.cached_session():
         x = np.linspace(-7., 7., 100).astype(np.float32)
         eps = 1e-3
         y = np.linspace(eps, 1. - eps, 100).astype(np.float32)
         assert_bijective_and_finite(Sigmoid(),
                                     x,
                                     y,
                                     event_ndims=0,
                                     atol=0.,
                                     rtol=1e-4)
    def __init__(self,
                 temperature,
                 logits=None,
                 probs=None,
                 validate_args=False,
                 allow_nan_stats=True,
                 name="RelaxedBernoulli"):
        """Construct RelaxedBernoulli distributions.

    Args:
      temperature: An 0-D `Tensor`, representing the temperature
        of a set of RelaxedBernoulli distributions. The temperature should be
        positive.
      logits: An N-D `Tensor` representing the log-odds
        of a positive event. Each entry in the `Tensor` parametrizes
        an independent RelaxedBernoulli distribution where the probability of an
        event is sigmoid(logits). Only one of `logits` or `probs` should be
        passed in.
      probs: An N-D `Tensor` representing the probability of a positive event.
        Each entry in the `Tensor` parameterizes an independent Bernoulli
        distribution. Only one of `logits` or `probs` should be passed in.
      validate_args: Python `bool`, default `False`. When `True` distribution
        parameters are checked for validity despite possibly degrading runtime
        performance. When `False` invalid inputs may silently render incorrect
        outputs.
      allow_nan_stats: Python `bool`, default `True`. When `True`, statistics
        (e.g., mean, mode, variance) use the value "`NaN`" to indicate the
        result is undefined. When `False`, an exception is raised if one or
        more of the statistic's batch members are undefined.
      name: Python `str` name prefixed to Ops created by this class.

    Raises:
      ValueError: If both `probs` and `logits` are passed, or if neither.
    """
        parameters = dict(locals())
        with ops.name_scope(name, values=[logits, probs, temperature]) as name:
            with ops.control_dependencies(
                [check_ops.assert_positive(temperature
                                           )] if validate_args else []):
                self._temperature = array_ops.identity(temperature,
                                                       name="temperature")
            self._logits, self._probs = distribution_util.get_logits_and_probs(
                logits=logits, probs=probs, validate_args=validate_args)
            super(RelaxedBernoulli,
                  self).__init__(distribution=logistic.Logistic(
                      self._logits / self._temperature,
                      1. / self._temperature,
                      validate_args=validate_args,
                      allow_nan_stats=allow_nan_stats,
                      name=name + "/Logistic"),
                                 bijector=Sigmoid(validate_args=validate_args),
                                 validate_args=validate_args,
                                 name=name)
        self._parameters = parameters
Exemplo n.º 5
0
 def testScalarCongruency(self):
     with self.cached_session():
         assert_scalar_congruency(Sigmoid(), lower_x=-7., upper_x=7.)