Skip to content

Commit f390949

Browse files
[Term Entry] PyTorch Tensor Operations: .igamma() (#7806)
* Adding igamma * minor content fixes * Update igamma.md * Update igamma.md ---------
1 parent d778a00 commit f390949

File tree

1 file changed

+87
-0
lines changed
  • content/pytorch/concepts/tensor-operations/terms/igamma

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
Title: '.igamma()'
3+
Description: 'Computes the lower incomplete gamma function for tensor inputs.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
- 'Machine Learning'
8+
Tags:
9+
- 'AI'
10+
- 'Deep Learning'
11+
- 'Methods'
12+
- 'PyTorch'
13+
- 'Tensor'
14+
CatalogContent:
15+
- 'intro-to-py-torch-and-neural-networks'
16+
- 'paths/data-science'
17+
---
18+
19+
The **`torch.igamma()`** function in PyTorch computes the lower regularized incomplete gamma function, a special mathematical function often used in probability, statistics, and machine learning. `torch.igamma()` is an alias for `torch.special.gammainc()`. This means both functions compute the regularized lower incomplete gamma function and can be used interchangeably.
20+
21+
## Syntax
22+
23+
```pseudo
24+
torch.igamma(input, other, *, out=None)
25+
```
26+
27+
Or, alternatively:
28+
29+
```pseudo
30+
torch.special.gammainc(input, other, *, out=None)
31+
```
32+
33+
**Parameters:**
34+
35+
- `input` (Tensor): The shape parameter `a` of the Gamma function.
36+
- `other` (Tensor): The upper limit `x` of the integral.
37+
- `out` (Tensor, optional): The output tensor to store results.
38+
39+
**Return value:**
40+
41+
Returns a tensor containing the lower regularized incomplete gamma function values for each corresponding pair of elements in `input` and `other`.
42+
43+
## Example 1: Basic Element-Wise Computation
44+
45+
In this example, `torch.igamma()` computes the lower regularized incomplete gamma function for corresponding elements of two 1D tensors:
46+
47+
```py
48+
import torch
49+
50+
a = torch.tensor([2.0, 3.0, 4.0])
51+
x = torch.tensor([1.0, 2.0, 3.0])
52+
53+
result = torch.igamma(a, x)
54+
print(result)
55+
```
56+
57+
This example produces the following output:
58+
59+
```shell
60+
tensor([0.2642, 0.3233, 0.3528])
61+
```
62+
63+
## Example 2: Gamma Distribution CDF
64+
65+
In this example, `torch.igamma()` calculates the cumulative distribution function (CDF) of a Gamma distribution with shape ${a\}$ and rate 1:
66+
67+
```py
68+
import torch
69+
70+
a = torch.tensor([2.0])
71+
x = torch.linspace(0, 5, 6)
72+
gamma_cdf = torch.igamma(a, x)
73+
74+
print(gamma_cdf)
75+
```
76+
77+
The output of this code is:
78+
79+
```shell
80+
tensor([0.0000, 0.2642, 0.5940, 0.8009, 0.9084, 0.9596])
81+
```
82+
83+
The `.igamma()` function is useful for:
84+
85+
- Computing CDFs of Gamma, Chi-square, or Exponential distributions.
86+
- Performing Bayesian statistical modeling (priors and posteriors).
87+
- Implementing neural network activations and loss functions involving special functions.

0 commit comments

Comments
 (0)