|
| 1 | +# Copyright 2015 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +"""Tests for Uniform distribution.""" |
| 16 | + |
| 17 | +from __future__ import absolute_import |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +import numpy as np |
| 22 | +import tensorflow as tf |
| 23 | + |
| 24 | + |
| 25 | +class UniformTest(tf.test.TestCase): |
| 26 | + |
| 27 | + def testUniformRange(self): |
| 28 | + with self.test_session(): |
| 29 | + a = 3.0 |
| 30 | + b = 10.0 |
| 31 | + uniform = tf.contrib.distributions.Uniform(a=a, b=b) |
| 32 | + self.assertAllClose(a, uniform.a.eval()) |
| 33 | + self.assertAllClose(b, uniform.b.eval()) |
| 34 | + self.assertAllClose(b - a, uniform.range.eval()) |
| 35 | + |
| 36 | + def testUniformPDF(self): |
| 37 | + with self.test_session(): |
| 38 | + a = tf.constant([-3.0] * 5 + [15.0]) |
| 39 | + b = tf.constant([11.0] * 5 + [20.0]) |
| 40 | + uniform = tf.contrib.distributions.Uniform(a=a, b=b) |
| 41 | + |
| 42 | + a_v = -3.0 |
| 43 | + b_v = 11.0 |
| 44 | + x = np.array([-10.5, 4.0, 0.0, 10.99, 11.3, 17.0], dtype=np.float32) |
| 45 | + |
| 46 | + def _expected_pdf(): |
| 47 | + pdf = np.zeros_like(x) + 1.0 / (b_v - a_v) |
| 48 | + pdf[x > b_v] = 0.0 |
| 49 | + pdf[x < a_v] = 0.0 |
| 50 | + pdf[5] = 1.0 / (20.0 - 15.0) |
| 51 | + return pdf |
| 52 | + |
| 53 | + expected_pdf = _expected_pdf() |
| 54 | + |
| 55 | + pdf = uniform.pdf(x) |
| 56 | + self.assertAllClose(expected_pdf, pdf.eval()) |
| 57 | + |
| 58 | + log_pdf = uniform.log_pdf(x) |
| 59 | + self.assertAllClose(np.log(expected_pdf), log_pdf.eval()) |
| 60 | + |
| 61 | + def testUniformShape(self): |
| 62 | + with self.test_session(): |
| 63 | + a = tf.constant([-3.0] * 5) |
| 64 | + b = tf.constant(11.0) |
| 65 | + uniform = tf.contrib.distributions.Uniform(a=a, b=b) |
| 66 | + |
| 67 | + self.assertEqual(uniform.batch_shape().eval(), (5,)) |
| 68 | + self.assertEqual(uniform.get_batch_shape(), tf.TensorShape([5])) |
| 69 | + self.assertEqual(uniform.event_shape().eval(), 1) |
| 70 | + self.assertEqual(uniform.get_event_shape(), tf.TensorShape([])) |
| 71 | + |
| 72 | + def testUniformPDFWithScalarEndpoint(self): |
| 73 | + with self.test_session(): |
| 74 | + a = tf.constant([0.0, 5.0]) |
| 75 | + b = tf.constant(10.0) |
| 76 | + uniform = tf.contrib.distributions.Uniform(a=a, b=b) |
| 77 | + |
| 78 | + x = np.array([0.0, 8.0], dtype=np.float32) |
| 79 | + expected_pdf = np.array([1.0 / (10.0 - 0.0), 1.0 / (10.0 - 5.0)]) |
| 80 | + |
| 81 | + pdf = uniform.pdf(x) |
| 82 | + self.assertAllClose(expected_pdf, pdf.eval()) |
| 83 | + |
| 84 | + def testUniformCDF(self): |
| 85 | + with self.test_session(): |
| 86 | + batch_size = 6 |
| 87 | + a = tf.constant([1.0] * batch_size) |
| 88 | + b = tf.constant([11.0] * batch_size) |
| 89 | + a_v = 1.0 |
| 90 | + b_v = 11.0 |
| 91 | + x = np.array([-2.5, 2.5, 4.0, 0.0, 10.99, 12.0], dtype=np.float32) |
| 92 | + |
| 93 | + uniform = tf.contrib.distributions.Uniform(a=a, b=b) |
| 94 | + |
| 95 | + def _expected_cdf(): |
| 96 | + cdf = (x - a_v) / (b_v - a_v) |
| 97 | + cdf[x >= b_v] = 1 |
| 98 | + cdf[x < a_v] = 0 |
| 99 | + return cdf |
| 100 | + |
| 101 | + cdf = uniform.cdf(x) |
| 102 | + self.assertAllClose(_expected_cdf(), cdf.eval()) |
| 103 | + |
| 104 | + log_cdf = uniform.log_cdf(x) |
| 105 | + self.assertAllClose(np.log(_expected_cdf()), log_cdf.eval()) |
| 106 | + |
| 107 | + def testUniformEntropy(self): |
| 108 | + with self.test_session(): |
| 109 | + a_v = np.array([1.0, 1.0, 1.0]) |
| 110 | + b_v = np.array([[1.5, 2.0, 3.0]]) |
| 111 | + uniform = tf.contrib.distributions.Uniform(a=a_v, b=b_v) |
| 112 | + |
| 113 | + expected_entropy = np.log(b_v - a_v) |
| 114 | + self.assertAllClose(expected_entropy, uniform.entropy().eval()) |
| 115 | + |
| 116 | + def testUniformAssertMaxGtMin(self): |
| 117 | + with self.test_session(): |
| 118 | + a_v = np.array([1.0, 1.0, 1.0], dtype=np.float32) |
| 119 | + b_v = np.array([1.0, 2.0, 3.0], dtype=np.float32) |
| 120 | + uniform = tf.contrib.distributions.Uniform(a=a_v, b=b_v) |
| 121 | + |
| 122 | + with self.assertRaisesWithPredicateMatch(tf.errors.InvalidArgumentError, |
| 123 | + "x < y"): |
| 124 | + uniform.a.eval() |
| 125 | + |
| 126 | + def testUniformSample(self): |
| 127 | + with self.test_session(): |
| 128 | + a = tf.constant([3.0, 4.0]) |
| 129 | + b = tf.constant(13.0) |
| 130 | + a1_v = 3.0 |
| 131 | + a2_v = 4.0 |
| 132 | + b_v = 13.0 |
| 133 | + n = tf.constant(100000) |
| 134 | + uniform = tf.contrib.distributions.Uniform(a=a, b=b) |
| 135 | + |
| 136 | + samples = uniform.sample(n, seed=137) |
| 137 | + sample_values = samples.eval() |
| 138 | + self.assertEqual(sample_values.shape, (100000, 2)) |
| 139 | + self.assertAllClose(sample_values[::, 0].mean(), (b_v + a1_v) / 2, |
| 140 | + atol=1e-2) |
| 141 | + self.assertAllClose(sample_values[::, 1].mean(), (b_v + a2_v) / 2, |
| 142 | + atol=1e-2) |
| 143 | + self.assertFalse(np.any(sample_values[::, 0] < a1_v) or np.any( |
| 144 | + sample_values >= b_v)) |
| 145 | + self.assertFalse(np.any(sample_values[::, 1] < a2_v) or np.any( |
| 146 | + sample_values >= b_v)) |
| 147 | + |
| 148 | + def testUniformSampleMultiDimensional(self): |
| 149 | + with self.test_session(): |
| 150 | + batch_size = 2 |
| 151 | + a_v = [3.0, 22.0] |
| 152 | + b_v = [13.0, 35.0] |
| 153 | + a = tf.constant([a_v] * batch_size) |
| 154 | + b = tf.constant([b_v] * batch_size) |
| 155 | + |
| 156 | + uniform = tf.contrib.distributions.Uniform(a=a, b=b) |
| 157 | + |
| 158 | + n_v = 100000 |
| 159 | + n = tf.constant(n_v) |
| 160 | + samples = uniform.sample(n, seed=138) |
| 161 | + self.assertEqual(samples.get_shape(), (n_v, batch_size, 2)) |
| 162 | + |
| 163 | + sample_values = samples.eval() |
| 164 | + |
| 165 | + self.assertFalse(np.any(sample_values[:, 0, 0] < a_v[0]) or np.any( |
| 166 | + sample_values[:, 0, 0] >= b_v[0])) |
| 167 | + self.assertFalse(np.any(sample_values[:, 0, 1] < a_v[1]) or np.any( |
| 168 | + sample_values[:, 0, 1] >= b_v[1])) |
| 169 | + |
| 170 | + self.assertAllClose(sample_values[:, 0, 0].mean(), (a_v[0] + b_v[0]) / 2, |
| 171 | + atol=1e-2) |
| 172 | + self.assertAllClose(sample_values[:, 0, 1].mean(), (a_v[1] + b_v[1]) / 2, |
| 173 | + atol=1e-2) |
| 174 | + |
| 175 | + def testUniformMeanAndVariance(self): |
| 176 | + with self.test_session(): |
| 177 | + a = 10.0 |
| 178 | + b = 100.0 |
| 179 | + uniform = tf.contrib.distributions.Uniform(a=a, b=b) |
| 180 | + self.assertAllClose(uniform.variance.eval(), (b - a)**2 / 12) |
| 181 | + self.assertAllClose(uniform.mean.eval(), (b + a) / 2) |
| 182 | + |
| 183 | + def testUniformNans(self): |
| 184 | + with self.test_session(): |
| 185 | + a = 10.0 |
| 186 | + b = [11.0, 100.0] |
| 187 | + uniform = tf.contrib.distributions.Uniform(a=a, b=b) |
| 188 | + |
| 189 | + no_nans = tf.constant(1.0) |
| 190 | + nans = tf.constant(0.0) / tf.constant(0.0) |
| 191 | + self.assertTrue(tf.is_nan(nans).eval()) |
| 192 | + with_nans = tf.pack([no_nans, nans]) |
| 193 | + |
| 194 | + pdf = uniform.pdf(with_nans) |
| 195 | + |
| 196 | + is_nan = tf.is_nan(pdf).eval() |
| 197 | + print(pdf.eval()) |
| 198 | + self.assertFalse(is_nan[0]) |
| 199 | + self.assertTrue(is_nan[1]) |
| 200 | + |
| 201 | + def testUniformSamplePdf(self): |
| 202 | + with self.test_session(): |
| 203 | + a = 10.0 |
| 204 | + b = [11.0, 100.0] |
| 205 | + uniform = tf.contrib.distributions.Uniform(a, b) |
| 206 | + self.assertTrue(tf.reduce_all(uniform.pdf(uniform.sample(10)) > 0).eval()) |
| 207 | + |
| 208 | + def testUniformBroadcasting(self): |
| 209 | + with self.test_session(): |
| 210 | + a = 10.0 |
| 211 | + b = [11.0, 20.0] |
| 212 | + uniform = tf.contrib.distributions.Uniform(a, b) |
| 213 | + |
| 214 | + pdf = uniform.pdf([[10.5, 11.5], [9.0, 19.0], [10.5, 21.0]]) |
| 215 | + expected_pdf = np.array([[1.0, 0.1], [0.0, 0.1], [1.0, 0.0]]) |
| 216 | + self.assertAllClose(expected_pdf, pdf.eval()) |
| 217 | + |
| 218 | + |
| 219 | +if __name__ == "__main__": |
| 220 | + tf.test.main() |
0 commit comments