Ejemplo n.º 1
0
    def test_context_stack(self):
        def_collector = default_summary_collector()
        self.assertIsNotNone(def_collector)

        c = SummaryCollector()
        self.assertIsNot(c, def_collector)

        with scoped_set_config(settings, auto_histogram=False):
            with c.as_default() as cc:
                self.assertIs(cc, c)
                self.assertIs(default_summary_collector(), c)
                self.assertFalse(settings.auto_histogram)

            with c.as_default(auto_histogram=True) as cc:
                self.assertIs(cc, c)
                self.assertIs(default_summary_collector(), c)
                self.assertTrue(settings.auto_histogram)

        with scoped_set_config(settings, auto_histogram=True):
            with c.as_default() as cc:
                self.assertIs(cc, c)
                self.assertIs(default_summary_collector(), c)
                self.assertTrue(settings.auto_histogram)

            with c.as_default(auto_histogram=False) as cc:
                self.assertIs(cc, c)
                self.assertIs(default_summary_collector(), c)
                self.assertFalse(settings.auto_histogram)

        self.assertIs(default_summary_collector(), def_collector)
Ejemplo n.º 2
0
 def test_check_numerics(self):
     with scoped_set_config(settings, check_numerics=True):
         uniform = Uniform(minval=-1e100, maxval=1e100)
         with self.test_session():
             with pytest.raises(Exception,
                                match=r'log_p : Tensor had Inf values'):
                 _ = uniform.log_prob(0.).eval()
Ejemplo n.º 3
0
 def test_check_numerics(self):
     with scoped_set_config(settings, check_numerics=True):
         normal = Normal(mean=0., std=-1.)
         with self.test_session():
             with pytest.raises(
                     Exception,
                     match=r'log\(std\) : Tensor had NaN values'):
                 _ = normal.logstd.eval()
Ejemplo n.º 4
0
    def test_multi_layer_flow(self):
        class _MyFlow(MultiLayerFlow):
            def __init__(self, log_det_list, **kwargs):
                self._log_det_list = list(log_det_list)
                super(_MyFlow, self).__init__(
                    len(self._log_det_list), x_value_ndims=0, y_value_ndims=0,
                    **kwargs
                )

            def explicitly_invertible(self):
                return True

            def _build(self, input=None):
                pass

            def _transform_layer(self, layer_id, x, compute_y, compute_log_det):
                return x, self._log_det_list[layer_id]

            def _inverse_transform_layer(self, layer_id, y, compute_x,
                                         compute_log_det):
                return y, self._log_det_list[layer_id]

        np.random.seed(1234)
        with self.test_session() as sess, \
                scoped_set_config(settings, enable_assertions=False):
            x = np.random.normal(size=[2, 3, 4]).astype(np.float32)
            log_det = np.zeros_like(x, dtype=np.float32)
            log_det_2 = np.random.normal(size=log_det.shape).astype(np.float32)

            # ZeroLogDet is the only element, test transform
            zero_log_det = ZeroLogDet(x.shape, tf.float32)
            flow = _MyFlow([
                ZeroLogDet(x.shape, tf.float32),
            ])
            _, log_det_out = flow.transform(x)
            self.assertIsNone(zero_log_det._self_tensor)
            self.assertIsInstance(log_det_out, ZeroLogDet)
            np.testing.assert_allclose(sess.run(log_det_out), log_det)

            # test ZeroLogDet is first element, test transform
            zero_log_det = ZeroLogDet(x.shape, tf.float32)
            flow = _MyFlow([
                ZeroLogDet(x.shape, tf.float32),
                tf.constant(log_det_2),
            ])
            _, log_det_out = flow.transform(x)
            self.assertIsNone(zero_log_det._self_tensor)
            np.testing.assert_allclose(sess.run(log_det_out), log_det_2)

            # test ZeroLogDet is second element, test transform
            zero_log_det = ZeroLogDet(x.shape, tf.float32)
            flow = _MyFlow([
                tf.constant(log_det_2),
                ZeroLogDet(x.shape, tf.float32),
            ])
            _, log_det_out = flow.transform(x)
            self.assertIsNone(zero_log_det._self_tensor)
            np.testing.assert_allclose(sess.run(log_det_out), log_det_2)