def testShapeGetterRaisesException(self): x = tf.Variable(-1.) self.evaluate(tf1.global_variables_initializer()) with self.assertRaisesOpError( 'Argument `concentration1` must be positive.'): b = tfb.Kumaraswamy(concentration0=1., concentration1=x, validate_args=True) self.evaluate(b.forward_event_shape_tensor([1, 2, 3])) with self.assertRaisesOpError( 'Argument `concentration0` must be positive.'): b = tfb.Kumaraswamy(concentration0=x, concentration1=1., validate_args=True) self.evaluate(b.forward_event_shape_tensor(tf.constant([1, 2, 3])))
def testBijector(self): with self.cached_session(): a = 2. b = 0.3 bijector = tfb.Kumaraswamy(concentration1=a, concentration0=b, validate_args=True) self.assertEqual("kumaraswamy", bijector.name) x = np.array([[[0.1], [0.2], [0.3], [0.4], [0.5]]], dtype=np.float32) # Kumaraswamy cdf. This is the same as inverse(x). y = 1. - (1. - x**a)**b self.assertAllClose(y, self.evaluate(bijector.inverse(x))) self.assertAllClose(x, self.evaluate(bijector.forward(y))) kumaraswamy_log_pdf = (np.log(a) + np.log(b) + (a - 1) * np.log(x) + (b - 1) * np.log1p(-x**a)) self.assertAllClose( np.squeeze(kumaraswamy_log_pdf, axis=-1), self.evaluate( bijector.inverse_log_det_jacobian(x, event_ndims=1))) self.assertAllClose(self.evaluate( -bijector.inverse_log_det_jacobian(x, event_ndims=1)), self.evaluate( bijector.forward_log_det_jacobian( y, event_ndims=1)), rtol=1e-4, atol=0.)
def testScalarCongruency(self): bijector_test_util.assert_scalar_congruency(tfb.Kumaraswamy( concentration1=0.5, concentration0=1.1), lower_x=0., upper_x=1., eval_func=self.evaluate, n=int(10e3), rtol=0.02)
def testScalarCongruency(self): with self.test_session(): assert_scalar_congruency(tfb.Kumaraswamy(concentration1=0.5, concentration0=1.1), lower_x=0., upper_x=1., n=int(10e3), rtol=0.02)
def testBijectorConcentration1LogDetJacobianFiniteAtZero(self): # When concentration = 1., forward_log_det_jacobian should be finite at # zero. concentration0 = np.logspace(0.1, 10., num=20).astype(np.float32) bijector = tfb.Kumaraswamy(concentration1=1., concentration0=concentration0) fldj = self.evaluate( bijector.forward_log_det_jacobian(0., event_ndims=0)) self.assertAllEqual(np.ones_like(fldj, dtype=np.bool), np.isfinite(fldj))
def testGradient(self): x = tf.Variable(1.) y = tf.Variable(2.) b = tfb.Kumaraswamy(concentration0=x, concentration1=y, validate_args=True) with tf.GradientTape() as tape: loss = b.forward(1.) g = tape.gradient(loss, b.trainable_variables) self.assertLen(g, 2) self.assertAllNotNone(g)
def transform_by_kumaraswamy(x, feature_ndims, example_ndims): """Apply a Kumaraswamy bijector to features.""" concentration1 = util.pad_shape_with_ones( self.concentration1, example_ndims, start=-(feature_ndims + 1)) concentration0 = util.pad_shape_with_ones( self.concentration0, example_ndims, start=-(feature_ndims + 1)) bij = bijectors.Kumaraswamy(concentration1, concentration0, validate_args=validate_args) # Apply the inverse as this is the Kumaraswamy CDF. return bij.inverse(x)
def testVariableConcentration1(self): x = tf.Variable(1.) b = tfb.Kumaraswamy(concentration0=1., concentration1=x, validate_args=True) self.evaluate(tf1.global_variables_initializer()) self.assertIs(x, b.concentration1) self.assertEqual((), self.evaluate(b.forward(1.)).shape) with self.assertRaisesOpError( 'Argument `concentration1` must be positive.'): with tf.control_dependencies([x.assign(-1.)]): self.assertEqual((), self.evaluate(b.forward(1.)).shape)
def testBijectiveAndFinite(self): with self.test_session(): concentration1 = 1.2 concentration0 = 2. bijector = tfb.Kumaraswamy( concentration1=concentration1, concentration0=concentration0, validate_args=True) # Omitting the endpoints 0 and 1, since idlj will be infinity at these # endpoints. y = np.linspace(.01, 0.99, num=10).astype(np.float32) x = 1 - (1 - y ** concentration1) ** concentration0 assert_bijective_and_finite(bijector, x, y, event_ndims=0, rtol=1e-3)
def __init__(self, concentration1=None, concentration0=None, validate_args=False, allow_nan_stats=True, name="Kumaraswamy"): """Initialize a batch of Kumaraswamy distributions. Args: concentration1: Positive floating-point `Tensor` indicating mean number of successes; aka "alpha". Implies `self.dtype` and `self.batch_shape`, i.e., `concentration1.shape = [N1, N2, ..., Nm] = self.batch_shape`. concentration0: Positive floating-point `Tensor` indicating mean number of failures; aka "beta". Otherwise has same semantics as `concentration1`. validate_args: Python `bool`, default `False`. When `True` distribution parameters are checked for validity despite possibly degrading runtime performance. When `False` invalid inputs may silently render incorrect outputs. allow_nan_stats: Python `bool`, default `True`. When `True`, statistics (e.g., mean, mode, variance) use the value "`NaN`" to indicate the result is undefined. When `False`, an exception is raised if one or more of the statistic's batch members are undefined. name: Python `str` name prefixed to Ops created by this class. """ with tf.name_scope(name, values=[concentration1, concentration0]) as name: dtype = dtype_util.common_dtype([concentration1, concentration0], tf.float32) concentration1 = tf.convert_to_tensor(concentration1, name="concentration1", dtype=dtype) concentration0 = tf.convert_to_tensor(concentration0, name="concentration0", dtype=dtype) super(Kumaraswamy, self).__init__( distribution=tf.distributions.Uniform( low=tf.zeros([], dtype=concentration1.dtype), high=tf.ones([], dtype=concentration1.dtype), allow_nan_stats=allow_nan_stats), bijector=bijectors.Kumaraswamy(concentration1=concentration1, concentration0=concentration0, validate_args=validate_args), batch_shape=distribution_util.get_broadcast_shape( concentration1, concentration0), name=name) self._reparameterization_type = tf.distributions.FULLY_REPARAMETERIZED