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)
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]))
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)
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)
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]))
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]))
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)