Hi Tdm!



 tdm:

I have written a custom torch.autograd.Function.
The forward function is complete, and is solely a number of tensor operations that transform the set of parameter inputs (matrix multiplication, argmax, concatenation etc.).
…
How can I implement the .backward() function in this case?


If your function consists solely of (usefully-differentiable) pytorch
tensor operations you don’t need to package it as an official
torch.autograd.Function nor write an explicit backward()
function; autograd will work “automatically” when you call backward()
on your final result (typically your loss).
So (just to make something up):
def my_function (input, params1, params2):
    return (params1 * input + params2 * input**2).sum (dim = 1)

autograd can “backward” through my_function() without you needing
to write any further code.
Best.
K. Frank