我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用tensorflow.digamma()。
def get_S_loss_hao(mean_x, logcov_x, qv_alpha, qv_beta, qeta_mu, qeta_sigma, epsilon = 1e-8): sigma_px = 1.0 S1 = tf.digamma(qv_alpha) - tf.digamma(qv_alpha + qv_beta) S2 = tf.cumsum(tf.digamma(qv_beta) - tf.digamma(qv_alpha + qv_beta)) mean_x_expand = tf.expand_dims(mean_x, 1) logcov_x_expand = tf.expand_dims(logcov_x, 1) qeta_mu_expand = tf.expand_dims(tf.transpose(qeta_mu), 0) qeta_sigma_expand = tf.expand_dims(tf.transpose(qeta_sigma), 0) S3 = 0.5 * tf.reduce_sum(1 + logcov_x_expand - 2 * tf.log(sigma_px) \ - (tf.exp(logcov_x_expand) + tf.square(qeta_sigma_expand) \ + tf.square(mean_x_expand - qeta_mu_expand)) / tf.square(sigma_px), 2) S = S3 + tf.concat(0, [S1, [0.0]]) + tf.concat(0, [[0.0], S2]) # get the variational distribution q(z) S_max = tf.reduce_max(S, reduction_indices=1) S_whiten = S - tf.expand_dims(S_max, 1) qz = tf.exp(S_whiten) / tf.expand_dims(tf.reduce_sum(tf.exp(S_whiten), 1), 1) # Summarize the S loss # S_loss = -tf.reduce_sum(tf.log(tf.reduce_sum(tf.exp(S), 1))) S_loss = -tf.reduce_sum(S_max) - tf.reduce_sum(tf.log(tf.reduce_sum(tf.exp(S - tf.expand_dims(S_max, 1)), 1) + epsilon)) return S_loss, qz, S
def get_S_loss_hao(mean_x, logcov_x, qv_alpha, qv_beta, qeta_mu, qeta_sigma, sigma_px, epsilon = 1e-8): S1 = tf.digamma(qv_alpha) - tf.digamma(qv_alpha + qv_beta) S2 = tf.cumsum(tf.digamma(qv_beta) - tf.digamma(qv_alpha + qv_beta)) mean_x_expand = tf.expand_dims(mean_x, 1) logcov_x_expand = tf.expand_dims(logcov_x, 1) qeta_mu_expand = tf.expand_dims(tf.transpose(qeta_mu), 0) qeta_sigma_expand = tf.expand_dims(tf.transpose(qeta_sigma), 0) sigma_px_expand = tf.expand_dims(tf.transpose(sigma_px), 0) S3 = 0.5 * tf.reduce_sum(1 + logcov_x_expand - 2 * tf.log(sigma_px_expand) \ - (tf.exp(logcov_x_expand) + tf.square(qeta_sigma_expand) \ + tf.square(mean_x_expand - qeta_mu_expand)) / tf.square(sigma_px_expand), 2) S = S3 + tf.concat(0, [S1, [0.0]]) + tf.concat(0, [[0.0], S2]) # get the variational distribution q(z) S_max = tf.reduce_max(S, reduction_indices=1) S_whiten = S - tf.expand_dims(S_max, 1) qz = tf.exp(S_whiten) / tf.expand_dims(tf.reduce_sum(tf.exp(S_whiten), 1), 1) # Summarize the S loss # S_loss = -tf.reduce_sum(tf.log(tf.reduce_sum(tf.exp(S), 1))) S_loss = -tf.reduce_sum(S_max) - tf.reduce_sum(tf.log(tf.reduce_sum(tf.exp(S - tf.expand_dims(S_max, 1)), 1) + epsilon)) return S_loss, qz, S
def get_S_loss(alpha, beta, mean_x, logcov_x, mean_eta, logcov_eta, sigma2, epsilon=1e-8): mean_x_pad = tf.expand_dims(mean_x, 1) logcov_x_pad = tf.expand_dims(logcov_x, 1) mean_eta_pad = tf.expand_dims(mean_eta, 0) logcov_eta_pad = tf.expand_dims(logcov_eta, 0) S1 = tf.digamma(alpha) - tf.digamma(alpha + beta) S2 = tf.cumsum(tf.digamma(beta) - tf.digamma(alpha + beta)) S = 0.5 * tf.reduce_sum( \ 1 + logcov_x_pad - math.log(sigma2) \ - (tf.exp(logcov_x_pad) + tf.exp(logcov_eta_pad) + tf.square(mean_x_pad - mean_eta_pad)) / sigma2 , 2 \ ) \ + tf.concat(0, [S1, tf.constant([0.0])]) + tf.concat(0, [tf.constant([0.0]), S2]) assignments = tf.argmax(S, dimension=1) S_max = tf.reduce_max(S, reduction_indices=1) S_loss = -tf.reduce_sum(S_max) - tf.reduce_sum(tf.log(tf.reduce_sum(tf.exp(S - tf.expand_dims(S_max, 1)), reduction_indices = 1) + epsilon)) return assignments, S_loss
def gammaPrior(alpha, beta, n, m): return - (alpha - n)*tf.digamma(alpha) + tf.lgamma(alpha) - scipy.special.gammaln(n) - n * (tf.log(beta) - np.log(m)) - alpha * (m / beta - 1.0)
def normal_div(self): regul = tf.mul(self.prec, tf.reduce_sum(tf.square(self.mean), 0) + tf.reduce_sum(self.var, 0)) return (tf.reduce_sum(regul) / 2.0 - self.shape[0] / 2.0 * tf.reduce_sum(tf.digamma(self.prec_a) - tf.log(self.prec_b)) - tf.reduce_sum(self.logvar) / 2.0 )
def normal_div_partial(self, pmean, plogvar, n): prop = self.shape[0] / n regul = prop * tf.mul(self.prec, tf.reduce_sum(tf.square(pmean), 0) + tf.reduce_sum(tf.exp(plogvar), 0)) return (tf.reduce_sum(regul) / 2.0 - self.shape[0] / 2.0 * tf.reduce_sum(tf.digamma(self.prec_a) - tf.log(self.prec_b)) - prop * tf.reduce_sum(plogvar) / 2.0 )
def normal_div(self): regul = tf.mul(self.prec_ph, tf.reduce_sum(tf.square(self.mean), 0) + tf.reduce_sum(self.var, 0)) return (tf.reduce_sum(regul) / 2.0 #- self.shape[0] / 2.0 * tf.reduce_sum(tf.digamma(self.prec_a) - tf.log(self.prec_b)) - tf.reduce_sum(self.logvar) / 2.0 )
def normal_div_partial(self, pmean, plogvar, n): prop = self.shape[0] / n regul = prop * tf.mul(self.prec_ph, tf.reduce_sum(tf.square(pmean), 0) + tf.reduce_sum(tf.exp(plogvar), 0)) return (tf.reduce_sum(regul) / 2.0 #- self.shape[0] / 2.0 * tf.reduce_sum(tf.digamma(self.prec_a) - tf.log(self.prec_b)) - prop * tf.reduce_sum(plogvar) / 2.0 )
def setUp(self): super(CoreUnaryOpsTest, self).setUp() self.ops = [ ('abs', operator.abs, tf.abs, core.abs_function), ('neg', operator.neg, tf.neg, core.neg), # TODO(shoyer): add unary + to core TensorFlow ('pos', None, None, None), ('sign', None, tf.sign, core.sign), ('reciprocal', None, tf.reciprocal, core.reciprocal), ('square', None, tf.square, core.square), ('round', None, tf.round, core.round_function), ('sqrt', None, tf.sqrt, core.sqrt), ('rsqrt', None, tf.rsqrt, core.rsqrt), ('log', None, tf.log, core.log), ('exp', None, tf.exp, core.exp), ('log', None, tf.log, core.log), ('ceil', None, tf.ceil, core.ceil), ('floor', None, tf.floor, core.floor), ('cos', None, tf.cos, core.cos), ('sin', None, tf.sin, core.sin), ('tan', None, tf.tan, core.tan), ('acos', None, tf.acos, core.acos), ('asin', None, tf.asin, core.asin), ('atan', None, tf.atan, core.atan), ('lgamma', None, tf.lgamma, core.lgamma), ('digamma', None, tf.digamma, core.digamma), ('erf', None, tf.erf, core.erf), ('erfc', None, tf.erfc, core.erfc), ('lgamma', None, tf.lgamma, core.lgamma), ] total_size = np.prod([v.size for v in self.original_lt.axes.values()]) self.test_lt = core.LabeledTensor( tf.cast(self.original_lt, tf.float32) / total_size, self.original_lt.axes)
def tf_entropy(self, distr_params): alpha, beta, alpha_beta, log_norm = distr_params return log_norm - (beta - 1.0) * tf.digamma(x=beta) - (alpha - 1.0) * tf.digamma(x=alpha) + \ (alpha_beta - 2.0) * tf.digamma(x=alpha_beta)
def tf_kl_divergence(self, distr_params1, distr_params2): alpha1, beta1, alpha_beta1, log_norm1 = distr_params1 alpha2, beta2, alpha_beta2, log_norm2 = distr_params2 return log_norm2 - log_norm1 - tf.digamma(x=beta1) * (beta2 - beta1) - \ tf.digamma(x=alpha1) * (alpha2 - alpha1) + tf.digamma(x=alpha_beta1) * (alpha_beta2 - alpha_beta1)
def test_Digamma(self): t = tf.digamma(self.random(4, 3)) self.check(t)
def kl_Beta(alpha, beta, alpha_0, beta_0): return tf.reduce_sum(tf.lgamma(alpha_0) + tf.lgamma(beta_0) - tf.lgamma(alpha_0+beta_0) + tf.lgamma(alpha + beta) - tf.lgamma(alpha) - tf.lgamma(beta) + (alpha - alpha_0) * tf.digamma(alpha) + (beta - beta_0) * tf.digamma(beta) - (alpha + beta - alpha_0 - beta_0) * tf.digamma(alpha + beta))
def kl_Beta(alpha, beta, alpha_0, beta_0): return tf.reduce_sum(math.lgamma(alpha_0) + math.lgamma(beta_0) - math.lgamma(alpha_0+beta_0) + tf.lgamma(alpha + beta) - tf.lgamma(alpha) - tf.lgamma(beta) + (alpha - alpha_0) * tf.digamma(alpha) + (beta - beta_0) * tf.digamma(beta) - (alpha + beta - alpha_0 - beta_0) * tf.digamma(alpha + beta) )