def _log_cdf(self, x): x = control_flow_ops.with_dependencies([check_ops.assert_positive(x)] if self.validate_args else [], x) contrib_tensor_util.assert_same_float_dtype(tensors=[x], dtype=self.dtype) # Note that igamma returns the regularized incomplete gamma function, # which is what we want for the CDF. return math_ops.log(math_ops.igamma(self.alpha, self.beta * x))
def testIgammaMediumValues(self, dtype, rtol, atol): # Test values near zero. x = np.random.uniform(low=1., high=100., size=[NUM_SAMPLES]).astype(dtype) a = np.random.uniform(low=1., high=100., size=[NUM_SAMPLES]).astype(dtype) expected_values = sps.gammainc(a, x) with self.session() as sess: with self.test_scope(): actual = sess.run(math_ops.igamma(a, x)) self.assertAllClose(expected_values, actual, atol=atol, rtol=rtol)
def cdf(self, x, name="cdf"): """CDF of observations `x` under these Gamma distribution(s). Args: x: tensor of dtype `dtype`, must be broadcastable with `alpha` and `beta`. name: The name to give this op. Returns: cdf: tensor of dtype `dtype`, the CDFs of `x`. """ with ops.name_scope(self.name): with ops.op_scope([self._alpha, self._beta, x], name): return math_ops.igamma(self._alpha, self._beta * x)
def testIgammaGradLargeValues(self, dtype, tolerance): self.maybe_skip_test(dtype) with self.session(): with self.test_scope(): x = constant_op.constant( np.random.uniform(low=100., high=int(1e4), size=[NUM_SAMPLES]).astype(dtype)) a = constant_op.constant( np.random.uniform(low=100., high=int(1e4), size=[NUM_SAMPLES]).astype(dtype)) f = lambda b: math_ops.igamma(b, x) max_error = gradient_checker_v2.max_error( *gradient_checker_v2.compute_gradient(f, x=[a], delta=1e-2)) self.assertLessEqual(max_error, tolerance)
def log_cdf(self, x, name="log_cdf"): """Log CDF of observations `x` under these Gamma distribution(s). Args: x: tensor of dtype `dtype`, must be broadcastable with `alpha` and `beta`. name: The name to give this op. Returns: log_cdf: tensor of dtype `dtype`, the log-CDFs of `x`. """ with ops.name_scope(self.name): with ops.name_scope(name, values=[self._alpha, self._beta, x]): x = ops.convert_to_tensor(x) x = control_flow_ops.with_dependencies([check_ops.assert_positive(x)] if self.validate_args else [], x) contrib_tensor_util.assert_same_float_dtype(tensors=[x], dtype=self.dtype) # Note that igamma returns the regularized incomplete gamma function, # which is what we want for the CDF. return math_ops.log(math_ops.igamma(self._alpha, self._beta * x))
def log_cdf(self, x, name="log_cdf"): """Log CDF of observations `x` under these Gamma distribution(s). Args: x: tensor of dtype `dtype`, must be broadcastable with `alpha` and `beta`. name: The name to give this op. Returns: log_cdf: tensor of dtype `dtype`, the log-CDFs of `x`. """ with ops.op_scope([self._alpha, self._beta, x], self.name): with ops.name_scope(name): x = ops.convert_to_tensor(x) x = control_flow_ops.with_dependencies( [check_ops.assert_positive(x)], x) contrib_tensor_util.assert_same_float_dtype(tensors=[x,], dtype=self.dtype) # Note that igamma returns the regularized incomplete gamma function, # which is what we want for the CDF. return math_ops.log(math_ops.igamma(self._alpha, self._beta * x))
def _testCompareToImplicitDerivative(self, dtype): """Compare to the implicit reparameterization derivative. Let's derive the formula we compare to. Start from the fact that CDF maps a random variable to the Uniform random variable: igamma(alpha, sample) = u, where u ~ Uniform(0, 1). Apply d / dalpha to both sides: d igamma(alpha, sample) / dalpha + d igamma(alpha, sample) / dsample * dsample/dalpha = 0 d igamma(alpha, sample) / dalpha + d igamma(alpha, sample) / dsample * dsample / dalpha = 0 dsample/dalpha = - (d igamma(alpha, sample) / dalpha) / d igamma(alpha, sample) / dsample This is the equation (8) of https://arxiv.org/abs/1805.08498 Args: dtype: TensorFlow dtype to perform the computations in. """ np_dtype = dtype.as_numpy_dtype alpha = constant_op.constant(np.logspace(-2, 3, dtype=np_dtype)) sample = random_ops.random_gamma([], alpha, np_dtype(1.0), dtype=dtype, seed=12345) actual = gradients_impl.gradients(sample, alpha)[0] sample_sg = array_ops.stop_gradient(sample) cdf = math_ops.igamma(alpha, sample_sg) dcdf_dalpha, dcdf_dsample = gradients_impl.gradients( cdf, [alpha, sample_sg]) # Numerically unstable due to division, do not try at home. expected = -dcdf_dalpha / dcdf_dsample (actual_val, expected_val) = self.evaluate((actual, expected)) self.assertAllClose(actual_val, expected_val, rtol=1e-3, atol=1e-3)
def _testCompareToImplicitDerivative(self, dtype): """Compare to the implicit reparameterization derivative. Let's derive the formula we compare to. Start from the fact that CDF maps a random variable to the Uniform random variable: igamma(alpha, sample) = u, where u ~ Uniform(0, 1). Apply d / dalpha to both sides: d igamma(alpha, sample) / dalpha + d igamma(alpha, sample) / dsample * dsample/dalpha = 0 d igamma(alpha, sample) / dalpha + d igamma(alpha, sample) / dsample * dsample / dalpha = 0 dsample/dalpha = - (d igamma(alpha, sample) / dalpha) / d igamma(alpha, sample) / dsample This is the equation (8) of https://arxiv.org/abs/1805.08498 Args: dtype: TensorFlow dtype to perform the computations in. """ np_dtype = dtype.as_numpy_dtype alpha = constant_op.constant(np.logspace(-2, 3, dtype=np_dtype)) sample = random_ops.random_gamma( [], alpha, np_dtype(1.0), dtype=dtype, seed=12345) actual = gradients_impl.gradients(sample, alpha)[0] sample_sg = array_ops.stop_gradient(sample) cdf = math_ops.igamma(alpha, sample_sg) dcdf_dalpha, dcdf_dsample = gradients_impl.gradients( cdf, [alpha, sample_sg]) # Numerically unstable due to division, do not try at home. expected = -dcdf_dalpha / dcdf_dsample (actual_val, expected_val) = self.evaluate((actual, expected)) self.assertAllClose(actual_val, expected_val, rtol=1e-3, atol=1e-3)
def _igamma(a, x): return math_ops.igamma(a, x)
def _cdf(self, x): return math_ops.igamma(self.alpha, self.beta * x)
def _cdf(self, x): x = self._maybe_assert_valid_sample(x) # Note that igamma returns the regularized incomplete gamma function, # which is what we want for the CDF. return math_ops.igamma(self.concentration, self.rate * x)
def cdf(self, x, name="cdf"): with ops.op_scope([self._alpha, self._beta, x], self.name): with ops.name_scope(name): return math_ops.igamma(self._alpha, self._beta * x)