YouTip LogoYouTip

Pytorch Torch Lgamma

## PyTorch `torch.lgamma` Function The `torch.lgamma` function in PyTorch computes the natural logarithm of the absolute value of the gamma function for each element in the input tensor: $$\text{out}_i = \ln |\Gamma(\text{input}_i)|$$ The gamma function $\Gamma(x)$ is a generalization of the factorial function to real and complex numbers. For any positive integer $n$, $\Gamma(n) = (n-1)!$. Using `torch.lgamma(x)` is mathematically equivalent to `torch.log(torch.lgamma(x).exp())`, but it is computationally much more stable and avoids underflow or overflow issues for large input values. --- ### Syntax and Parameters ```python torch.lgamma(input, *, out=None) -> Tensor ``` #### Parameters * **`input`** (*Tensor*): The input tensor containing real numbers. * **`out`** (*Tensor*, *optional*): The output tensor where the result will be written. #### Return Value * Returns a new tensor with the same shape and data type as `input` containing the natural logarithm of the absolute value of the gamma function for each element. --- ### Code Examples #### Example 1: Basic Usage with Positive Integers Since $\Gamma(n) = (n-1)!$, we can verify the results for small integers: * $\Gamma(1) = 0! = 1 \implies \ln(1) = 0$ * $\Gamma(2) = 1! = 1 \implies \ln(1) = 0$ * $\Gamma(3) = 2! = 2 \implies \ln(2) \approx 0.6931$ * $\Gamma(4) = 3! = 6 \implies \ln(6) \approx 1.7918$ ```python import torch # Create a tensor of positive integers x = torch.tensor([1.0, 2.0, 3.0, 4.0]) # Compute the natural logarithm of the gamma function y = torch.lgamma(x) print("Input tensor x: ", x) print("lgamma(x) result:", y) ``` **Output:** ```text Input tensor x: tensor([1., 2., 3., 4.]) lgamma(x) result: tensor([0.0000, 0.0000, 0.6931, 1.7918]) ``` --- #### Example 2: In-place Operation If you want to modify the tensor in place to save memory, you can use the in-place variant `torch.lgamma_()`: ```python import torch x = torch.tensor([1.0, 2.0, 3.0, 4.0]) # Compute lgamma in-place x.lgamma_() print("In-place result:", x) ``` **Output:** ```text In-place result: tensor([0.0000, 0.0000, 0.6931, 1.7918]) ``` --- ### Key Considerations 1. **Mathematical Domain**: The gamma function $\Gamma(x)$ is undefined for non-positive integers ($0, -1, -2, \dots$). Passing these values to `torch.lgamma` will result in `inf` (infinity) or `nan` (Not a Number). 2. **Numerical Stability**: In deep learning and probabilistic modeling (such as working with Dirichlet, Gamma, or Beta distributions), you often need to compute the log-gamma function. Always use `torch.lgamma(x)` instead of `torch.log(torch.special.gammasgn(x) * torch.exp(torch.lgamma(x)))` to prevent numerical overflow when $x$ is large. 3. **Autograd Support**: `torch.lgamma` fully supports automatic differentiation. Its derivative is the digamma function ($\psi(x)$), which can be computed using `torch.digamma`.
← Pytorch Torch LinspacePytorch Torch Isin β†’