|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2021 The Edward2 Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Tests for utils.""" |
| 17 | +from absl.testing import absltest |
| 18 | +from absl.testing import parameterized |
| 19 | + |
| 20 | +import edward2.jax as ed |
| 21 | + |
| 22 | +import jax |
| 23 | +import jax.numpy as jnp |
| 24 | + |
| 25 | +import numpy as np |
| 26 | +import tensorflow as tf |
| 27 | + |
| 28 | + |
| 29 | +class MeanFieldLogitsTest(parameterized.TestCase, tf.test.TestCase): |
| 30 | + |
| 31 | + def testMeanFieldLogitsLikelihood(self): |
| 32 | + """Tests if scaling is correct under different likelihood.""" |
| 33 | + batch_size = 10 |
| 34 | + num_classes = 12 |
| 35 | + variance = 1.5 |
| 36 | + mean_field_factor = 2. |
| 37 | + |
| 38 | + rng_key = jax.random.PRNGKey(0) |
| 39 | + logits = jax.random.normal(rng_key, (batch_size, num_classes)) |
| 40 | + covmat = jnp.ones(batch_size) * variance |
| 41 | + |
| 42 | + logits_logistic = ed.nn.utils.mean_field_logits( |
| 43 | + logits, covmat, mean_field_factor=mean_field_factor) |
| 44 | + logits_poisson = ed.nn.utils.mean_field_logits( |
| 45 | + logits, |
| 46 | + covmat, |
| 47 | + mean_field_factor=mean_field_factor, |
| 48 | + likelihood='poisson') |
| 49 | + |
| 50 | + self.assertAllClose(logits_logistic, logits / 2., atol=1e-4) |
| 51 | + self.assertAllClose(logits_poisson, logits * np.exp(1.5), atol=1e-4) |
| 52 | + |
| 53 | + def testMeanFieldLogitsTemperatureScaling(self): |
| 54 | + """Tests using mean_field_logits as temperature scaling method.""" |
| 55 | + batch_size = 10 |
| 56 | + num_classes = 12 |
| 57 | + |
| 58 | + rng_key = jax.random.PRNGKey(0) |
| 59 | + logits = jax.random.normal(rng_key, (batch_size, num_classes)) |
| 60 | + |
| 61 | + # Test if there's no change to logits when mean_field_factor < 0. |
| 62 | + logits_no_change = ed.nn.utils.mean_field_logits( |
| 63 | + logits, covmat=None, mean_field_factor=-1) |
| 64 | + |
| 65 | + # Test if mean_field_logits functions as a temperature scaling method when |
| 66 | + # mean_field_factor > 0, with temperature = sqrt(1. + mean_field_factor). |
| 67 | + logits_scale_by_two = ed.nn.utils.mean_field_logits( |
| 68 | + logits, covmat=None, mean_field_factor=3.) |
| 69 | + |
| 70 | + self.assertAllClose(logits_no_change, logits, atol=1e-4) |
| 71 | + self.assertAllClose(logits_scale_by_two, logits / 2., atol=1e-4) |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + absltest.main() |
0 commit comments