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())
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())
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())
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())
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)
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)
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())
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)
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)
def testScalarCongruency(self): with self.test_session(): bijector = Softplus() assert_scalar_congruency(bijector, lower_x=-2., upper_x=2.)
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()
def testScalarCongruencyWithNegativeHingeSoftness(self): with self.test_session(): bijector = Softplus(hinge_softness=-1.3) assert_scalar_congruency(bijector, lower_x=-2., upper_x=2.)
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()
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.)
def testHingeSoftnessZeroRaises(self): with self.cached_session(): with self.assertRaisesWithPredicateMatch( errors.InvalidArgumentError, "must be non-zero"): _ = Softplus(hinge_softness=0., validate_args=True)