Exemplo n.º 1
0
 def testBijectorScalarCongruencyLowerTemperature(self):
     floor = tfb.Softfloor(self.dtype(0.1))
     bijector_test_util.assert_scalar_congruency(floor,
                                                 self.dtype(-1.1),
                                                 self.dtype(1.1),
                                                 eval_func=self.evaluate,
                                                 rtol=5e-2)
Exemplo n.º 2
0
 def testBijectorForwardGradient(self):
   x_np = np.array([0.1, 2.23, 4.1], dtype=self.dtype)
   x = tf.constant(x_np)
   with tf.GradientTape() as tape:
     tape.watch(x)
     value = tfb.Softfloor(self.dtype(1.2)).forward(x)
   grad = tape.gradient(value, x)
   self.assertAllClose(_softfloor_grad_np(x_np, 1.2), grad)
Exemplo n.º 3
0
 def testBijectiveAndFiniteLowTemperature(self):
     floor = tfb.Softfloor(self.dtype(1e-1))
     x = np.sort(5 * self._rng.randn(3, 10), axis=-1).astype(self.dtype)
     y = 5 * self._rng.randn(3, 10).astype(self.dtype)
     bijector_test_util.assert_bijective_and_finite(floor,
                                                    x,
                                                    y,
                                                    eval_func=self.evaluate,
                                                    event_ndims=1)
Exemplo n.º 4
0
 def testVariableTemperature(self):
     temperature = tf.Variable(1.)
     b = tfb.Softfloor(temperature, validate_args=True)
     self.evaluate(temperature.initializer)
     self.assertIs(temperature, b.temperature)
     self.assertEqual((), self.evaluate(b.forward(0.5)).shape)
     with self.assertRaisesOpError(
             'Argument `temperature` was not positive.'):
         with tf.control_dependencies([temperature.assign(0.)]):
             self.evaluate(b.forward(0.5))
Exemplo n.º 5
0
 def testBijectorApproximatesFloorLowTemperature(self):
     # Let's make this look floor.
     floor = tfb.Softfloor(self.dtype(1e-4))
     # We chose a high temperature, and truncated range so that
     # we are likely to be retrieving 2.
     pos_values = np.linspace(2.01, 2.99, 100).astype(self.dtype)
     neg_values = np.linspace(-2.99, -2.01, 100).astype(self.dtype)
     self.assertAllClose(self.evaluate(floor.forward(pos_values)),
                         np.floor(pos_values))
     self.assertAllClose(self.evaluate(floor.forward(neg_values)),
                         np.floor(neg_values))
Exemplo n.º 6
0
 def testBijectorEndpointsAtLimit(self):
     # Check that we don't get NaN at half-integer and the floor matches.
     floor = tfb.Softfloor(self.dtype(1e-5))
     half_integers = np.linspace(0.5, 10.5, 11).astype(self.dtype)
     self.assertAllClose(self.evaluate(floor.forward(half_integers)),
                         np.floor(half_integers))
     self.assertAllFinite(self.evaluate(floor.inverse(half_integers)))
     # At integer values, check we don't have NaN's, and this is close to
     # integer - 0.5 (which is only true in the limit!).
     integers = np.linspace(0., 10., 11).astype(self.dtype)
     self.assertAllClose(self.evaluate(floor.forward(integers)),
                         integers - 0.5,
                         rtol=3e-3)
     self.assertAllFinite(self.evaluate(floor.inverse(integers)))
Exemplo n.º 7
0
 def testShapeGetters(self):
   x = tf.TensorShape([4])
   y = tf.TensorShape([4])
   bijector = tfb.Softfloor(self.dtype(1.), validate_args=True)
   self.assertAllEqual(y, bijector.forward_event_shape(x))
   self.assertAllEqual(
       tensorshape_util.as_list(y),
       self.evaluate(
           bijector.forward_event_shape_tensor(tensorshape_util.as_list(x))))
   self.assertAllEqual(x, bijector.inverse_event_shape(y))
   self.assertAllEqual(
       tensorshape_util.as_list(x),
       self.evaluate(
           bijector.inverse_event_shape_tensor(tensorshape_util.as_list(y))))
Exemplo n.º 8
0
    def testBijectorApproximatesIdentityHighTemperature(self):
        # Let's make this look like the identity.
        floor = tfb.Softfloor(self.dtype(1e4))
        pos_values = np.linspace(2.01, 2.99, 100).astype(self.dtype)
        neg_values = np.linspace(-2.99, -2.01, 100).astype(self.dtype)
        self.assertAllClose(self.evaluate(floor.forward(pos_values)),
                            pos_values,
                            rtol=1e-5)
        self.assertAllClose(self.evaluate(floor.forward(neg_values)),
                            neg_values,
                            rtol=1e-5)

        self.assertAllClose(self.evaluate(floor.inverse(pos_values)),
                            pos_values,
                            rtol=5e-4)
        self.assertAllClose(self.evaluate(floor.inverse(neg_values)),
                            neg_values,
                            rtol=5e-4)
Exemplo n.º 9
0
 def testBijectorForwardGradient(self):
     x_np = np.array([0.1, 2.23, 4.1], dtype=self.dtype)
     x = tf.constant(x_np)
     grad = value_and_gradient(tfb.Softfloor(self.dtype(1.2)).forward, x)[1]
     self.assertAllClose(_softfloor_grad_np(x_np, 1.2), grad)
Exemplo n.º 10
0
 def testBijectorScalarCongruencyMediumTemperature(self):
     floor = tfb.Softfloor(self.dtype(5.))
     bijector_test_util.assert_scalar_congruency(floor,
                                                 self.dtype(-1.1),
                                                 self.dtype(1.1),
                                                 eval_func=self.evaluate)