From aa83cf38516a538e792954cb66ba0c3ebdc34eed Mon Sep 17 00:00:00 2001 From: Priyanshjain10 Date: Thu, 30 Oct 2025 14:39:03 +0530 Subject: [PATCH 1/5] docs: Add PyTorch .polygamma() tensor operation documentation Created comprehensive documentation entry for PyTorch's .polygamma() tensor operation function including: - Complete front matter with metadata (subjects, tags, catalog content) - Clear definition explaining the polygamma function - Detailed syntax section with parameter descriptions - Three practical code examples: * Digamma function (n=0) * Trigamma function (n=1) * Higher order polygamma (n=2) - Usage notes for statistical applications - Links to related tensor operation functions This documentation follows Codecademy's term entry template and markdown style guide. Fixes #7849 Submitted for Hacktoberfest 2025. --- .../terms/polygamma/polygamma.md | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md diff --git a/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md b/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md new file mode 100644 index 00000000000..db18f540de8 --- /dev/null +++ b/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md @@ -0,0 +1,106 @@ +--- +Title: '.polygamma()' +Description: 'Computes the polygamma function of input tensors element-wise' +Subjects: + - 'AI' + - 'Data Science' + - 'Machine Learning' +Tags: + - 'Functions' + - 'PyTorch' + - 'Tensor Operations' +CatalogContent: + - 'intro-to-pytorch' + - 'paths/data-science' +--- + +## Definition + +The **`.polygamma()`** function in PyTorch computes the polygamma function of input tensors element-wise. The polygamma function is the (n+1)-th derivative of the logarithm of the gamma function, where n is a non-negative integer. + +## Syntax + +```py +torch.polygamma(n, input, *, out=None) → Tensor +``` + +### Parameters + +- `n` (int): The order of the polygamma function. When `n=0`, this is the digamma function; when `n=1`, this is the trigamma function. +- `input` (Tensor): The input tensor containing values for which to compute the polygamma function. +- `out` (Tensor, optional): The output tensor to store the result. Default is `None`. + +### Returns + +A tensor containing the computed polygamma values with the same shape as `input`. + +## Example 1: Digamma Function (n=0) + +The digamma function is the derivative of the log-gamma function: + +```py +import torch + +# Create a tensor +x = torch.tensor([1.0, 2.0, 3.0, 4.0]) + +# Compute digamma (polygamma with n=0) +digamma_values = torch.polygamma(0, x) +print(digamma_values) +``` + +Output: + +```shell +tensor([-0.5772, 0.4228, 0.9228, 1.2561]) +``` + +## Example 2: Trigamma Function (n=1) + +The trigamma function is the second derivative of the log-gamma function: + +```py +import torch + +# Create input tensor +x = torch.tensor([1.0, 2.0, 3.0]) + +# Compute trigamma (polygamma with n=1) +trigamma_values = torch.polygamba(1, x) +print(trigamma_values) +``` + +Output: + +```shell +tensor([1.6449, 0.6449, 0.3949]) +``` + +## Example 3: Higher Order Polygamma + +```py +import torch + +# Compute polygamma of order 2 +x = torch.tensor([2.0, 3.0, 4.0]) +polygamma_2 = torch.polygamma(2, x) +print(polygamma_2) +``` + +Output: + +```shell +tensor([-0.8224, -0.3540, -0.2164]) +``` + +## Notes + +- The polygamma function is commonly used in statistical inference, particularly in Bayesian statistics and variational inference. +- Input values should typically be positive for meaningful results. +- The function supports automatic differentiation for gradient-based optimization. + +## Related Functions + +- [`.lgamma()`](/pytorch/concepts/tensor-operations/terms/lgamma): Computes the natural logarithm of the absolute value of the gamma function +- [`.digamma()`](/pytorch/concepts/tensor-operations/terms/digamma): Computes the digamma function (equivalent to `.polygamma(0, input)`) +- [`.mvlgamma()`](/pytorch/concepts/tensor-operations/terms/mvlgamma): Computes the multivariate log-gamma function From 1882f224575a5a82bf3f8958ead9a37aa93d274b Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 31 Oct 2025 12:52:16 +0530 Subject: [PATCH 2/5] Update polygamma.md --- .../terms/polygamma/polygamma.md | 34 ++++++------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md b/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md index db18f540de8..31ab96215be 100644 --- a/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md +++ b/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md @@ -1,6 +1,6 @@ --- Title: '.polygamma()' -Description: 'Computes the polygamma function of input tensors element-wise' +Description: 'Computes the n-th derivative of the digamma function for each element in the input tensor.' Subjects: - 'AI' - 'Data Science' @@ -14,29 +14,27 @@ CatalogContent: - 'paths/data-science' --- -## Definition - The **`.polygamma()`** function in PyTorch computes the polygamma function of input tensors element-wise. The polygamma function is the (n+1)-th derivative of the logarithm of the gamma function, where n is a non-negative integer. ## Syntax -```py +```pseudo torch.polygamma(n, input, *, out=None) → Tensor ``` -### Parameters +**Parameters:** - `n` (int): The order of the polygamma function. When `n=0`, this is the digamma function; when `n=1`, this is the trigamma function. - `input` (Tensor): The input tensor containing values for which to compute the polygamma function. - `out` (Tensor, optional): The output tensor to store the result. Default is `None`. -### Returns +**Return value:** A tensor containing the computed polygamma values with the same shape as `input`. ## Example 1: Digamma Function (n=0) -The digamma function is the derivative of the log-gamma function: +In this example, `.polygamma()` is used with `n=0` to compute the digamma function (first derivative of the log-gamma function): ```py import torch @@ -49,7 +47,7 @@ digamma_values = torch.polygamma(0, x) print(digamma_values) ``` -Output: +The output of this code is: ```shell tensor([-0.5772, 0.4228, 0.9228, 1.2561]) @@ -57,7 +55,7 @@ tensor([-0.5772, 0.4228, 0.9228, 1.2561]) ## Example 2: Trigamma Function (n=1) -The trigamma function is the second derivative of the log-gamma function: +In this example, `.polygamma()` is used with `n=1` to compute the trigamma function (second derivative of the log-gamma function): ```py import torch @@ -70,7 +68,7 @@ trigamma_values = torch.polygamba(1, x) print(trigamma_values) ``` -Output: +The output of this code is: ```shell tensor([1.6449, 0.6449, 0.3949]) @@ -78,6 +76,8 @@ tensor([1.6449, 0.6449, 0.3949]) ## Example 3: Higher Order Polygamma +In this example, `.polygamma()` is used with `n=2` to compute the second-order polygamma function (derivative of the trigamma function): + ```py import torch @@ -87,20 +87,8 @@ polygamma_2 = torch.polygamma(2, x) print(polygamma_2) ``` -Output: +The output of this code is: ```shell tensor([-0.8224, -0.3540, -0.2164]) ``` - -## Notes - -- The polygamma function is commonly used in statistical inference, particularly in Bayesian statistics and variational inference. -- Input values should typically be positive for meaningful results. -- The function supports automatic differentiation for gradient-based optimization. - -## Related Functions - -- [`.lgamma()`](/pytorch/concepts/tensor-operations/terms/lgamma): Computes the natural logarithm of the absolute value of the gamma function -- [`.digamma()`](/pytorch/concepts/tensor-operations/terms/digamma): Computes the digamma function (equivalent to `.polygamma(0, input)`) -- [`.mvlgamma()`](/pytorch/concepts/tensor-operations/terms/mvlgamma): Computes the multivariate log-gamma function From 45c844443c95082bf8863a24473dd742a4f74194 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 31 Oct 2025 12:55:13 +0530 Subject: [PATCH 3/5] Update polygamma.md --- .../concepts/tensor-operations/terms/polygamma/polygamma.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md b/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md index 31ab96215be..d364a849d3f 100644 --- a/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md +++ b/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md @@ -64,7 +64,7 @@ import torch x = torch.tensor([1.0, 2.0, 3.0]) # Compute trigamma (polygamma with n=1) -trigamma_values = torch.polygamba(1, x) +trigamma_values = torch.polygamma(1, x) print(trigamma_values) ``` From 99c31914c8c4b2f11bd9addb5ac069997bd093b1 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 31 Oct 2025 12:56:21 +0530 Subject: [PATCH 4/5] Update polygamma.md --- .../concepts/tensor-operations/terms/polygamma/polygamma.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md b/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md index d364a849d3f..f429e5c723d 100644 --- a/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md +++ b/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/data-science' --- -The **`.polygamma()`** function in PyTorch computes the polygamma function of input tensors element-wise. The polygamma function is the (n+1)-th derivative of the logarithm of the gamma function, where n is a non-negative integer. +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, where `n` is a non-negative integer ## Syntax From 4244204b55c462932fa309d7b7c32f0f59be5592 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 31 Oct 2025 14:33:19 +0530 Subject: [PATCH 5/5] Update polygamma.md --- .../concepts/tensor-operations/terms/polygamma/polygamma.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md b/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md index f429e5c723d..3c80fbc122e 100644 --- a/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md +++ b/content/pytorch/concepts/tensor-operations/terms/polygamma/polygamma.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/data-science' --- -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, where `n` is a non-negative integer +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. ## Syntax