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

Faster Gradient Penalty Calculation #14

Open
AhmedThahir opened this issue Dec 8, 2024 · 0 comments
Open

Faster Gradient Penalty Calculation #14

AhmedThahir opened this issue Dec 8, 2024 · 0 comments

Comments

@AhmedThahir
Copy link

This is referring to: https://github.com/t-vi/pytorch-tvmisc/blob/master/misc/gradient_regularized_gan_gaussian_toy_problem.ipynb

Current Code

grads = torch.autograd.grad(scores, discriminator.parameters(), retain_graph=True, create_graph=True)
gr_norm_sq = 0.0
for gr in grads:
    gr_norm_sq += (gr**2).sum()

Suggestion

grads_norm_penalty = (
	torch.concat(
		[g.view(-1) for g in grads]
	)
	.norm(p=2)
)

Question

What is your opinion on this? This tries to batch operations as much as possible.

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