Hi Jordi!



 inglada:

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).


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 2 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.


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.


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