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

[Feature Request] Return Number of Model Parameters #3166

Closed
isaykatsman opened this issue Oct 18, 2017 · 4 comments
Closed

[Feature Request] Return Number of Model Parameters #3166

isaykatsman opened this issue Oct 18, 2017 · 4 comments

Comments

@isaykatsman
Copy link

Would be nice to call model.num_params() and be able to obtain the number of trainable parameters of a model. Especially useful during model summary printing. The PR should probably reference this: https://discuss.pytorch.org/t/finding-the-total-number-of-trainable-parameters-in-a-graph/1751/2

@soumith
Copy link
Member

soumith commented Oct 18, 2017

this can be trivially done with a line of code.

sum([p.numel() for p in model.parameters()])

I dont see any benefit of having an extra function for this.
I think this is more unnecessary because listing the number of parameters of a model is not something you repeatedly do 100s of times over your code. It's usually just a single line for logging purposes.

@apaszke apaszke closed this as completed Oct 18, 2017
@dpappas
Copy link

dpappas commented Aug 27, 2019

This will not work for any model.

for example:

class MLP(nn.Module):
    def __init__(self, input_dim=None, sizes=None, activation_functions=None):
        super(MLP, self).__init__()
        ################################
        sizes               = [input_dim]+ sizes
        self.activations    = activation_functions
        self.linears        = []
        for i in range(len(sizes)-1):
            self.linears.append(nn.Linear(sizes[i], sizes[i+1]))
        ################################
    def forward(self, features):
        ret = features
        for layer, activation in zip(self.linears, self.activations):
            ret = layer(ret)
            if(activation is not None):
                ret = activation(ret)
        return ret

my_mlp = MLP(100, [10, 1], [None, None])
print(sum([p.numel() for p in model.parameters()]))

@soumith
Copy link
Member

soumith commented Aug 27, 2019

@dpappas that's because model.parameters() in your case will return an incomplete set of parameters. Do self.linears = nn.ModuleList() instead of []

@dpappas
Copy link

dpappas commented Aug 28, 2019

Perfect! Thank you very much.

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

4 participants