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

calculate param problem #72

Closed
WZMIAOMIAO opened this issue Jun 3, 2021 · 2 comments
Closed

calculate param problem #72

WZMIAOMIAO opened this issue Jun 3, 2021 · 2 comments

Comments

@WZMIAOMIAO
Copy link

Hi, I find that the results of fvcore.nn.prameter_count function and tensorflow are different when calculate same model.
The difference comes from the BN layer. tensorflow calculate beta, gamma, moving_mean and variance(four params), but fvcore.nn.prameter_count only calculate beta, gamma.

for example:

import torch
from fvcore.nn import parameter_count_table


def main():
    model = torch.nn.BatchNorm2d(1)
    print(parameter_count_table(model))


if __name__ == '__main__':
    main()

result:

| name    | #elements or shape   |
|:--------|:---------------------|
| model   | 2                    |
|  weight |  (1,)                |
|  bias   |  (1,)                |

only calculate beta, gamma.

In https://github.com/facebookresearch/fvcore/blob/master/fvcore/nn/parameter_count.py#L10-L30

r = defaultdict(int)
for name, prm in model.named_parameters():
    size = prm.numel()
    name = name.split(".")
    for k in range(0, len(name) + 1):
        prefix = ".".join(name[:k])

using model.named_parameters() only get beta, gamma no moving_mean and variance.

Are there any other considerations?

@ppwwyyxx
Copy link
Contributor

ppwwyyxx commented Jun 3, 2021

In pytorch terminology, technically the moving mean and variance are "buffers" but not "parameters".

In general, it's hard to automatically decide for a module whether its "buffers" should be included in parameter count, since in many other modules they are not useful at all after training. Maybe an option can be added to control whether to include all buffers or no buffers in the count. Other suggestions are welcome.

@WZMIAOMIAO
Copy link
Author

thank for your reply. Maybe adding an option is better.

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

2 participants