本文整理匯總了Python中torch.lgamma方法的典型用法代碼示例。如果您正苦於以下問題:Python torch.lgamma方法的具體用法?Python torch.lgamma怎麽用?Python torch.lgamma使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch
的用法示例。
在下文中一共展示了torch.lgamma方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import lgamma [as 別名]
def __init__(self, n, eta, validate_args=False):
TModule.__init__(self)
if not isinstance(n, int) or n < 1:
raise ValueError("n must be a positive integer")
if isinstance(eta, Number):
eta = torch.tensor(float(eta))
self.n = torch.tensor(n, dtype=torch.long, device=eta.device)
batch_shape = eta.shape
event_shape = torch.Size([n, n])
# Normalization constant(s)
i = torch.arange(n, dtype=eta.dtype, device=eta.device)
C = (((2 * eta.view(-1, 1) - 2 + i) * i).sum(1) * math.log(2)).view_as(eta)
C += n * torch.sum(2 * torch.lgamma(i / 2 + 1) - torch.lgamma(i + 2))
# need to assign values before registering as buffers to make argument validation work
self.eta = eta
self.C = C
super(LKJPrior, self).__init__(batch_shape, event_shape, validate_args=validate_args)
# now need to delete to be able to register buffer
del self.eta, self.C
self.register_buffer("eta", eta)
self.register_buffer("C", C)
示例2: forward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import lgamma [as 別名]
def forward(ctx, scale, c, dim):
scale = scale.double()
c = c.double()
ctx.scale = scale.clone().detach()
ctx.c = c.clone().detach()
ctx.dim = dim
device = scale.device
output = .5 * (Constants.logpi - Constants.log2) + scale.log() -(int(dim) - 1) * (c.log() / 2 + Constants.log2)
dim = torch.tensor(int(dim)).to(device).double()
k_float = rexpand(torch.arange(int(dim)), *scale.size()).double().to(device)
s = torch.lgamma(dim) - torch.lgamma(k_float + 1) - torch.lgamma(dim - k_float) \
+ (dim - 1 - 2 * k_float).pow(2) * c * scale.pow(2) / 2 \
+ torch.log1p(torch.erf((dim - 1 - 2 * k_float) * c.sqrt() * scale / math.sqrt(2)))
signs = torch.tensor([1., -1.]).double().to(device).repeat(((int(dim)+1) // 2)*2)[:int(dim)]
signs = rexpand(signs, *scale.size())
ctx.log_sum_term = log_sum_exp_signs(s, signs, dim=0)
output = output + ctx.log_sum_term
return output.float()
示例3: backward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import lgamma [as 別名]
def backward(ctx, grad_output):
grad_input = grad_output.clone()
device = grad_input.device
scale = ctx.scale
c = ctx.c
dim = torch.tensor(int(ctx.dim)).to(device).double()
k_float = rexpand(torch.arange(int(dim)), *scale.size()).double().to(device)
signs = torch.tensor([1., -1.]).double().to(device).repeat(((int(dim)+1) // 2)*2)[:int(dim)]
signs = rexpand(signs, *scale.size())
log_arg = (dim - 1 - 2 * k_float).pow(2) * c * scale * (1+torch.erf((dim - 1 - 2 * k_float) * c.sqrt() * scale / math.sqrt(2))) + \
torch.exp(-(dim - 1 - 2 * k_float).pow(2) * c * scale.pow(2) / 2) * 2 / math.sqrt(math.pi) * (dim - 1 - 2 * k_float) * c.sqrt() / math.sqrt(2)
log_arg_signs = torch.sign(log_arg)
s = torch.lgamma(dim) - torch.lgamma(k_float + 1) - torch.lgamma(dim - k_float) \
+ (dim - 1 - 2 * k_float).pow(2) * c * scale.pow(2) / 2 \
+ torch.log(log_arg_signs * log_arg)
log_grad_sum_sigma = log_sum_exp_signs(s, log_arg_signs * signs, dim=0)
grad_scale = torch.exp(log_grad_sum_sigma - ctx.log_sum_term)
grad_scale = 1 / ctx.scale + grad_scale
grad_scale = (grad_input * grad_scale.float()).view(-1, *grad_input.shape).sum(0)
return (grad_scale, None, None)
示例4: mean
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import lgamma [as 別名]
def mean(self):
c = self.c.double()
scale = self.scale.double()
dim = torch.tensor(int(self.dim)).double().to(self.device)
signs = torch.tensor([1., -1.]).double().to(self.device).repeat(((self.dim+1) // 2)*2)[:self.dim].unsqueeze(-1).unsqueeze(-1).expand(self.dim, *self.scale.size())
k_float = rexpand(torch.arange(self.dim), *self.scale.size()).double().to(self.device)
s2 = torch.lgamma(dim) - torch.lgamma(k_float + 1) - torch.lgamma(dim - k_float) \
+ (dim - 1 - 2 * k_float).pow(2) * c * scale.pow(2) / 2 \
+ torch.log1p(torch.erf((dim - 1 - 2 * k_float) * c.sqrt() * scale / math.sqrt(2)))
S2 = log_sum_exp_signs(s2, signs, dim=0)
log_arg = (dim - 1 - 2 * k_float) * c.sqrt() * scale.pow(2) * (1 + torch.erf((dim - 1 - 2 * k_float) * c.sqrt() * scale / math.sqrt(2))) + \
torch.exp(-(dim - 1 - 2 * k_float).pow(2) * c * scale.pow(2) / 2) * scale * math.sqrt(2 / math.pi)
log_arg_signs = torch.sign(log_arg)
s1 = torch.lgamma(dim) - torch.lgamma(k_float + 1) - torch.lgamma(dim - k_float) \
+ (dim - 1 - 2 * k_float).pow(2) * c * scale.pow(2) / 2 \
+ torch.log(log_arg_signs * log_arg)
S1 = log_sum_exp_signs(s1, signs * log_arg_signs, dim=0)
output = torch.exp(S1 - S2)
return output.float()
示例5: log_zinb_positive
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import lgamma [as 別名]
def log_zinb_positive(x, mu, theta, pi, eps=1e-8):
"""Note: All inputs are torch Tensors
log likelihood (scalar) of a minibatch according to a zinb model.
Notes:
We parametrize the bernoulli using the logits, hence the softplus functions appearing
Variables:
mu: mean of the negative binomial (has to be positive support) (shape: minibatch x genes)
theta: inverse dispersion parameter (has to be positive support) (shape: minibatch x genes)
pi: logit of the dropout parameter (real support) (shape: minibatch x genes)
eps: numerical stability constant
"""
# theta is the dispersion rate. If .ndimension() == 1, it is shared for all cells (regardless of batch or labels)
if theta.ndimension() == 1:
theta = theta.view(
1, theta.size(0)
) # In this case, we reshape theta for broadcasting
softplus_pi = F.softplus(-pi) # uses log(sigmoid(x)) = -softplus(-x)
log_theta_eps = torch.log(theta + eps)
log_theta_mu_eps = torch.log(theta + mu + eps)
pi_theta_log = -pi + theta * (log_theta_eps - log_theta_mu_eps)
case_zero = F.softplus(pi_theta_log) - softplus_pi
mul_case_zero = torch.mul((x < eps).type(torch.float32), case_zero)
case_non_zero = (
-softplus_pi
+ pi_theta_log
+ x * (torch.log(mu + eps) - log_theta_mu_eps)
+ torch.lgamma(x + theta)
- torch.lgamma(theta)
- torch.lgamma(x + 1)
)
mul_case_non_zero = torch.mul((x > eps).type(torch.float32), case_non_zero)
res = mul_case_zero + mul_case_non_zero
return res
示例6: log_nb_positive
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import lgamma [as 別名]
def log_nb_positive(x, mu, theta, eps=1e-8):
"""Note: All inputs should be torch Tensors
log likelihood (scalar) of a minibatch according to a nb model.
Variables:
mu: mean of the negative binomial (has to be positive support) (shape: minibatch x genes)
theta: inverse dispersion parameter (has to be positive support) (shape: minibatch x genes)
eps: numerical stability constant
"""
if theta.ndimension() == 1:
theta = theta.view(
1, theta.size(0)
) # In this case, we reshape theta for broadcasting
log_theta_mu_eps = torch.log(theta + mu + eps)
res = (
theta * (torch.log(theta + eps) - log_theta_mu_eps)
+ x * (torch.log(mu + eps) - log_theta_mu_eps)
+ torch.lgamma(x + theta)
- torch.lgamma(theta)
- torch.lgamma(x + 1)
)
return res
示例7: __log_surface_area
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import lgamma [as 別名]
def __log_surface_area(self):
if torch.__version__ >= "1.0.0":
lgamma = torch.lgamma(torch.tensor([(self._dim + 1) / 2]).to(self.device))
else:
lgamma = torch.lgamma(
torch.Tensor([(self._dim + 1) / 2], device=self.device)
)
return math.log(2) + ((self._dim + 1) / 2) * math.log(math.pi) - lgamma
示例8: cdf_r
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import lgamma [as 別名]
def cdf_r(value, scale, c, dim):
value = value.double()
scale = scale.double()
c = c.double()
if dim == 2:
return 1 / torch.erf(c.sqrt() * scale / math.sqrt(2)) * .5 * \
(2 * torch.erf(c.sqrt() * scale / math.sqrt(2)) + torch.erf((value - c.sqrt() * scale.pow(2)) / math.sqrt(2) / scale) - \
torch.erf((c.sqrt() * scale.pow(2) + value) / math.sqrt(2) / scale))
else:
device = value.device
k_float = rexpand(torch.arange(dim), *value.size()).double().to(device)
dim = torch.tensor(dim).to(device).double()
s1 = torch.lgamma(dim) - torch.lgamma(k_float + 1) - torch.lgamma(dim - k_float) \
+ (dim - 1 - 2 * k_float).pow(2) * c * scale.pow(2) / 2 \
+ torch.log( \
torch.erf((value - (dim - 1 - 2 * k_float) * c.sqrt() * scale.pow(2)) / scale / math.sqrt(2)) \
+ torch.erf((dim - 1 - 2 * k_float) * c.sqrt() * scale / math.sqrt(2)) \
)
s2 = torch.lgamma(dim) - torch.lgamma(k_float + 1) - torch.lgamma(dim - k_float) \
+ (dim - 1 - 2 * k_float).pow(2) * c * scale.pow(2) / 2 \
+ torch.log1p(torch.erf((dim - 1 - 2 * k_float) * c.sqrt() * scale / math.sqrt(2)))
signs = torch.tensor([1., -1.]).double().to(device).repeat(((int(dim)+1) // 2)*2)[:int(dim)]
signs = rexpand(signs, *value.size())
S1 = log_sum_exp_signs(s1, signs, dim=0)
S2 = log_sum_exp_signs(s2, signs, dim=0)
output = torch.exp(S1 - S2)
zero_value_idx = value == 0.
output[zero_value_idx] = 0.
return output.float()
示例9: _log_surface_area
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import lgamma [as 別名]
def _log_surface_area(self):
return math.log(2) + ((self._dim + 1) / 2) * math.log(math.pi) - torch.lgamma(
torch.Tensor([(self._dim + 1) / 2]))
示例10: students_t_nll
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import lgamma [as 別名]
def students_t_nll(x, df, scale):
"""The NLL of a Generalized Student's T distribution (w/o including TFP)."""
x = torch.as_tensor(x)
df = torch.as_tensor(df)
scale = torch.as_tensor(scale)
log_partition = torch.log(torch.abs(scale)) + torch.lgamma(
0.5 * df) - torch.lgamma(0.5 * df + torch.tensor(0.5)) + torch.tensor(
0.5 * np.log(np.pi))
return 0.5 * ((df + 1.) * torch.log1p(
(x / scale)**2. / df) + torch.log(df)) + log_partition
# A constant scale that makes tf.image.rgb_to_yuv() volume preserving.
示例11: lbeta
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import lgamma [as 別名]
def lbeta(a, b):
return torch.lgamma(a) + torch.lgamma(b) - torch.lgamma(a + b)
示例12: kuma_moments
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import lgamma [as 別名]
def kuma_moments(a, b, n):
"""
Computes nth moment of Kumaraswamy using using torch.lgamma
:param a:
:param b:
:param n:
:return: nth moment
"""
arg1 = 1 + n / a
log_value = torch.lgamma(arg1) + torch.lgamma(b) - torch.lgamma(arg1 + b)
return b * torch.exp(log_value)
示例13: log_mixture_nb
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import lgamma [as 別名]
def log_mixture_nb(x, mu_1, mu_2, theta_1, theta_2, pi, eps=1e-8):
"""Note: All inputs should be torch Tensors
log likelihood (scalar) of a minibatch according to a mixture nb model.
pi is the probability to be in the first component.
For totalVI, the first component should be background.
Parameters
----------
mu1: mean of the first negative binomial component (has to be positive support) (shape: minibatch x genes)
theta1: first inverse dispersion parameter (has to be positive support) (shape: minibatch x genes)
mu2: mean of the second negative binomial (has to be positive support) (shape: minibatch x genes)
theta2: second inverse dispersion parameter (has to be positive support) (shape: minibatch x genes)
If None, assume one shared inverse dispersion parameter.
eps: numerical stability constant
Returns
-------
"""
if theta_2 is not None:
log_nb_1 = log_nb_positive(x, mu_1, theta_1)
log_nb_2 = log_nb_positive(x, mu_2, theta_2)
# this is intended to reduce repeated computations
else:
theta = theta_1
if theta.ndimension() == 1:
theta = theta.view(
1, theta.size(0)
) # In this case, we reshape theta for broadcasting
log_theta_mu_1_eps = torch.log(theta + mu_1 + eps)
log_theta_mu_2_eps = torch.log(theta + mu_2 + eps)
lgamma_x_theta = torch.lgamma(x + theta)
lgamma_theta = torch.lgamma(theta)
lgamma_x_plus_1 = torch.lgamma(x + 1)
log_nb_1 = (
theta * (torch.log(theta + eps) - log_theta_mu_1_eps)
+ x * (torch.log(mu_1 + eps) - log_theta_mu_1_eps)
+ lgamma_x_theta
- lgamma_theta
- lgamma_x_plus_1
)
log_nb_2 = (
theta * (torch.log(theta + eps) - log_theta_mu_2_eps)
+ x * (torch.log(mu_2 + eps) - log_theta_mu_2_eps)
+ lgamma_x_theta
- lgamma_theta
- lgamma_x_plus_1
)
logsumexp = torch.logsumexp(torch.stack((log_nb_1, log_nb_2 - pi)), dim=0)
softplus_pi = F.softplus(-pi)
log_mixture_nb = logsumexp - softplus_pi
return log_mixture_nb
示例14: grad_cdf_value_scale
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import lgamma [as 別名]
def grad_cdf_value_scale(value, scale, c, dim):
device = value.device
dim = torch.tensor(int(dim)).to(device).double()
signs = torch.tensor([1., -1.]).double().to(device).repeat(((int(dim)+1) // 2)*2)[:int(dim)]
signs = rexpand(signs, *value.size())
k_float = rexpand(torch.arange(dim), *value.size()).double().to(device)
log_arg1 = (dim - 1 - 2 * k_float).pow(2) * c * scale * \
(\
torch.erf((value - (dim - 1 - 2 * k_float) * c.sqrt() * scale.pow(2)) / scale / math.sqrt(2)) \
+ torch.erf((dim - 1 - 2 * k_float) * c.sqrt() * scale / math.sqrt(2)) \
)
log_arg2 = math.sqrt(2 / math.pi) * ( \
(dim - 1 - 2 * k_float) * c.sqrt() * torch.exp(-(dim - 1 - 2 * k_float).pow(2) * c * scale.pow(2) / 2) \
- ((value / scale.pow(2) + (dim - 1 - 2 * k_float) * c.sqrt()) * torch.exp(-(value - (dim - 1 - 2 * k_float) * c.sqrt() * scale.pow(2)).pow(2) / (2 * scale.pow(2)))) \
)
log_arg = log_arg1 + log_arg2
sign_log_arg = torch.sign(log_arg)
s = torch.lgamma(dim) - torch.lgamma(k_float + 1) - torch.lgamma(dim - k_float) \
+ (dim - 1 - 2 * k_float).pow(2) * c * scale.pow(2) / 2 \
+ torch.log(sign_log_arg * log_arg)
log_grad_sum_sigma = log_sum_exp_signs(s, signs * sign_log_arg, dim=0)
grad_sum_sigma = torch.sum(signs * sign_log_arg * torch.exp(s), dim=0)
s1 = torch.lgamma(dim) - torch.lgamma(k_float + 1) - torch.lgamma(dim - k_float) \
+ (dim - 1 - 2 * k_float).pow(2) * c * scale.pow(2) / 2 \
+ torch.log( \
torch.erf((value - (dim - 1 - 2 * k_float) * c.sqrt() * scale.pow(2)) / scale / math.sqrt(2)) \
+ torch.erf((dim - 1 - 2 * k_float) * c.sqrt() * scale / math.sqrt(2)) \
)
S1 = log_sum_exp_signs(s1, signs, dim=0)
grad_log_cdf_scale = grad_sum_sigma / S1.exp()
log_unormalised_prob = - value.pow(2) / (2 * scale.pow(2)) + (dim - 1) * logsinh(c.sqrt() * value) - (dim - 1) / 2 * c.log()
with torch.autograd.enable_grad():
scale = scale.float()
logZ = _log_normalizer_closed_grad.apply(scale, c, dim)
grad_logZ_scale = grad(logZ, scale, grad_outputs=torch.ones_like(scale))
grad_log_cdf_scale = - grad_logZ_scale[0] + 1 / scale + grad_log_cdf_scale.float()
cdf = cdf_r(value.double(), scale.double(), c.double(), int(dim)).float().squeeze(0)
grad_scale = cdf * grad_log_cdf_scale
grad_value = (log_unormalised_prob.float() - logZ).exp()
return grad_value, grad_scale