Created
November 27, 2017 20:39
-
-
Save zackenton/12a86b6e0ff274b39608e40f4a412f2b to your computer and use it in GitHub Desktop.
Count number trainable parameters in a 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 pytorch_count_params(model): | |
"count number trainable parameters in a pytorch model" | |
total_params = sum(reduce( lambda a, b: a*b, x.size()) for x in model.parameters()) | |
return total_params |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can find
reduce
infrom functools import reduce
I assume
or you can use simpler version (without reduce):
total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)