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

Performance improvement #3

Open
htt210 opened this issue Jul 24, 2019 · 0 comments
Open

Performance improvement #3

htt210 opened this issue Jul 24, 2019 · 0 comments

Comments

@htt210
Copy link

htt210 commented Jul 24, 2019

I found that performing forward-backward in a loop is much faster than using autograd.grad with retain_graph=True. Your current code is:

        loglikelihood_grads = zip(*[autograd.grad(
            l, self.parameters(),
            retain_graph=(i < len(loglikelihoods))
        ) for i, l in enumerate(loglikelihoods, 1)])

https://github.com/kuc2477/pytorch-ewc/blob/master/model.py#L75

after I change it to:

        for batch, label in zip(buffer_data, buffer_label):
            self.zero_grad()
            loglikelihood = F.cross_entropy(self(batch), label)
            loglikelihood.backward()
            for n, p in self.named_parameters():
                n = n.replace('.', '__')
                grads[n] = grads.get(n, 0) + p.grad ** 2

it runs much faster. Can you please investigate this?

Thank you.
Thanh Tung

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