Hi,

So how am I supposed to pass a trained network module to a function?

I’m not sure to understand the question. You can pass your model as any other parameter to the function.

Is there any more elegant way for this?

You can use torch.autograd.grad() to get the gradients wrt the input directly. (And avoid populating the .grad fields of all the weights in your model).