|
| 1 | +--- |
| 2 | +Title: '.polygamma()' |
| 3 | +Description: 'Computes the n-th derivative of the digamma function for each element in the input tensor.' |
| 4 | +Subjects: |
| 5 | + - 'AI' |
| 6 | + - 'Data Science' |
| 7 | + - 'Machine Learning' |
| 8 | +Tags: |
| 9 | + - 'Functions' |
| 10 | + - 'PyTorch' |
| 11 | + - 'Tensor Operations' |
| 12 | +CatalogContent: |
| 13 | + - 'intro-to-pytorch' |
| 14 | + - 'paths/data-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`.polygamma()`** function in PyTorch computes the polygamma function of input tensors element-wise. The polygamma function is the n-th derivative of the [digamma function](https://www.codecademy.com/resources/docs/pytorch/tensor-operations/digamma), where `n` is a non-negative integer. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +torch.polygamma(n, input, *, out=None) → Tensor |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `n` (int): The order of the polygamma function. When `n=0`, this is the digamma function; when `n=1`, this is the trigamma function. |
| 28 | +- `input` (Tensor): The input tensor containing values for which to compute the polygamma function. |
| 29 | +- `out` (Tensor, optional): The output tensor to store the result. Default is `None`. |
| 30 | + |
| 31 | +**Return value:** |
| 32 | + |
| 33 | +A tensor containing the computed polygamma values with the same shape as `input`. |
| 34 | + |
| 35 | +## Example 1: Digamma Function (n=0) |
| 36 | + |
| 37 | +In this example, `.polygamma()` is used with `n=0` to compute the digamma function (first derivative of the log-gamma function): |
| 38 | + |
| 39 | +```py |
| 40 | +import torch |
| 41 | + |
| 42 | +# Create a tensor |
| 43 | +x = torch.tensor([1.0, 2.0, 3.0, 4.0]) |
| 44 | + |
| 45 | +# Compute digamma (polygamma with n=0) |
| 46 | +digamma_values = torch.polygamma(0, x) |
| 47 | +print(digamma_values) |
| 48 | +``` |
| 49 | + |
| 50 | +The output of this code is: |
| 51 | + |
| 52 | +```shell |
| 53 | +tensor([-0.5772, 0.4228, 0.9228, 1.2561]) |
| 54 | +``` |
| 55 | + |
| 56 | +## Example 2: Trigamma Function (n=1) |
| 57 | + |
| 58 | +In this example, `.polygamma()` is used with `n=1` to compute the trigamma function (second derivative of the log-gamma function): |
| 59 | + |
| 60 | +```py |
| 61 | +import torch |
| 62 | + |
| 63 | +# Create input tensor |
| 64 | +x = torch.tensor([1.0, 2.0, 3.0]) |
| 65 | + |
| 66 | +# Compute trigamma (polygamma with n=1) |
| 67 | +trigamma_values = torch.polygamma(1, x) |
| 68 | +print(trigamma_values) |
| 69 | +``` |
| 70 | + |
| 71 | +The output of this code is: |
| 72 | + |
| 73 | +```shell |
| 74 | +tensor([1.6449, 0.6449, 0.3949]) |
| 75 | +``` |
| 76 | + |
| 77 | +## Example 3: Higher Order Polygamma |
| 78 | + |
| 79 | +In this example, `.polygamma()` is used with `n=2` to compute the second-order polygamma function (derivative of the trigamma function): |
| 80 | + |
| 81 | +```py |
| 82 | +import torch |
| 83 | + |
| 84 | +# Compute polygamma of order 2 |
| 85 | +x = torch.tensor([2.0, 3.0, 4.0]) |
| 86 | +polygamma_2 = torch.polygamma(2, x) |
| 87 | +print(polygamma_2) |
| 88 | +``` |
| 89 | + |
| 90 | +The output of this code is: |
| 91 | + |
| 92 | +```shell |
| 93 | +tensor([-0.8224, -0.3540, -0.2164]) |
| 94 | +``` |
0 commit comments