Difficulties in using jacobian of torch.autograd.functional
I am solving PDE, so I need the jacobian matrix of the residual with respect to variables.
Here is my code
import torch
from torch.autograd.functional import jacobian
def get_residual (pgnew, swnew):
    residual_w  = 5*(swnew-swold)+T_w*((pgnew[2:,:,:]-pgnew[1:-1,:,:])-(pc[2:,:,:]-pc[1:-1,:,:])) - T_w*((pgnew[1:-1,:,:]-pgnew[0:-2,:,:])-(pc[1:-1,:,:]-pc[0:-2,:,:]))
    residual_g  = 5*((1-swnew)-(1-swold))+T_g*(pgnew[2:,:,:]-pgnew[1:-1,:,:]) - T_g*(pgnew[1:-1,:,:]-pgnew[0:-2,:,:])
    residual    = torch.ravel(torch.column_stack((residual_w,residual_g)))
    return residual

if __name__ == '__main__':
    dt       = 0.01
    T_w      = 10 
    T_g      = 12
    swnew     = torch.zeros(3, 1, 1, requires_grad=True, dtype=torch.float64)   
    swold     = torch.ones(3, 1, 1, requires_grad=True, dtype=torch.float64)     
    pgnew     = 2*torch.ones(5, 1, 1, requires_grad=True, dtype=torch.float64)
    pc        = 3*torch.ones(5, 1, 1, requires_grad=True, dtype=torch.float64)
    

    unknown  = torch.ravel(torch.column_stack((pgnew[1:-1,:,:],swnew)))
    
    residual = get_residual(pgnew, swnew)
    print('Check Jacobian \n', jacobian(get_residual, unknown))


I am following this tutorial;
however, it shows an error, namely,

get_residual() missing 1 required positional argument: 'swnew',

so I change it to
print('Check Jacobian \n', jacobian(get_residual(pgnew, swnew),unknown)

Then it shows 'Tensor' object is not callable'
Thank you

You have to give the jacobian a tuple as second argument, containing the arguments of get_residual:
print('Check Jacobian \n', jacobian(get_residual, (pgnew, swnew)))

(You may need to make sure that they have the shape you want etc.)
