Python tensorflow.lgamma() Examples
The following are 30
code examples of tensorflow.lgamma().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow
, or try the search function
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: multivariate.py From zhusuan with MIT License | 6 votes |
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)
Example #2
Source File: univariate.py From zhusuan with MIT License | 6 votes |
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)
Example #3
Source File: univariate.py From zhusuan with MIT License | 6 votes |
def _log_prob(self, given): logits = self.logits n = tf.cast(self.n_experiments, self.param_dtype) given = tf.cast(given, self.param_dtype) log_1_minus_p = -tf.nn.softplus(logits) lgamma_n_plus_1 = tf.lgamma(n + 1) lgamma_given_plus_1 = tf.lgamma(given + 1) lgamma_n_minus_given_plus_1 = tf.lgamma(n - given + 1) if self._check_numerics: lgamma_given_plus_1 = tf.check_numerics( lgamma_given_plus_1, "lgamma(given + 1)") lgamma_n_minus_given_plus_1 = tf.check_numerics( lgamma_n_minus_given_plus_1, "lgamma(n - given + 1)") return lgamma_n_plus_1 - lgamma_n_minus_given_plus_1 - \ lgamma_given_plus_1 + given * logits + n * log_1_minus_p
Example #4
Source File: poisson.py From tensorprob with MIT License | 6 votes |
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
Example #5
Source File: tensorflow.py From deepx with MIT License | 5 votes |
def gammaln(self, x): return tf.lgamma(x)
Example #6
Source File: cwise_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
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)
Example #7
Source File: distributions.py From hands-detection with MIT License | 5 votes |
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)
Example #8
Source File: distributions.py From object_detection_kitti with Apache License 2.0 | 5 votes |
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)
Example #9
Source File: nn_extra_student.py From bruno with MIT License | 5 votes |
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)
Example #10
Source File: nn_extra_student.py From bruno with MIT License | 5 votes |
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)
Example #11
Source File: loss.py From dca with Apache License 2.0 | 5 votes |
def poisson_loss(y_true, y_pred): y_pred = tf.cast(y_pred, tf.float32) y_true = tf.cast(y_true, tf.float32) # we can use the Possion PMF from TensorFlow as well # dist = tf.contrib.distributions # return -tf.reduce_mean(dist.Poisson(y_pred).log_pmf(y_true)) nelem = _nelem(y_true) y_true = _nan2zero(y_true) # last term can be avoided since it doesn't depend on y_pred # however keeping it gives a nice lower bound to zero ret = y_pred - y_true*tf.log(y_pred+1e-10) + tf.lgamma(y_true+1.0) return tf.divide(tf.reduce_sum(ret), nelem) # We need a class (or closure) here, # because it's not possible to # pass extra arguments to Keras loss functions # See https://github.com/fchollet/keras/issues/2121 # dispersion (theta) parameter is a scalar by default. # scale_factor scales the nbinom mean before the # calculation of the loss to balance the # learning rates of theta and network weights
Example #12
Source File: distributions.py From object_detection_with_tensorflow with MIT License | 5 votes |
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)
Example #13
Source File: pcrl.py From cornac with Apache License 2.0 | 5 votes |
def log_q(self, z, alpha, beta): return ( (alpha - 1) * tf.log(z) - beta * z + alpha * tf.log(beta) - tf.lgamma(alpha) ) # Log density of the standard normal N(0, 1)
Example #14
Source File: pcrl.py From cornac with Apache License 2.0 | 5 votes |
def loss(self, C, X_g, X_, alpha, beta, z, E, Zik, Tk): const_term = C * tf.log(1e-10 + X_) - X_ const_term = tf.reduce_sum(const_term, 1) loss1 = C * tf.log(1e-10 + X_g) - X_g loss1 = tf.reduce_sum(loss1, 1) loss2 = self.log_q(z, alpha + self.B, beta) loss2 = const_term * tf.reduce_sum(loss2, 1) # loss3 = -log_r(E, alpha,beta) loss3 = -self.log_r(E, alpha + self.B, beta) loss3 = const_term * tf.reduce_sum(loss3, 1) # The sum of KL terms of all generator's wheights (up to constant terms) kl_w = 0.0 if not self.w_determinist: for l in range(0, self.L + 1): kl_w += tf.reduce_sum( -0.5 * tf.reduce_sum(tf.square(self.generator_params[l]), 1) ) # KL Divergence term kl_term = ( (alpha - self.aa - Zik) * tf.digamma(alpha) - tf.lgamma(alpha) + (self.aa + Zik) * tf.log(beta) + alpha * (Tk + self.bb - beta) / beta ) kl_term = -tf.reduce_sum(kl_term, 1) return ( -tf.reduce_mean(loss1 + loss2 + loss3 + kl_term) + kl_w / self.aux_data.shape[0] ) # fitting PCRL to observed data
Example #15
Source File: distributions.py From g-tensorflow-models with Apache License 2.0 | 5 votes |
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)
Example #16
Source File: distributions.py From models with Apache License 2.0 | 5 votes |
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)
Example #17
Source File: distributions.py From multilabel-image-classification-tensorflow with MIT License | 5 votes |
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)
Example #18
Source File: test_dists.py From sonic_contest with MIT License | 5 votes |
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
Example #19
Source File: cwise_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
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)
Example #20
Source File: distributions.py From DOTA_models with Apache License 2.0 | 5 votes |
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)
Example #21
Source File: cwise_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
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)
Example #22
Source File: cwise_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
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)
Example #23
Source File: distributions.py From Gun-Detector with Apache License 2.0 | 5 votes |
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)
Example #24
Source File: distributions.py From yolo_v2 with Apache License 2.0 | 5 votes |
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)
Example #25
Source File: gaussMMVAE_collapsed.py From mixture_density_VAEs with MIT License | 5 votes |
def beta_fn(a,b): return tf.exp( tf.lgamma(a) + tf.lgamma(b) - tf.lgamma(a+b) )
Example #26
Source File: ops.py From tfdeploy with MIT License | 5 votes |
def test_Lgamma(self): t = tf.lgamma(self.random(4, 3)) self.check(t)
Example #27
Source File: univariate.py From zhusuan with MIT License | 5 votes |
def _log_prob(self, given): alpha, beta = self.alpha, self.beta log_given = tf.log(given) log_beta = tf.log(beta) lgamma_alpha = tf.lgamma(alpha) if self._check_numerics: log_given = tf.check_numerics(log_given, "log(given)") log_beta = tf.check_numerics(log_beta, "log(beta)") lgamma_alpha = tf.check_numerics(lgamma_alpha, "lgamma(alpha)") return alpha * log_beta - lgamma_alpha - (alpha + 1) * log_given - \ beta / given
Example #28
Source File: univariate.py From zhusuan with MIT License | 5 votes |
def _log_prob(self, given): alpha, beta = self.alpha, self.beta log_given = tf.log(given) log_beta = tf.log(beta) lgamma_alpha = tf.lgamma(alpha) if self._check_numerics: log_given = tf.check_numerics(log_given, "log(given)") log_beta = tf.check_numerics(log_beta, "log(beta)") lgamma_alpha = tf.check_numerics(lgamma_alpha, "lgamma(alpha)") return alpha * log_beta - lgamma_alpha + (alpha - 1) * log_given - \ beta * given
Example #29
Source File: multivariate.py From zhusuan with MIT License | 5 votes |
def _log_prob(self, given): logits, temperature = self.path_param(self.logits),\ self.path_param(self.temperature) n = tf.cast(self.n_categories, self.dtype) log_temperature = tf.log(temperature) if self._check_numerics: log_temperature = tf.check_numerics( log_temperature, "log(temperature)") temp = logits - temperature * given return tf.lgamma(n) + (n - 1) * log_temperature + \ tf.reduce_sum(temp, axis=-1) - \ n * tf.reduce_logsumexp(temp, axis=-1)
Example #30
Source File: utils.py From zhusuan with MIT License | 5 votes |
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)