-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
poisson_lognormal_test.py
168 lines (146 loc) · 6.39 KB
/
poisson_lognormal_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Copyright 2018 The TensorFlow Probability Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Tests for PoissonLogNormalQuadratureCompoundTest."""
import tensorflow.compat.v1 as tf1
import tensorflow.compat.v2 as tf
from tensorflow_probability.python.distributions import poisson_lognormal as pl
from tensorflow_probability.python.internal import test_util
class _PoissonLogNormalQuadratureCompoundTest(
test_util.DiscreteScalarDistributionTestHelpers):
"""Tests the PoissonLogNormalQuadratureCompoundTest distribution."""
def testSampleProbConsistent(self):
pln = pl.PoissonLogNormalQuadratureCompound(
loc=tf1.placeholder_with_default(
-2., shape=[] if self.static_shape else None),
scale=tf1.placeholder_with_default(
1.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=1, rtol=0.1)
def testMeanVariance(self):
pln = pl.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.03)
def testSampleProbConsistentBroadcastScalar(self):
pln = pl.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)
# JAX needs more samples to get good agreement.
self.run_test_sample_consistent_log_prob(
self.evaluate, pln, batch_size=2,
num_samples=int(1e6), num_threshold=int(1e4),
rtol=0.1, atol=0.01)
def testMeanVarianceBroadcastScalar(self):
pln = pl.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_mean_variance(
self.evaluate, pln, rtol=0.1, atol=0.01)
def testSampleProbConsistentBroadcastBoth(self):
pln = pl.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_log_prob(
self.evaluate, pln, batch_size=4, rtol=0.1, atol=0.08)
def testMeanVarianceBroadcastBoth(self):
pln = pl.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)
@test_util.tf_tape_safety_test
def testGradientThroughParams(self):
pln = pl.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]))
@test_util.tf_tape_safety_test
def testGradientThroughNonVariableParams(self):
pln = pl.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 testAssertValidSample(self):
pln = pl.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 testPdfBoundary(self):
pln = pl.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)
@test_util.test_all_tf_execution_regimes
class PoissonLogNormalQuadratureCompoundStaticShapeTest(
_PoissonLogNormalQuadratureCompoundTest, test_util.TestCase):
@property
def static_shape(self):
return True
@test_util.test_all_tf_execution_regimes
class PoissonLogNormalQuadratureCompoundDynamicShapeTest(
_PoissonLogNormalQuadratureCompoundTest, test_util.TestCase):
@property
def static_shape(self):
return False
if __name__ == '__main__':
test_util.main()