コード例 #1
0
  def testBijectorForwardInverseWithHingeSoftnessEventDimsZero(self):
    with self.test_session():
      bijector = Softplus(event_ndims=0, hinge_softness=1.5)
      x = 2 * rng.randn(2, 10)
      y = 1.5 * self._softplus(x / 1.5)

      self.assertAllClose(y, bijector.forward(x).eval())
      self.assertAllClose(x, bijector.inverse(y).eval())
コード例 #2
0
  def testBijectorLogDetJacobianEventDimsOne(self):
    with self.test_session():
      bijector = Softplus(event_ndims=1)
      y = 2 * rng.rand(2, 10)
      ildj_before = self._softplus_ildj_before_reduction(y)
      ildj = np.sum(ildj_before, axis=1)

      self.assertAllClose(ildj, bijector.inverse_log_det_jacobian(y).eval())
コード例 #3
0
  def testBijectorLogDetJacobianEventDimsZero(self):
    with self.test_session():
      bijector = Softplus(event_ndims=0)
      y = 2 * rng.rand(2, 10)
      # No reduction needed if event_dims = 0.
      ildj = self._softplus_ildj_before_reduction(y)

      self.assertAllClose(ildj, bijector.inverse_log_det_jacobian(y).eval())
コード例 #4
0
  def testBijectorForwardInverseEventDimsOne(self):
    with self.test_session():
      bijector = Softplus(event_ndims=1)
      self.assertEqual("softplus", bijector.name)
      x = 2 * rng.randn(2, 10)
      y = self._softplus(x)

      self.assertAllClose(y, bijector.forward(x).eval())
      self.assertAllClose(x, bijector.inverse(y).eval())
コード例 #5
0
ファイル: softplus_test.py プロジェクト: sgcm520/tensorflow2
 def testBijectiveAndFiniteWithNegativeHingeSoftness32Bit(self):
     with self.test_session():
         bijector = Softplus(hinge_softness=-0.7)
         x = np.linspace(-20., 20., 100).astype(np.float32)
         y = -np.logspace(-10, 10, 100).astype(np.float32)
         assert_bijective_and_finite(bijector,
                                     x,
                                     y,
                                     event_ndims=0,
                                     rtol=1e-2,
                                     atol=1e-2)
コード例 #6
0
ファイル: softplus_test.py プロジェクト: sgcm520/tensorflow2
 def testBijectiveAndFinite32bit(self):
     with self.test_session():
         bijector = Softplus()
         x = np.linspace(-20., 20., 100).astype(np.float32)
         y = np.logspace(-10, 10, 100).astype(np.float32)
         assert_bijective_and_finite(bijector,
                                     x,
                                     y,
                                     event_ndims=0,
                                     rtol=1e-2,
                                     atol=1e-2)
コード例 #7
0
 def testBijector(self):
     with self.test_session():
         chain = Chain((Exp(event_ndims=1), Softplus(event_ndims=1)))
         self.assertEqual("chain_of_exp_of_softplus", chain.name)
         x = np.asarray([[[1., 2.], [2., 3.]]])
         self.assertAllClose(1. + np.exp(x), chain.forward(x).eval())
         self.assertAllClose(np.log(x - 1.), chain.inverse(x).eval())
         self.assertAllClose(-np.sum(np.log(x - 1.), axis=2),
                             chain.inverse_log_det_jacobian(x).eval())
         self.assertAllClose(np.sum(x, axis=2),
                             chain.forward_log_det_jacobian(x).eval())
コード例 #8
0
ファイル: softplus_test.py プロジェクト: sgcm520/tensorflow2
 def testBijectiveAndFinite16bit(self):
     with self.test_session():
         bijector = Softplus()
         # softplus(-20) is zero, so we can't use such a large range as in 32bit.
         x = np.linspace(-10., 20., 100).astype(np.float16)
         # Note that float16 is only in the open set (0, inf) for a smaller
         # logspace range.  The actual range was (-7, 4), so use something smaller
         # for the test.
         y = np.logspace(-6, 3, 100).astype(np.float16)
         assert_bijective_and_finite(bijector,
                                     x,
                                     y,
                                     event_ndims=0,
                                     rtol=1e-1,
                                     atol=1e-3)
コード例 #9
0
    def testMinEventNdimsChain(self):
        chain = Chain([Exp(), Exp(), Exp()])
        self.assertEqual(0, chain.forward_min_event_ndims)
        self.assertEqual(0, chain.inverse_min_event_ndims)

        chain = Chain([Affine(), Affine(), Affine()])
        self.assertEqual(1, chain.forward_min_event_ndims)
        self.assertEqual(1, chain.inverse_min_event_ndims)

        chain = Chain([Exp(), Affine()])
        self.assertEqual(1, chain.forward_min_event_ndims)
        self.assertEqual(1, chain.inverse_min_event_ndims)

        chain = Chain([Affine(), Exp()])
        self.assertEqual(1, chain.forward_min_event_ndims)
        self.assertEqual(1, chain.inverse_min_event_ndims)

        chain = Chain([Affine(), Exp(), Softplus(), Affine()])
        self.assertEqual(1, chain.forward_min_event_ndims)
        self.assertEqual(1, chain.inverse_min_event_ndims)
コード例 #10
0
ファイル: softplus_test.py プロジェクト: sgcm520/tensorflow2
 def testScalarCongruency(self):
     with self.test_session():
         bijector = Softplus()
         assert_scalar_congruency(bijector, lower_x=-2., upper_x=2.)
コード例 #11
0
ファイル: softplus_test.py プロジェクト: sgcm520/tensorflow2
 def testHingeSoftnessZeroRaises(self):
     with self.test_session():
         bijector = Softplus(hinge_softness=0., validate_args=True)
         with self.assertRaisesOpError("must be non-zero"):
             bijector.forward([1., 1.]).eval()
コード例 #12
0
ファイル: softplus_test.py プロジェクト: sgcm520/tensorflow2
 def testScalarCongruencyWithNegativeHingeSoftness(self):
     with self.test_session():
         bijector = Softplus(hinge_softness=-1.3)
         assert_scalar_congruency(bijector, lower_x=-2., upper_x=2.)
コード例 #13
0
 def testHingeSoftnessZeroRaises(self):
   with self.test_session():
     bijector = Softplus(event_ndims=0, hinge_softness=0., validate_args=True)
     with self.assertRaisesOpError("must be non-zero"):
       bijector.forward([1., 1.]).eval()
コード例 #14
0
 def testScalarCongruencyWithPositiveHingeSoftness(self):
     with self.test_session():
         bijector = Softplus(event_ndims=0, hinge_softness=1.3)
         assert_scalar_congruency(bijector, lower_x=-2., upper_x=2.)
コード例 #15
0
 def testHingeSoftnessZeroRaises(self):
     with self.cached_session():
         with self.assertRaisesWithPredicateMatch(
                 errors.InvalidArgumentError, "must be non-zero"):
             _ = Softplus(hinge_softness=0., validate_args=True)