本文整理汇总了Python中tensorflow.lgamma方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.lgamma方法的具体用法?Python tensorflow.lgamma怎么用?Python tensorflow.lgamma使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.lgamma方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _log_prob
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import lgamma [as 别名]
def _log_prob(self, given):
logits, temperature = self.path_param(self.logits), \
self.path_param(self.temperature)
log_given = tf.log(given)
log_temperature = tf.log(temperature)
n = tf.cast(self.n_categories, self.dtype)
if self._check_numerics:
log_given = tf.check_numerics(log_given, "log(given)")
log_temperature = tf.check_numerics(
log_temperature, "log(temperature)")
temp = logits - temperature * log_given
return tf.lgamma(n) + (n - 1) * log_temperature + \
tf.reduce_sum(temp - log_given, axis=-1) - \
n * tf.reduce_logsumexp(temp, axis=-1)
示例2: _log_prob
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import lgamma [as 别名]
def _log_prob(self, given):
# TODO: not right when given=0 or 1
alpha, beta = self.alpha, self.beta
log_given = tf.log(given)
log_1_minus_given = tf.log(1 - given)
lgamma_alpha, lgamma_beta = tf.lgamma(alpha), tf.lgamma(beta)
lgamma_alpha_plus_beta = tf.lgamma(alpha + beta)
if self._check_numerics:
log_given = tf.check_numerics(log_given, "log(given)")
log_1_minus_given = tf.check_numerics(
log_1_minus_given, "log(1 - given)")
lgamma_alpha = tf.check_numerics(lgamma_alpha, "lgamma(alpha)")
lgamma_beta = tf.check_numerics(lgamma_beta, "lgamma(beta)")
lgamma_alpha_plus_beta = tf.check_numerics(
lgamma_alpha_plus_beta, "lgamma(alpha + beta)")
return (alpha - 1) * log_given + (beta - 1) * log_1_minus_given - (
lgamma_alpha + lgamma_beta - lgamma_alpha_plus_beta)
示例3: Poisson
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import lgamma [as 别名]
def Poisson(lambda_, name=None):
k = tf.placeholder(config.int_dtype, name=name)
# FIXME tf.lgamma only supports floats so cast before
Distribution.logp = (
tf.cast(k, config.dtype)*tf.log(lambda_) -
lambda_ -
tf.lgamma(tf.cast(k+1, config.dtype))
)
# TODO Distribution.integral = ...
def integral(l, u):
return tf.constant(1, dtype=config.dtype)
Distribution.integral = integral
return k
示例4: logp
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import lgamma [as 别名]
def logp(self, bin_counts):
"""Compute the log probability for the counts in the bin, under the model.
Args:
bin_counts: array-like integer counts
Returns:
The log-probability under the Poisson models for each element of
bin_counts.
"""
k = tf.to_float(bin_counts)
# log poisson(k, r) = log(r^k * e^(-r) / k!) = k log(r) - r - log k!
# log poisson(k, r=exp(x)) = k * x - exp(x) - lgamma(k + 1)
return k * self.logr - tf.exp(self.logr) - tf.lgamma(k + 1)
示例5: log_combination
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import lgamma [as 别名]
def log_combination(n, ks):
"""
Compute the log combination function.
.. math::
\\log \\binom{n}{k_1, k_2, \\dots} = \\log n! - \\sum_{i}\\log k_i!
:param n: A N-D `float` Tensor. Can broadcast to match `tf.shape(ks)[:-1]`.
:param ks: A (N + 1)-D `float` Tensor. Each slice `[i, j, ..., k, :]` is
a vector of `[k_1, k_2, ...]`.
:return: A N-D Tensor of type same as `n`.
"""
return tf.lgamma(n + 1) - tf.reduce_sum(tf.lgamma(ks + 1), axis=-1)
示例6: test_Lgamma
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import lgamma [as 别名]
def test_Lgamma(self):
t = tf.lgamma(self.random(4, 3))
self.check(t)
示例7: beta_fn
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import lgamma [as 别名]
def beta_fn(a,b):
return tf.exp( tf.lgamma(a) + tf.lgamma(b) - tf.lgamma(a+b) )
示例8: testFloatBasic
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import lgamma [as 别名]
def testFloatBasic(self):
x = np.arange(-3, 3).reshape(1, 3, 2).astype(np.float32)
y = (x + .5).astype(np.float32) # no zero
z = (x + 15.5).astype(np.float32) # all positive
k = np.arange(-0.90, 0.90, 0.25).astype(np.float32) # between -1 and 1
self._compareBoth(x, np.abs, tf.abs)
self._compareBoth(x, np.abs, _ABS)
self._compareBoth(x, np.negative, tf.neg)
self._compareBoth(x, np.negative, _NEG)
self._compareBoth(y, self._inv, tf.inv)
self._compareBoth(x, np.square, tf.square)
self._compareBoth(z, np.sqrt, tf.sqrt)
self._compareBoth(z, self._rsqrt, tf.rsqrt)
self._compareBoth(x, np.exp, tf.exp)
self._compareBoth(z, np.log, tf.log)
self._compareBoth(z, np.log1p, tf.log1p)
self._compareBoth(x, np.tanh, tf.tanh)
self._compareBoth(x, self._sigmoid, tf.sigmoid)
self._compareBoth(y, np.sign, tf.sign)
self._compareBoth(x, np.sin, tf.sin)
self._compareBoth(x, np.cos, tf.cos)
self._compareBoth(k, np.arcsin, tf.asin)
self._compareBoth(k, np.arccos, tf.acos)
self._compareBoth(x, np.arctan, tf.atan)
self._compareBoth(x, np.tan, tf.tan)
self._compareBoth(
y,
np.vectorize(self._replace_domain_error_with_inf(math.lgamma)),
tf.lgamma)
self._compareBoth(x, np.vectorize(math.erf), tf.erf)
self._compareBoth(x, np.vectorize(math.erfc), tf.erfc)
self._compareBothSparse(x, np.abs, tf.abs)
self._compareBothSparse(x, np.negative, tf.neg)
self._compareBothSparse(x, np.square, tf.square)
self._compareBothSparse(z, np.sqrt, tf.sqrt, tol=1e-3)
self._compareBothSparse(x, np.tanh, tf.tanh)
self._compareBothSparse(y, np.sign, tf.sign)
self._compareBothSparse(x, np.vectorize(math.erf), tf.erf)
示例9: testFloatEmpty
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import lgamma [as 别名]
def testFloatEmpty(self):
x = np.empty((2, 0, 5), dtype=np.float32)
self._compareBoth(x, np.abs, tf.abs)
self._compareBoth(x, np.abs, _ABS)
self._compareBoth(x, np.negative, tf.neg)
self._compareBoth(x, np.negative, _NEG)
self._compareBoth(x, self._inv, tf.inv)
self._compareBoth(x, np.square, tf.square)
self._compareBoth(x, np.sqrt, tf.sqrt)
self._compareBoth(x, self._rsqrt, tf.rsqrt)
self._compareBoth(x, np.exp, tf.exp)
self._compareBoth(x, np.log, tf.log)
self._compareBoth(x, np.log1p, tf.log1p)
self._compareBoth(x, np.tanh, tf.tanh)
self._compareBoth(x, self._sigmoid, tf.sigmoid)
self._compareBoth(x, np.sign, tf.sign)
self._compareBoth(x, np.sin, tf.sin)
self._compareBoth(x, np.cos, tf.cos)
# Can't use vectorize below, so just use some arbitrary function
self._compareBoth(x, np.sign, tf.lgamma)
self._compareBoth(x, np.sign, tf.erf)
self._compareBoth(x, np.sign, tf.erfc)
self._compareBoth(x, np.tan, tf.tan)
self._compareBoth(x, np.arcsin, tf.asin)
self._compareBoth(x, np.arccos, tf.acos)
self._compareBoth(x, np.arctan, tf.atan)
self._compareBothSparse(x, np.abs, tf.abs)
self._compareBothSparse(x, np.negative, tf.neg)
self._compareBothSparse(x, np.square, tf.square)
self._compareBothSparse(x, np.sqrt, tf.sqrt, tol=1e-3)
self._compareBothSparse(x, np.tanh, tf.tanh)
self._compareBothSparse(x, np.sign, tf.sign)
self._compareBothSparse(x, np.sign, tf.erf)
示例10: testDoubleBasic
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import lgamma [as 别名]
def testDoubleBasic(self):
x = np.arange(-3, 3).reshape(1, 3, 2).astype(np.float64)
y = (x + .5).astype(np.float64) # no zero
z = (x + 15.5).astype(np.float64) # all positive
k = np.arange(-0.90, 0.90, 0.35).reshape(1, 3, 2).astype(np.float64) # between -1 and 1
self._compareBoth(x, np.abs, tf.abs)
self._compareBoth(x, np.abs, _ABS)
self._compareBoth(x, np.negative, tf.neg)
self._compareBoth(x, np.negative, _NEG)
self._compareBoth(y, self._inv, tf.inv)
self._compareBoth(x, np.square, tf.square)
self._compareBoth(z, np.sqrt, tf.sqrt)
self._compareBoth(z, self._rsqrt, tf.rsqrt)
self._compareBoth(x, np.exp, tf.exp)
self._compareBoth(z, np.log, tf.log)
self._compareBoth(z, np.log1p, tf.log1p)
self._compareBoth(x, np.tanh, tf.tanh)
self._compareBoth(x, self._sigmoid, tf.sigmoid)
self._compareBoth(y, np.sign, tf.sign)
self._compareBoth(x, np.sin, tf.sin)
self._compareBoth(x, np.cos, tf.cos)
self._compareBoth(
y,
np.vectorize(self._replace_domain_error_with_inf(math.lgamma)),
tf.lgamma)
self._compareBoth(x, np.vectorize(math.erf), tf.erf)
self._compareBoth(x, np.vectorize(math.erfc), tf.erfc)
self._compareBoth(x, np.arctan, tf.atan)
self._compareBoth(k, np.arcsin, tf.asin)
self._compareBoth(k, np.arccos, tf.acos)
self._compareBoth(k, np.tan, tf.tan)
self._compareBothSparse(x, np.abs, tf.abs)
self._compareBothSparse(x, np.negative, tf.neg)
self._compareBothSparse(x, np.square, tf.square)
self._compareBothSparse(z, np.sqrt, tf.sqrt, tol=1e-3)
self._compareBothSparse(x, np.tanh, tf.tanh)
self._compareBothSparse(y, np.sign, tf.sign)
self._compareBothSparse(x, np.vectorize(math.erf), tf.erf)
示例11: testHalfBasic
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import lgamma [as 别名]
def testHalfBasic(self):
x = np.arange(-3, 3).reshape(1, 3, 2).astype(np.float16)
y = (x + .5).astype(np.float16) # no zero
z = (x + 15.5).astype(np.float16) # all positive
self._compareBoth(x, np.abs, tf.abs)
self._compareBoth(x, np.abs, _ABS)
self._compareBoth(x, np.negative, tf.neg)
self._compareBoth(x, np.negative, _NEG)
self._compareBoth(y, self._inv, tf.inv)
self._compareBoth(x, np.square, tf.square)
self._compareBoth(z, np.sqrt, tf.sqrt)
self._compareBoth(z, self._rsqrt, tf.rsqrt)
self._compareBoth(x, np.exp, tf.exp)
self._compareBoth(z, np.log, tf.log)
self._compareBoth(z, np.log1p, tf.log1p)
self._compareBoth(x, np.tanh, tf.tanh)
self._compareBoth(x, self._sigmoid, tf.sigmoid)
self._compareBoth(y, np.sign, tf.sign)
self._compareBoth(x, np.sin, tf.sin)
self._compareBoth(x, np.cos, tf.cos)
self._compareBoth(
y,
np.vectorize(self._replace_domain_error_with_inf(math.lgamma)),
tf.lgamma)
self._compareBoth(x, np.vectorize(math.erf), tf.erf)
self._compareBoth(x, np.vectorize(math.erfc), tf.erfc)
self._compareBothSparse(x, np.abs, tf.abs)
self._compareBothSparse(x, np.negative, tf.neg)
self._compareBothSparse(x, np.square, tf.square)
self._compareBothSparse(z, np.sqrt, tf.sqrt, tol=1e-3)
self._compareBothSparse(x, np.tanh, tf.tanh)
self._compareBothSparse(y, np.sign, tf.sign)
self._compareBothSparse(x, np.vectorize(math.erf), tf.erf, tol=1e-3)
示例12: _beta_log_prob
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import lgamma [as 别名]
def _beta_log_prob(alpha, beta, value):
with tf.Graph().as_default():
with tf.Session() as sess:
gamma_a, gamma_b, gamma_ab = sess.run((tf.lgamma(alpha), tf.lgamma(beta),
tf.lgamma(alpha + beta)))
log_denom = gamma_a + gamma_b - gamma_ab
log_num = np.log((value ** (alpha - 1)) * ((1 - value) ** (beta - 1)))
return log_num - log_denom
示例13: gammaln
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import lgamma [as 别名]
def gammaln(self, x):
return tf.lgamma(x)
示例14: get_log_likelihood
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import lgamma [as 别名]
def get_log_likelihood(self, observation, mask_dim=None):
x = observation
mu, var, nu = self.current_distribution
ln_gamma_quotient = tf.lgamma((1. + nu) / 2.) - tf.lgamma(nu / 2.)
ln_nom = (-(1. + nu) / 2.) * tf.log1p(tf.square(x - mu) / ((nu - 2.) * var))
ln_denom = 0.5 * tf.log((nu - 2.) * np.pi * var)
log_pdf = ln_gamma_quotient + ln_nom - ln_denom
if mask_dim is not None:
return tf.reduce_sum(log_pdf * mask_dim, 1)
else:
return tf.reduce_sum(log_pdf, 1)
示例15: get_log_likelihood_under_prior
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import lgamma [as 别名]
def get_log_likelihood_under_prior(self, observation, mask_dim=None):
x = observation
mu, var, nu = self.prior
ln_gamma_quotient = tf.lgamma((1. + nu) / 2.) - tf.lgamma(nu / 2.)
ln_nom = (-(1. + nu) / 2.) * tf.log1p((tf.square(x - mu) / ((nu - 2.) * var)))
ln_denom = 0.5 * tf.log((nu - 2.) * np.pi * var)
log_pdf = ln_gamma_quotient + ln_nom - ln_denom
if mask_dim is not None:
return tf.reduce_sum(log_pdf * mask_dim, 1)
else:
return tf.reduce_sum(log_pdf, 1)