Skip to content

Count the total number of parameters in a model #1134

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

Merged
merged 8 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ popular datasets for natural language.
experimental_transforms
experimental_vectors
experimental_vocab
models_utils
examples <examples>

.. automodule:: torchtext
Expand Down
13 changes: 13 additions & 0 deletions docs/source/models_utils.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.. role:: hidden
:class: hidden-section

torchtext.experimental.models.utils
===================================

.. automodule:: torchtext.experimental.models.utils
.. currentmodule:: torchtext.experimental.models.utils

:hidden:`count_model_param`
~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autofunction:: count_model_param
Empty file added test/models/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions test/models/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import torch
from torchtext.experimental.models.utils import count_model_param
from ..common.torchtext_test_case import TorchtextTestCase


class TestModelsUtils(TorchtextTestCase):
def test_count_model_parameters_func(self):
model = torch.nn.Embedding(100, 200)
self.assertEqual(count_model_param(model, unit=10**3), 20.0)
3 changes: 2 additions & 1 deletion torchtext/experimental/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from . import datasets
from . import transforms
from . import models

__all__ = ['datasets', 'transforms']
__all__ = ['datasets', 'transforms', 'models']
3 changes: 3 additions & 0 deletions torchtext/experimental/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .utils import count_model_param

__all__ = ["count_model_param"]
22 changes: 22 additions & 0 deletions torchtext/experimental/models/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import torch


def count_model_param(nn_model, unit=10**6):
r"""
Count the parameters in a model

Args:
model: the model (torch.nn.Module)
unit: the unit of the returned value. Default: 10**6 or M.

Examples:
>>> import torch
>>> import torchtext
>>> from torchtext.experimental.models.utils import count_model_param
>>> model = torch.nn.Embedding(100, 200)
>>> count_model_param(model, unit=10**3)
>>> 20.
"""
model_parameters = filter(lambda p: p.requires_grad, nn_model.parameters())
params = sum([torch.prod(torch.tensor(p.size())) for p in model_parameters])
return params.item() / unit