Skip to content

Commit e4e9092

Browse files
authored
[Term Entry] PyTorch Tensor Operations: .lgamma() (#7619)
* [Term Entry] PyTorch Tensor Operations: .lgamma() * minor wording fixes * Apply suggestion from @avdhoottt * Apply suggestion from @avdhoottt * Apply suggestion from @avdhoottt ---------
1 parent f1dbc49 commit e4e9092

File tree

1 file changed

+58
-0
lines changed
  • content/pytorch/concepts/tensor-operations/terms/lgamma

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
Title: '.lgamma()'
3+
Description: 'Computes the natural log of the absolute gamma function for each element in a tensor.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Functions'
9+
- 'Math'
10+
- 'PyTorch'
11+
- 'Tensor'
12+
CatalogContent:
13+
- 'intro-to-py-torch-and-neural-networks'
14+
- 'paths/data-science'
15+
---
16+
17+
In PyTorch, the **`.lgamma()`** function returns the natural logarithm of the absolute value of the gamma function for each element in an input [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors).
18+
19+
## Syntax
20+
21+
```pseudo
22+
torch.lgamma(input, *, out=None) → Tensor
23+
```
24+
25+
**Parameters:**
26+
27+
- `input` (Tensor): Input tensor of floating point or complex numbers.
28+
- `out` (Optional): Output tensor to write results to.
29+
30+
**Return value:**
31+
32+
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.
33+
34+
## Example
35+
36+
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:
37+
38+
```py
39+
import torch
40+
41+
x = torch.tensor([0.5, 1.0, 2.5, 5.0, -0.5])
42+
43+
# Functional form
44+
y1 = torch.lgamma(x)
45+
46+
# Tensor method
47+
y2 = x.lgamma()
48+
49+
print(y1)
50+
print(torch.allclose(y1, y2))
51+
```
52+
53+
Here's the output of this code:
54+
55+
```shell
56+
tensor([0.5724, 0.0000, 0.2847, 3.1781, 1.2655])
57+
True
58+
```

0 commit comments

Comments
 (0)