def testBijector(self): with self.test_session(): for fwd in [ bijectors.Identity(), bijectors.Exp(event_ndims=1), bijectors.Affine(shift=[0., 1.], scale_diag=[2., 3.], event_ndims=1), bijectors.Softplus(event_ndims=1), bijectors.SoftmaxCentered(event_ndims=1), bijectors.SigmoidCentered(), ]: rev = bijectors.Invert(fwd) self.assertEqual("_".join(["invert", fwd.name]), rev.name) x = [[[1., 2.], [2., 3.]]] self.assertAllClose( fwd.inverse(x).eval(), rev.forward(x).eval()) self.assertAllClose( fwd.forward(x).eval(), rev.inverse(x).eval()) self.assertAllClose( fwd.forward_log_det_jacobian(x).eval(), rev.inverse_log_det_jacobian(x).eval()) self.assertAllClose( fwd.inverse_log_det_jacobian(x).eval(), rev.forward_log_det_jacobian(x).eval())
def testDistWithBatchShapeOneThenTransformedThroughSoftplus(self): # This complex combination of events resulted in a loss of static shape # information when tensor_util.constant_value(self._needs_rotation) was # being used incorrectly (resulting in always rotating). # Batch shape = [1], event shape = [3] mu = array_ops.zeros((1, 3)) diag = array_ops.ones((1, 3)) with self.cached_session(): base_dist = ds.MultivariateNormalDiag(mu, diag, validate_args=True) dist = ds.TransformedDistribution(base_dist, validate_args=True, bijector=bijectors.Softplus()) samps = dist.sample(5) # Shape [5, 1, 3]. self.assertAllEqual([5, 1], dist.log_prob(samps).get_shape())
def testInvertible(self): # Generate random inputs from an unconstrained space, with # event size 6 to specify 3x3 triangular matrices. batch_shape = [2, 1] x = np.float32(self._rng.randn(*(batch_shape + [6]))) b = bijectors.ScaleTriL(diag_bijector=bijectors.Softplus(), diag_shift=3.14159) y = self.evaluate(b.forward(x)) self.assertAllEqual(y.shape, batch_shape + [3, 3]) x_ = self.evaluate(b.inverse(y)) self.assertAllClose(x, x_, rtol=1e-4) fldj = self.evaluate(b.forward_log_det_jacobian(x, event_ndims=1)) ildj = self.evaluate(b.inverse_log_det_jacobian(y, event_ndims=2)) self.assertAllClose(fldj, -ildj, rtol=1e-4)