Created
February 5, 2023 11:58
-
-
Save konverner/94f819b5fbab7ce3b6745b7a228805dd to your computer and use it in GitHub Desktop.
compute gradient norm of pytorch model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def grad_norm(model: nn.Module): | |
total_norm = 0 | |
for p in model.parameters(): | |
param_norm = p.grad.detach().data.norm(2) | |
total_norm += param_norm.item() ** 2 | |
return total_norm ** 0.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment