Esempio n. 1
0
    def testGetAndMakeFromParameters(self):
        one = tf.constant(1.0)
        d = tfp.distributions.Normal(loc=one, scale=3.0, validate_args=True)
        d = tfp.bijectors.Tanh()(d)
        d = tfp.bijectors.Tanh()(d)
        p = utils.get_parameters(d)

        expected_p = utils.Params(
            tfp.distributions.TransformedDistribution,
            params={
                'bijector':
                utils.Params(tfp.bijectors.Chain,
                             params={
                                 'bijectors': [
                                     utils.Params(tfp.bijectors.Tanh,
                                                  params={}),
                                     utils.Params(tfp.bijectors.Tanh,
                                                  params={}),
                                 ]
                             }),
                'distribution':
                utils.Params(tfp.distributions.Normal,
                             params={
                                 'validate_args': True,
                                 'scale': 3.0,
                                 'loc': one
                             })
            })

        self.compare_params(p, expected_p)

        d_recreated = utils.make_from_parameters(p)
        points = [0.01, 0.25, 0.5, 0.75, 0.99]
        self.assertAllClose(d.log_prob(points), d_recreated.log_prob(points))
Esempio n. 2
0
    def testGetAndMakeNontrivialBijectorFromParameters(self):
        scale_matrix = tf.Variable([[1.0, 2.0], [-1.0, 0.0]])
        d = tfp.distributions.MultivariateNormalDiag(loc=[1.0, 1.0],
                                                     scale_diag=[2.0, 3.0],
                                                     validate_args=True)
        b = tfp.bijectors.ScaleMatvecLinearOperator(
            scale=tf.linalg.LinearOperatorFullMatrix(matrix=scale_matrix),
            adjoint=True)
        b_d = b(d)
        p = utils.get_parameters(b_d)

        expected_p = utils.Params(
            tfp.distributions.TransformedDistribution,
            params={
                'bijector':
                utils.Params(tfp.bijectors.ScaleMatvecLinearOperator,
                             params={
                                 'adjoint':
                                 True,
                                 'scale':
                                 utils.Params(
                                     tf.linalg.LinearOperatorFullMatrix,
                                     params={'matrix': scale_matrix})
                             }),
                'distribution':
                utils.Params(tfp.distributions.MultivariateNormalDiag,
                             params={
                                 'validate_args': True,
                                 'scale_diag': [2.0, 3.0],
                                 'loc': [1.0, 1.0]
                             })
            })

        self.compare_params(p, expected_p)

        b_d_recreated = utils.make_from_parameters(p)

        points = [[-1.0, -2.0], [0.0, 0.0], [3.0, -5.0], [5.0, 5.0],
                  [1.0, np.inf], [-np.inf, 0.0]]
        self.assertAllClose(b_d.log_prob(points),
                            b_d_recreated.log_prob(points))
Esempio n. 3
0
 def merge_and_convert(spec, params):
     return distribution_utils.make_from_parameters(
         distribution_utils.merge_to_parameters_from_dict(
             spec.parameters, params))