A problem about "autograd" and "special_bessel_j0"

Hi, everyone. I plan to calculate the derivative of the Bessel function via the “autograd” function, the code is

but I received an error:

If I add the “requires_grad_(true)” to the “y”, just like this:

I also received an error:

So I feel so confused. How can I calculate the derivatives normally? Thank you!

Hi Rui!

As near as I can tell, pytorch does not implement backward() for
torch.special.bessel_j0() (nor torch.special.bessel_y0()),
although I cannot find this explicitly documented anywhere.

It seems like a number of functions in torch.special don’t have
backward() implemented. The only related information I could find
is this quote from the torch.special tracking issue:

Would a backward method to Function be needed for kv (both with respect to order nu and argument z)?

Implementing support for autograd isn’t always required, but if the function is differentiable that’d be great!

(So somebody was talking about implementing backward() for some of
the bessel functions, implying that it hadn’t been done yet.)

In general, setting requires_grad = True to fix an error is a mistake. In
your case the result of bessel_y0 (x) doesn’t carry requires_grad = True
and is not connected by any computation graph to the operation,
bessel_y0 (x), that produced it. Setting requires_grad = True
makes y a leaf of what could become a new computation graph but does
not reconnect it to any existing graph.

The derivatives of j0() and y0() can be expressed in terms of j1()
and y1(), respectively, and these are implemented in torch.special.
So if you just need the derivative, use j1() (or y1()) to evaluate it.

If, instead, you need to be able to use autograd with, say, j0(), you
could write a custom autograd function for j0() that simply uses
pytorch’s j0() for its forward() method and implements its backward()
method by using pytorch’s j1() to compute j0()'s derivative. (Doing so
could be adequate for your use case, but could have numerical issues
for some ranges of x.)

As an aside, please don’t post screenshots of textual information. Doing
so breaks accessibility, searchability, and copy-paste.

Best.

K. Frank

Hi,Frank!Thank you for your patient explanation and suggestions,Implementing the backward method seems a great way to solve the issue, I will give it a try!

Best.
Rui