Ejemplo n.º 1
0
    def testBatchMultivariateIdentity(self):
        with self.cached_session() as sess:
            placeholder = array_ops.placeholder(dtypes.float32, name="x")

            def static_run(fun, x, **kwargs):
                return fun(x, **kwargs).eval()

            def dynamic_run(fun, x_value, **kwargs):
                x_value = np.array(x_value)
                return sess.run(fun(placeholder, **kwargs),
                                feed_dict={placeholder: x_value})

            for run in (static_run, dynamic_run):
                mu = [[1., -1]]
                # Corresponds to 1 2x2 matrix, with twos on the diagonal.
                scale = 2.
                bijector = Affine(shift=mu, scale_identity_multiplier=scale)
                x = [[[1., 1]]]
                self.assertAllClose([[[3., 1]]], run(bijector.forward, x))
                self.assertAllClose([[[0., 1]]], run(bijector.inverse, x))
                self.assertAllClose(
                    -np.log(4),
                    run(bijector.inverse_log_det_jacobian, x, event_ndims=1))
Ejemplo n.º 2
0
  def testDiagWithTriL(self):
    with self.cached_session() as sess:
      placeholder = array_ops.placeholder(dtypes.float32, name="x")

      def static_run(fun, x, **kwargs):
        return fun(x, **kwargs).eval()

      def dynamic_run(fun, x_value, **kwargs):
        x_value = np.array(x_value)
        return sess.run(
            fun(placeholder, **kwargs), feed_dict={placeholder: x_value})

      for run in (static_run, dynamic_run):
        mu = -1.
        # scale = [[2., 0], [2, 3]]
        bijector = Affine(
            shift=mu, scale_diag=[1., 2.], scale_tril=[[1., 0], [2., 1]])
        x = [[1., 2]]  # One multivariate sample.
        self.assertAllClose([[1., 7]], run(bijector.forward, x))
        self.assertAllClose([[1., 1 / 3.]], run(bijector.inverse, x))
        self.assertAllClose(
            -np.log(6.),
            run(bijector.inverse_log_det_jacobian, x, event_ndims=1))
Ejemplo n.º 3
0
  def testBatchMultivariateFullDynamic(self):
    with self.cached_session() as sess:
      x = array_ops.placeholder(dtypes.float32, name="x")
      mu = array_ops.placeholder(dtypes.float32, name="mu")
      scale_diag = array_ops.placeholder(dtypes.float32, name="scale_diag")

      x_value = np.array([[[1., 1]]], dtype=np.float32)
      mu_value = np.array([[1., -1]], dtype=np.float32)
      scale_diag_value = np.array([[2., 2]], dtype=np.float32)

      feed_dict = {
          x: x_value,
          mu: mu_value,
          scale_diag: scale_diag_value,
      }

      bijector = Affine(shift=mu, scale_diag=scale_diag)
      self.assertAllClose([[[3., 1]]], sess.run(bijector.forward(x), feed_dict))
      self.assertAllClose([[[0., 1]]], sess.run(bijector.inverse(x), feed_dict))
      self.assertAllClose(
          [-np.log(4)],
          sess.run(bijector.inverse_log_det_jacobian(
              x, event_ndims=1), feed_dict))
    def testIdentityWithDiagUpdate(self):
        with self.test_session() as sess:

            def static_run(fun, x):
                return fun(x).eval()

            def dynamic_run(fun, x_value):
                x_value = np.array(x_value)
                x = array_ops.placeholder(dtypes.float32, name="x")
                return sess.run(fun(x), feed_dict={x: x_value})

            for run in (static_run, dynamic_run):
                mu = -1.
                # Corresponds to scale = 2
                bijector = Affine(shift=mu,
                                  scale_identity_multiplier=1.,
                                  scale_diag=[1.],
                                  event_ndims=0)
                self.assertEqual(0, bijector.event_ndims.eval())  # "is vector"
                x = [1., 2, 3]  # Three scalar samples (no batches).
                self.assertAllClose([1., 3, 5], run(bijector.forward, x))
                self.assertAllClose([1., 1.5, 2.], run(bijector.inverse, x))
                self.assertAllClose(-np.log(2.),
                                    run(bijector.inverse_log_det_jacobian, x))
