def _cdf(self, x): # CDF is the probability that the Poisson variable is less or equal to x. # For fractional x, the CDF is equal to the CDF at n = floor(x). # For negative x, the CDF is zero, but tf.igammac gives NaNs, so we impute # the values and handle this case explicitly. n = tf.maximum(tf.floor(x), 0.) cdf = tf.igammac(1. + n, self.rate) return tf.where(tf.broadcast_to(tf.less(x, 0.), tf.shape(cdf)), tf.zeros_like(cdf), cdf)
def _cdf(self, x): # CDF is the probability that the Poisson variable is less or equal to x. # For fractional x, the CDF is equal to the CDF at n = floor(x). # For negative x, the CDF is zero, but tf.igammac gives NaNs, so we impute # the values and handle this case explicitly. safe_x = tf.maximum(x if self.interpolate_nondiscrete else tf.floor(x), 0.) cdf = tf.igammac(1. + safe_x, self.rate) return tf.where(tf.broadcast_to(x < 0., tf.shape(cdf)), tf.zeros_like(cdf), cdf)
def testPoissonCdfContinuousRelaxation(self): batch_size = 12 lam = tf.constant([3.0] * batch_size) x = np.array([-3., -0.5, 0., 2., 2.2, 3., 3.1, 4., 5., 5.5, 6., 7.]).astype(np.float32) expected_continuous_cdf = tf.igammac(1. + x, lam) expected_continuous_cdf = tf.where( x >= 0., expected_continuous_cdf, tf.zeros_like(expected_continuous_cdf)) expected_continuous_log_cdf = tf.log(expected_continuous_cdf) poisson = self._make_poisson(rate=lam, interpolate_nondiscrete=True) log_cdf = poisson.log_cdf(x) self.assertEqual(log_cdf.shape, (batch_size, )) self.assertAllClose(self.evaluate(log_cdf), self.evaluate(expected_continuous_log_cdf)) cdf = poisson.cdf(x) self.assertEqual(cdf.shape, (batch_size, )) self.assertAllClose(self.evaluate(cdf), self.evaluate(expected_continuous_cdf))
def testPoissonCdfContinuousRelaxation(self): batch_size = 12 lam = tf.constant([3.0] * batch_size) x = np.array( [-3., -0.5, 0., 2., 2.2, 3., 3.1, 4., 5., 5.5, 6., 7.]).astype( np.float32) expected_continuous_cdf = tf.igammac(1. + x, lam) expected_continuous_cdf = tf.where(x >= 0., expected_continuous_cdf, tf.zeros_like(expected_continuous_cdf)) expected_continuous_log_cdf = tf.log(expected_continuous_cdf) poisson = self._make_poisson(rate=lam, interpolate_nondiscrete=True) log_cdf = poisson.log_cdf(x) self.assertEqual(log_cdf.shape, (batch_size,)) self.assertAllClose(self.evaluate(log_cdf), self.evaluate(expected_continuous_log_cdf)) cdf = poisson.cdf(x) self.assertEqual(cdf.shape, (batch_size,)) self.assertAllClose(self.evaluate(cdf), self.evaluate(expected_continuous_cdf))
def _cdf(self, x): x = self._maybe_assert_valid_sample(x) # Note that igammac returns the upper regularized incomplete gamma # function Q(a, x), which is what we want for the CDF. return tf.igammac(self.concentration, self.rate / x)
def test_Igammac(self): t = tf.igammac(*self.random((3, 3), (3, 3))) self.check(t)
import tensorflow as tf """tf.igammac(a,x,name=None) 功能:计算gamma(a,x)/gamma(a),gamma(a,x)=\intergral_from_x_to_inf t^(a-1)*exp^(-t)dt。 输入:x为张量,可以为`float32`, `float64`类型。""" a = tf.constant(1, tf.float64) x = tf.constant([[-1, 0, 1, 2, 3]], tf.float64) z = tf.igammac(a, x) sess = tf.Session() print(sess.run(z)) sess.close() # z==>[[ nan 1. 0.36787944 0.13533528 0.04978707]]
def _cdf(self, x): if self.validate_args: x = distribution_util.embed_check_nonnegative_integer_form(x) return tf.igammac(1. + x, self.rate)
def GO_Gamma_v2(x, alpha): # x sim Gamma(alpha, 1) x = tf.cast(x, tf.float64) alpha = tf.cast(alpha, tf.float64) logx = tf.log(x) ex_gamma_xa = tf.exp(x + tf.lgamma(alpha) + (1. - alpha) * logx) psi_m_log = tf.digamma(alpha + 1.) - logx igamma_up_reg = tf.igammac(alpha, x) # Part 1 indx1 = tf.where(x <= 1e-2) x_indx1 = tf.gather_nd(x, indx1) alpha_indx1 = tf.gather_nd(alpha, indx1) GO_Gamma_alpha_value1 = tf.exp(x_indx1) * x_indx1 / alpha_indx1 * ( tf.gather_nd(psi_m_log, indx1) + x_indx1 / tf.pow(alpha_indx1 + 1., 2) - tf.pow(x_indx1 / (alpha_indx1 + 2.), 2) + 0.5 * tf.pow(x_indx1, 3) / tf.pow(alpha_indx1 + 3., 2) ) # Part 2 N_alpha = tf.round(tf.exp( - 0.488484605941243044124888683654717169702053070068359375 * tf.log(alpha) + 1.6948389987594634220613443176262080669403076171875 )) indx2 = tf.where(tf.logical_and( tf.logical_and(x > 1e-2, alpha <= 3.), (x <= (alpha + N_alpha * tf.sqrt(alpha))) )) KK = 15 kk = tf.cast(tf.range(1, KK + 1), tf.float64) x_indx2 = tf.gather_nd(x, indx2) alpha_indx2 = tf.gather_nd(alpha, indx2) GO_Gamma_alpha_value2 = tf.gather_nd(ex_gamma_xa, indx2) * ( -tf.gather_nd(psi_m_log, indx2) * tf.gather_nd(igamma_up_reg, indx2) + ( tf.digamma(alpha_indx2 + KK + 1.) - tf.gather_nd(logx, indx2) - tf.reduce_sum( tf.igammac(tf.expand_dims(alpha_indx2, 1) + tf.expand_dims(kk, 0), tf.expand_dims(x_indx2, 1)) / (tf.expand_dims(alpha_indx2, 1) + tf.expand_dims(kk, 0)) , 1) ) ) # Part 2_1 indx2_1 = tf.where(tf.logical_and( tf.logical_and(x > 1e-2, alpha <= 3.), (x > (alpha + N_alpha * tf.sqrt(alpha))) )) KK = 15 kk = tf.cast(tf.range(1, KK + 1), tf.float64) x_indx2_1 = tf.gather_nd(x, indx2_1) alpha_indx2_1 = tf.gather_nd(alpha, indx2_1) GO_Gamma_alpha_value2_1 = tf.gather_nd(ex_gamma_xa, indx2_1) * ( -tf.gather_nd(psi_m_log, indx2_1) * tf.gather_nd(igamma_up_reg, indx2_1) + ( tf.digamma(alpha_indx2_1 + KK + 1.) - tf.gather_nd(logx, indx2_1) - tf.reduce_sum( tf.igammac(tf.expand_dims(alpha_indx2_1, 1) + tf.expand_dims(kk, 0), tf.expand_dims(x_indx2_1, 1)) / (tf.expand_dims(alpha_indx2_1, 1) + tf.expand_dims(kk, 0)) , 1) ) ) GO_Gamma_alpha_value2_1 = tf.maximum( GO_Gamma_alpha_value2_1, 1. / alpha_indx2_1 - tf.gather_nd(ex_gamma_xa, indx2_1) * tf.gather_nd(psi_m_log, indx2_1) * tf.gather_nd(igamma_up_reg, indx2_1) ) # Part 3 indx3 = tf.where( tf.logical_and( tf.logical_and(x > 1e-2, alpha > 3.), alpha <= 500. ) ) KK = 10 kk = tf.cast(tf.range(1, KK + 1), tf.float64) x_indx3 = tf.gather_nd(x, indx3) alpha_indx3 = tf.gather_nd(alpha, indx3) x_l = alpha_indx3 - tf.log(alpha_indx3) * tf.sqrt(alpha_indx3) logx_l = tf.log(x_l) ex_gamma_xa_l = tf.exp(x_l + tf.lgamma(alpha_indx3) + (1. - alpha_indx3) * logx_l) psi_m_log_l = tf.digamma(alpha_indx3 + 1.) - logx_l igamma_low_reg_l = tf.igamma(alpha_indx3, x_l) # igamma_up_reg_l = tf.igammac(alpha_indx3, x_l) # f_l = ex_gamma_xa_l * ( # -psi_m_log_l * igamma_up_reg_l + # (tf.digamma(alpha_indx3 + KK + 1.) - logx_l - # tf.reduce_sum( # tf.igammac(tf.expand_dims(alpha_indx3, 1) + tf.expand_dims(kk, 0), tf.expand_dims(x_l, 1)) / # (tf.expand_dims(alpha_indx3, 1) + tf.expand_dims(kk, 0)) # , 1)) # ) f_l = ex_gamma_xa_l * ( psi_m_log_l * igamma_low_reg_l + tf.reduce_sum( tf.igamma(tf.expand_dims(alpha_indx3, 1) + tf.expand_dims(kk, 0), tf.expand_dims(x_l, 1)) / (tf.expand_dims(alpha_indx3, 1) + tf.expand_dims(kk, 0)) , 1) ) g_l = (1. + (1. - alpha_indx3) / x_l) * f_l + ( -ex_gamma_xa_l / x_l * igamma_low_reg_l + (psi_m_log_l + tf.reduce_sum( tf.exp( tf.expand_dims(kk, 0) * tf.log(tf.expand_dims(x_l, 1)) + tf.lgamma(tf.expand_dims(alpha_indx3, 1)) - tf.lgamma( tf.expand_dims(alpha_indx3, 1) + tf.expand_dims(kk, 0) + 1.) ) , 1)) ) x_m = alpha_indx3 f_m = 1. + 0.167303227226226980395296095593948848545551300048828125 / \ ( tf.pow(x_m, 1.0008649793164192676186985409003682434558868408203125) - 0.07516433982238841793321881823430885560810565948486328125 ) x_r = 2. * alpha_indx3 - x_l f_r = 1. / alpha_indx3 - tf.exp(x_r + tf.lgamma(alpha_indx3) + (1. - alpha_indx3) * tf.log(x_r)) * ( (tf.digamma(alpha_indx3 + 1.) - tf.log(x_r)) * tf.igammac(alpha_indx3, x_r) ) lambda_r = tf.exp( 959.627335718427275423891842365264892578125 / ( tf.pow(alpha_indx3, 1.324768828487964622553363369661383330821990966796875) + 142.427456986662718918523751199245452880859375 ) - 13.01439996187340142341781756840646266937255859375 ) x_mat_i = tf.concat([tf.expand_dims(x_l, 1), tf.expand_dims(x_m, 1), tf.expand_dims(x_r, 1)], 1) x_mat_bar_i = x_mat_i - tf.expand_dims(alpha_indx3, 1) x_mat_hat_i = tf.sqrt(x_mat_i) - tf.sqrt(tf.expand_dims(alpha_indx3, 1)) f_mat_i = tf.concat([tf.expand_dims(f_l, 1), tf.expand_dims(f_m, 1), tf.expand_dims(f_r, 1)], 1) lambda_mat_i = tf.concat([tf.expand_dims(tf.ones_like(alpha_indx3), 1), tf.expand_dims(tf.ones_like(alpha_indx3), 1), tf.expand_dims(lambda_r, 1) ], 1) x_mat_j = tf.expand_dims(x_l, 1) g_mat_j = tf.expand_dims(g_l, 1) lambda_mat_j = tf.expand_dims(tf.ones_like(alpha_indx3), 1) A = tf.reduce_sum(lambda_mat_i * tf.pow(x_mat_bar_i, 2), 1) + tf.reduce_sum(lambda_mat_j, 1) B = tf.reduce_sum(lambda_mat_i * x_mat_bar_i * x_mat_hat_i, 1) + \ tf.reduce_sum(lambda_mat_j / 2. / tf.sqrt(x_mat_j), 1) C = tf.reduce_sum(lambda_mat_i * x_mat_bar_i, 1) D = tf.reduce_sum(lambda_mat_i * tf.pow(x_mat_hat_i, 2), 1) + tf.reduce_sum(lambda_mat_j / 4. / x_mat_j, 1) E = tf.reduce_sum(lambda_mat_i * x_mat_hat_i, 1) F = tf.reduce_sum(lambda_mat_i, 1) G = tf.reduce_sum(lambda_mat_i * x_mat_bar_i * f_mat_i, 1) + tf.reduce_sum(lambda_mat_j * g_mat_j, 1) H = tf.reduce_sum(lambda_mat_i * x_mat_hat_i * f_mat_i, 1) + \ tf.reduce_sum(lambda_mat_j / 2. / tf.sqrt(x_mat_j) * g_mat_j, 1) I = tf.reduce_sum(lambda_mat_i * f_mat_i, 1) Z = F * tf.pow(B, 2) - 2. * B * C * E + D * tf.pow(C, 2) + A * tf.pow(E, 2) - A * D * F a_cor = 1. / Z * (G * (tf.pow(E, 2) - D * F) + H * (B * F - C * E) - I * (B * E - C * D)) b_cor = 1. / Z * (G * (B * F - C * E) + H * (tf.pow(C, 2) - A * F) - I * (B * C - A * E)) c_cor = 1. / Z * (-G * (B * E - C * D) + I * (tf.pow(B, 2) - A * D) - H * (B * C - A * E)) GO_Gamma_alpha_value3 = a_cor * (x_indx3 - alpha_indx3) + b_cor * (tf.sqrt(x_indx3) - tf.sqrt(alpha_indx3)) + c_cor GO_Gamma_alpha_value3 = tf.maximum( GO_Gamma_alpha_value3, 1. / alpha_indx3 - tf.gather_nd(ex_gamma_xa, indx3) * tf.gather_nd(psi_m_log, indx3) * tf.gather_nd(igamma_up_reg, indx3) ) # Part 4 # indx4 = tf.where( # tf.logical_and( # tf.logical_and(x > 1e-2, alpha > 500.), # (x <= (alpha + 2. * tf.log(alpha) * tf.sqrt(alpha))) # ) # ) indx4 = tf.where( tf.logical_and(x > 1e-2, alpha > 500.) ) x_indx4 = tf.gather_nd(x, indx4) alpha_indx4 = tf.gather_nd(alpha, indx4) f_m_large = 1. + 0.167303227226226980395296095593948848545551300048828125 / \ ( tf.pow(alpha_indx4, 1.0008649793164192676186985409003682434558868408203125) - 0.07516433982238841793321881823430885560810565948486328125 ) g_m_large = 0.54116502161502622048061539317131973803043365478515625 * \ tf.pow(alpha_indx4, -1.010274491769996618728555404231883585453033447265625) GO_Gamma_alpha_value4 = f_m_large + g_m_large * (x_indx4 - alpha_indx4) # Part 4_1 # indx4_1 = tf.where( # tf.logical_and( # tf.logical_and(x > 1e-2, alpha > 500.), # (x > (alpha + 2. * tf.log(alpha) * tf.sqrt(alpha))) # ) # ) # alpha_indx4_1 = tf.gather_nd(alpha, indx4_1) # GO_Gamma_alpha_value4_1 = 1. / alpha_indx4_1 - tf.gather_nd(ex_gamma_xa, indx4_1) * \ # tf.gather_nd(psi_m_log, indx4_1) * tf.gather_nd(igamma_up_reg, indx4_1) # Summerize GO_Gamma_alpha = tf.sparse_to_dense(indx1, x.shape, GO_Gamma_alpha_value1) + \ tf.sparse_to_dense(indx2, x.shape, GO_Gamma_alpha_value2) + \ tf.sparse_to_dense(indx2_1, x.shape, GO_Gamma_alpha_value2_1) + \ tf.sparse_to_dense(indx3, x.shape, GO_Gamma_alpha_value3) + \ tf.sparse_to_dense(indx4, x.shape, GO_Gamma_alpha_value4) # + \ # tf.sparse_to_dense(indx4_1, x.shape, GO_Gamma_alpha_value4_1) GO_Gamma_alpha = tf.cast(GO_Gamma_alpha, tf.float32) return GO_Gamma_alpha # , x_l, x_r, f_l, f_m, f_r, g_l
tf.gather() tf.gather_nd() tf.gather_v2() tf.get_summary_op() tf.gradients() tf.boolean_mask() tf.sparse_mask() tf.sequence_mask() tf.random_gamma() tf.digamma() tf.igamma() tf.lgamma() tf.polygamma() tf.igammac() tf.tensor_shape.as_shape() # gfile tf.gfile.Copy() tf.gfile.DeleteRecursively() tf.gfile.Exists() tf.gfile.Glob() tf.gfile.IsDirectory() tf.gfile.ListDirectory() tf.gfile.MakeDirs() tf.gfile.MkDir() tf.gfile.Remove() tf.gfile.Rename() tf.gfile.Stat()