Implementing exponential integral for torch tensors

Hi,
I would need to compute the exponential integral as in SciPy (scipy.special.expi — SciPy v1.6.2 Reference Guide) on a torch tensor without dettaching the gradients (so going through numpy is not an option as far as I understand).

I have seen that there is a torch.special module (torch.special — PyTorch master documentation) where some of the functions of scipy.special are being implemented. I was wondering if there are plans to implement the expi function and if somebody can give me an idea on how to proceed in the meanwhile.

I have found this notebook (Exponential_Integral_Python) which gives 2 implementations which use the quad function from scipy.integrate. Would there be a way to proceed like this using Pytorch?

Thanks.

Hi Jordi!

It is possible to use something other than pytorch (such as scipy) to
calculate things that need gradients as long as you are willing to write
a custom autograd function in which you have to provide the gradients
yourself by writing the appropriate backward() method.

In the case of the exponential integral – being the integral of an
elementary function – it is easy to calculate its derivative (unlike
some other special functions), so writing backward() is easy.

So: Write the custom autograd function, implement forward() by
calling out to scipy, and implement backward() by simply calculating
exp (-x) / x (packaged appropriately).

(Yes, calling out to scipy “detaches the gradients,” but your custom
backward() method reattaches them, so to speak.)

This is easy enough.

Of course, easier still would be to use an official pytorch version, if and
when one is implemented. (I have no idea if or when such might be
scheduled.)

Good luck.

K. Frank

Hi,

Thanks for the quick and clear answer. This seems feasible in my case.
Thank you!