Ejemplo n.º 5
0
  def testIdentityWithTriL(self):
    with self.test_session() as sess:

      def static_run(fun, x):
        return fun(x).eval()

      def dynamic_run(fun, x_value):
        x_value = np.array(x_value)
        x = array_ops.placeholder(dtypes.float32, name="x")
        return sess.run(fun(x), feed_dict={x: x_value})

      for run in (static_run, dynamic_run):
        mu = -1.
        # scale = [[2., 0], [2, 2]]
        bijector = Affine(
            shift=mu,
            scale_identity_multiplier=1.,
            scale_tril=[[1., 0], [2., 1]])
        self.assertEqual(1, bijector.event_ndims.eval())  # "is vector"
        x = [[1., 2]]  # One multivariate sample.
        self.assertAllClose([[1., 5]], run(bijector.forward, x))
        self.assertAllClose([[1., 0.5]], run(bijector.inverse, x))
        self.assertAllClose(-np.log(4.),
                            run(bijector.inverse_log_det_jacobian, x))
Ejemplo n.º 6
0
    def testTwoBatchScalarIdentityViaDiagMultiplier(self):
        with self.test_session() as sess:

            def static_run(fun, x):
                return fun(x).eval()

            def dynamic_run(fun, x_value):
                x_value = np.array(x_value)
                x = array_ops.placeholder(dtypes.float32, name="x")
                return sess.run(fun(x), feed_dict={x: x_value})

            for run in (static_run, dynamic_run):
                mu = [1., -1]
                # Univariate, two batches.
                # Corresponds to scale = 1.
                bijector = Affine(shift=mu,
                                  scale_identity_multiplier=1.,
                                  event_ndims=0)
                self.assertEqual(0, bijector.event_ndims.eval())  # "is scalar"
                x = [1., 1]  # One sample from each of two batches.
                self.assertAllClose([2., 0], run(bijector.forward, x))
                self.assertAllClose([0., 2], run(bijector.inverse, x))
                self.assertAllClose(0.,
                                    run(bijector.inverse_log_det_jacobian, x))
 def testScalePropertyAssertsCorrectly(self):
     with self.test_session():
         with self.assertRaises(NotImplementedError):
             scale = Affine(  # pylint:disable=unused-variable
                 scale_tril=[[1., 0], [2, 1]],
                 scale_perturb_factor=[2., 1.]).scale
 def testScalarCongruency(self):
     with self.test_session():
         bijector = Affine(shift=3.6,
                           scale_identity_multiplier=0.42,
                           event_ndims=0)
         assert_scalar_congruency(bijector, lower_x=-2., upper_x=2.)
 def testEventNdimsLargerThanOneRaises(self):
     with self.test_session():
         mu = [1., -1]
         # Scale corresponds to 2x2 identity matrix.
         bijector = Affine(shift=mu, event_ndims=2, validate_args=True)
         bijector.forward([1., 1.]).eval()
 def testProperties(self):
     with self.test_session():
         mu = -1.
         # scale corresponds to 1.
         bijector = Affine(shift=mu, event_ndims=0)
         self.assertEqual("affine", bijector.name)
Ejemplo n.º 11
0
 def testScaleDiagAndEventNdimsZeroRaises(self):
   # Check Diag matrix with zero scaling.
   with self.assertRaisesRegexp(ValueError, "only scale argument"):
     Affine(shift=None, scale_diag=[0.0], event_ndims=0, validate_args=True)
Ejemplo n.º 12
0
 def testScalarEventIdentityScale(self):
     with self.test_session() as sess:
         doubler = Affine(scale_identity_multiplier=2., event_ndims=0)
         doubler2 = doubler.inverse_log_det_jacobian(2.)
         doubler2_ildj_ = sess.run([doubler2])
         self.assertAllClose([-np.log(2.)], doubler2_ildj_)
Ejemplo n.º 13
0
 def testProperties(self):
     with self.cached_session():
         mu = -1.
         # scale corresponds to 1.
         bijector = Affine(shift=mu)
         self.assertEqual("affine", bijector.name)