PyTorch .lgamma()
Published Oct 30, 2025
Contribute to Docs
In PyTorch, the .lgamma() function returns the natural logarithm of the absolute value of the gamma function for each element in an input tensor.
Syntax
torch.lgamma(input, *, out=None) → Tensor
Parameters:
input(Tensor): Input tensor of floating point or complex numbers.out(Optional): Output tensor to write results to.
Return value:
Returns a tensor of the same shape as the input, where each element is the log of the absolute gamma function of the corresponding input element.
Example
The example demonstrates computing the natural logarithm of the absolute gamma function using both the functional form torch.lgamma() and the tensor method .lgamma(), then verifies both approaches produce identical results:
import torchx = torch.tensor([0.5, 1.0, 2.5, 5.0, -0.5])# Functional formy1 = torch.lgamma(x)# Tensor methody2 = x.lgamma()print(y1)print(torch.allclose(y1, y2))
Here’s the output of this code:
tensor([0.5724, 0.0000, 0.2847, 3.1781, 1.2655])True
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
- Intermediate.3 hours