예제 #1
0
 def testMeanVariance(self):
   pln = tfd.PoissonLogNormalQuadratureCompound(
       loc=tf1.placeholder_with_default(
           0., shape=[] if self.static_shape else None),
       scale=tf1.placeholder_with_default(
           1., shape=[] if self.static_shape else None),
       quadrature_size=10,
       validate_args=True)
   self.run_test_sample_consistent_mean_variance(self.evaluate, pln, rtol=0.02)
예제 #2
0
 def testAssertValidSample(self):
     pln = tfd.PoissonLogNormalQuadratureCompound(
         loc=tf1.placeholder_with_default(
             0., shape=[] if self.static_shape else None),
         scale=tf1.placeholder_with_default(
             1., shape=[] if self.static_shape else None),
         quadrature_size=10,
         validate_args=True)
     with self.assertRaisesOpError('Sample must be non-negative.'):
         self.evaluate(pln.log_prob([-1.2, 3., 4.2]))
예제 #3
0
 def testMeanVarianceBroadcastBoth(self):
   pln = tfd.PoissonLogNormalQuadratureCompound(
       loc=tf1.placeholder_with_default(
           [[0.], [-0.5]], shape=[2, 1] if self.static_shape else None),
       scale=tf1.placeholder_with_default(
           [[1., 0.9]], shape=[1, 2] if self.static_shape else None),
       quadrature_size=10,
       validate_args=True)
   self.run_test_sample_consistent_mean_variance(
       self.evaluate, pln, rtol=0.1, atol=0.01)
예제 #4
0
 def testSampleProbConsistentBroadcastScalar(self):
   pln = tfd.PoissonLogNormalQuadratureCompound(
       loc=tf1.placeholder_with_default(
           [0., -0.5], shape=[2] if self.static_shape else None),
       scale=tf1.placeholder_with_default(
           1., shape=[] if self.static_shape else None),
       quadrature_size=10,
       validate_args=True)
   self.run_test_sample_consistent_log_prob(
       self.evaluate, pln, batch_size=2, rtol=0.1, atol=0.01)
예제 #5
0
 def testGradientThroughParams(self):
   pln = tfd.PoissonLogNormalQuadratureCompound(
       loc=tf.Variable([0., -0.5], shape=[2] if self.static_shape
                       else None),
       scale=tf.Variable([1., 0.9], shape=[2] if self.static_shape
                         else None),
       quadrature_size=10, validate_args=True)
   with tf.GradientTape() as tape:
     loss = -pln.log_prob([1., 2.])
   grad = tape.gradient(loss, pln.trainable_variables)
   self.assertLen(grad, 2)
   self.assertFalse(any([g is None for g in grad]))
예제 #6
0
 def testGradientThroughNonVariableParams(self):
   pln = tfd.PoissonLogNormalQuadratureCompound(
       loc=tf.convert_to_tensor([0., -0.5]),
       scale=tf.convert_to_tensor([1., 0.9]),
       quadrature_size=10, validate_args=True)
   with tf.GradientTape() as tape:
     tape.watch(pln.loc)
     tape.watch(pln.scale)
     loss = -pln.log_prob([1., 2.])
   grad = tape.gradient(loss, [pln.loc, pln.scale])
   self.assertLen(grad, 2)
   self.assertFalse(any([g is None for g in grad]))
예제 #7
0
    def testPdfBoundary(self):
        pln = tfd.PoissonLogNormalQuadratureCompound(
            loc=tf1.placeholder_with_default(
                0., shape=[] if self.static_shape else None),
            scale=tf1.placeholder_with_default(
                1., shape=[] if self.static_shape else None),
            quadrature_size=10,
            validate_args=True)

        pdf = self.evaluate(pln.prob(0.))
        log_pdf = self.evaluate(pln.log_prob(0.))
        self.assertAllFinite(pdf)
        self.assertAllFinite(log_pdf)