-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from lab-cosmo/exponents
Add general integer exponents
- Loading branch information
Showing
9 changed files
with
162 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import torch | ||
from scipy.special import exp1 | ||
from torch.special import gammaln | ||
|
||
|
||
def gamma(x: torch.Tensor) -> torch.Tensor: | ||
""" | ||
(Complete) Gamma function. | ||
pytorch has not implemented the commonly used (complete) Gamma function. We define | ||
it in a custom way to make autograd work as in | ||
https://discuss.pytorch.org/t/is-there-a-gamma-function-in-pytorch/17122 | ||
""" | ||
return torch.exp(gammaln(x)) | ||
|
||
|
||
class CustomExp1(torch.autograd.Function): | ||
"""Custom exponential integral function Exp1(x) to have an autograd-compatible version.""" | ||
|
||
@staticmethod | ||
def forward(ctx, input): | ||
ctx.save_for_backward(input) | ||
input_numpy = input.cpu().numpy() if not input.is_cpu else input.numpy() | ||
return torch.tensor(exp1(input_numpy), device=input.device, dtype=input.dtype) | ||
|
||
@staticmethod | ||
def backward(ctx, grad_output): | ||
(input,) = ctx.saved_tensors | ||
return -grad_output * torch.exp(-input) / input | ||
|
||
|
||
def torch_exp1(input): | ||
"""Wrapper for the custom exponential integral function.""" | ||
return CustomExp1.apply(input) | ||
|
||
|
||
def gammaincc_over_powerlaw(exponent: torch.Tensor, z: torch.Tensor) -> torch.Tensor: | ||
"""Function to compute the regularized incomplete gamma function complement for integer exponents.""" | ||
if exponent == 1: | ||
return torch.exp(-z) / z | ||
if exponent == 2: | ||
return torch.sqrt(torch.pi / z) * torch.erfc(torch.sqrt(z)) | ||
if exponent == 3: | ||
return torch_exp1(z) | ||
if exponent == 4: | ||
return 2 * ( | ||
torch.exp(-z) - torch.sqrt(torch.pi * z) * torch.erfc(torch.sqrt(z)) | ||
) | ||
if exponent == 5: | ||
return torch.exp(-z) - z * torch_exp1(z) | ||
if exponent == 6: | ||
return ( | ||
(2 - 4 * z) * torch.exp(-z) | ||
+ 4 * torch.sqrt(torch.pi * z**3) * torch.erfc(torch.sqrt(z)) | ||
) / 3 | ||
raise ValueError(f"Unsupported exponent: {exponent}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import numpy as np | ||
import torch | ||
from scipy.special import exp1 | ||
|
||
from torchpme.lib import torch_exp1 | ||
|
||
|
||
def finite_difference_derivative(func, x, h=1e-5): | ||
return (func(x + h) - func(x - h)) / (2 * h) | ||
|
||
|
||
def test_torch_exp1_consistency_with_scipy(): | ||
x = torch.rand(1000, dtype=torch.float64) | ||
torch_result = torch_exp1(x) | ||
scipy_result = exp1(x.numpy()) | ||
assert np.allclose(torch_result.numpy(), scipy_result, atol=1e-6) | ||
|
||
|
||
def test_torch_exp1_derivative(): | ||
x = torch.rand(1, dtype=torch.float64, requires_grad=True) | ||
torch_result = torch_exp1(x) | ||
torch_result.backward() | ||
torch_exp1_prime = x.grad | ||
finite_diff_result = finite_difference_derivative(exp1, x.detach().numpy()) | ||
assert np.allclose(torch_exp1_prime.numpy(), finite_diff_result, atol=1e-6) |
Oops, something went wrong.