How to destroy computational graph after setting create_graph to True?


Can I ask how to destroy the computational graph after setting create_graph to True?

Say that I have:

x = torch.randn(10,5)
y = torch.randn(10,5)

def f(x,y):
    return torch.sum(x*y*y)

def jacobian_matrix_with_graph(x, y):
    jac = torch.autograd.functional.jacobian(f,(x,y),create_graph=True)
    return jac

def partial_hes(y): # only compute hessian matrix 1. f /partial_x /partial_y 2. f /partial_x /partial_y
    hes = jacobian_matrix_with_graph(x, y) # x being global variable
    return hes
hessian_matrix_pxpy = torch.autograd.functional.jacobian(partial_hes,(y))[1]

After I have used this computation graph, how do I destroy it manually?

Thanks in advance.

There is no way to manually “destroy” the graph. It will be freed automatically once all references to it are gone. If you wish to obtain a tensor with the value of the output, but no longer attached to this graph, you can use detach