Count parameters of a model and its submodules. Adopted from https://github.com/facebookresearch/fvcore/blob/main/fvcore/nn/parameter_count.py Args: model (nn.Module): the model to count parameters. Returns: dict[str, int]: the key is either a parameter name or a m
(model: nn.Module)
| 268 | |
| 269 | |
| 270 | def parameter_count(model: nn.Module) -> typing.DefaultDict[str, int]: |
| 271 | """Count parameters of a model and its submodules. |
| 272 | |
| 273 | Adopted from |
| 274 | https://github.com/facebookresearch/fvcore/blob/main/fvcore/nn/parameter_count.py |
| 275 | |
| 276 | Args: |
| 277 | model (nn.Module): the model to count parameters. |
| 278 | |
| 279 | Returns: |
| 280 | dict[str, int]: the key is either a parameter name or a module name. |
| 281 | The value is the number of elements in the parameter, or in all |
| 282 | parameters of the module. The key "" corresponds to the total |
| 283 | number of parameters of the model. |
| 284 | """ |
| 285 | count = defaultdict(int) # type: typing.DefaultDict[str, int] |
| 286 | for name, param in model.named_parameters(): |
| 287 | size = param.numel() |
| 288 | name = name.split('.') |
| 289 | for k in range(0, len(name) + 1): |
| 290 | prefix = '.'.join(name[:k]) |
| 291 | count[prefix] += size |
| 292 | return count |
| 293 | |
| 294 | |
| 295 | def parameter_count_table(model: nn.Module, max_depth: int = 3) -> str: |
no outgoing calls
searching dependent graphs…