Implementing exponential integral for torch tensors

inglada: <p>Hi,<br>
I would need to compute the exponential integral as in SciPy (<a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.expi.html" class="inline-onebox" rel="noopener nofollow ugc">scipy.special.expi — SciPy v1.6.2 Reference Guide</a>) on a torch tensor without dettaching the gradients (so going through numpy is not an option as far as I understand).</p>
<p>I have seen that there is a torch.special module (<a href="https://pytorch.org/docs/master/special.html" class="inline-onebox" rel="noopener nofollow ugc">torch.special — PyTorch master documentation</a>) 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.</p>
<p>I have found this notebook (<a href="https://perso.crans.org/besson/publis/notebooks/Exponential_Integral_Python.html" class="inline-onebox" rel="noopener nofollow ugc">Exponential_Integral_Python</a>) which gives 2 implementations which use the quad function from scipy.integrate. Would there be a way to proceed like this using Pytorch?</p>
<p>Thanks.</p>

KFrank: <p>Hi Jordi!</p>
<aside class="quote no-group quote-modified" data-username="inglada" data-post="1" data-topic="119196" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/ba9def/48.png" class="avatar"> inglada:</div>
<blockquote>
<p>I would need to compute the exponential integral as in SciPy (<a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.expi.html" class="inline-onebox" rel="noopener nofollow ugc">scipy.special.expi — SciPy v1.11.4 Manual</a>) on a torch tensor without dettaching the gradients (so going through numpy is not an option as far as I understand).</p>
</blockquote>
</aside>
<p>It is possible to use something other than pytorch (such as scipy) to<br>
calculate things that need gradients as long as you are willing to write<br>
a <a href="https://pytorch.org/docs/1.8.1/autograd.html#function" rel="noopener nofollow ugc">custom autograd function</a> in which you have to provide the gradients<br>
yourself by writing the appropriate <code>backward()</code> method.</p>
<p>In the case of the exponential integral – being the integral of an<br>
elementary function – it is easy to calculate its derivative (unlike<br>
some other special functions), so writing <code>backward()</code> is easy.</p>
<p>So:  Write the custom autograd function, implement <code>forward()</code> by<br>
calling out to scipy, and implement <code>backward()</code> by simply calculating<br>
<code>exp (-x) / x</code> (packaged appropriately).</p>
<p>(Yes, calling out to scipy “detaches the gradients,” but your custom<br>
<code>backward()</code> method reattaches them, so to speak.)</p>
<p>This is easy enough.</p>
<aside class="quote no-group">
<blockquote>
<p>I have seen that there is a torch.special module (<a href="https://pytorch.org/docs/master/special.html" class="inline-onebox" rel="noopener nofollow ugc">torch.special — PyTorch master documentation</a>) where some of the functions of scipy.special are being implemented.</p>
</blockquote>
</aside>
<p>Of course, easier still would be to use an official pytorch version, if and<br>
when one is implemented.  (I have no idea if or when such might be<br>
scheduled.)</p>
<p>Good luck.</p>
<p>K. Frank</p>

inglada: <p>Hi,</p>
<p>Thanks for the quick and clear answer. This seems feasible in my case.<br>
Thank you!</p>