Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some confusion about jacobian_trace #1

Open
HoJ-Onle opened this issue Sep 1, 2022 · 0 comments
Open

Some confusion about jacobian_trace #1

HoJ-Onle opened this issue Sep 1, 2022 · 0 comments

Comments

@HoJ-Onle
Copy link

HoJ-Onle commented Sep 1, 2022

Hello. It's a nice work!
I have some confusion about the implementation of the jacobian_trace function. The jacobian_trace function used the function keep_grad which has set create_graph=True. It doesn't seem like we have a need for higher gradient. It had a very high demand on my GPU memory when I was running some larger datasets. I want to know if I can set it to False and only keep retain_graph=True.

def keep_grad(output, input, grad_outputs=None):
    return torch.autograd.grad(output, input, grad_outputs=grad_outputs, retain_graph=True, create_graph=True)[0]

def exact_jacobian_trace(fx, x):
    vals = []
    for i in range(x.size(1)):
        fxi = fx[:, i]
        dfxi_dxi = keep_grad(fxi.sum(), x)[:, i][:, None]
        vals.append(dfxi_dxi)
    vals = torch.cat(vals, dim=1)
    return vals.sum(dim=1)

Looking forward to your relpy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant