espnet2.torch_utils.model_summary.model_summary
Less than 1 minute
espnet2.torch_utils.model_summary.model_summary
espnet2.torch_utils.model_summary.model_summary(model: Module) → str
Generate a summary of the given PyTorch model, including the total number of
parameters, trainable parameters, and model structure.
This function inspects the provided PyTorch model and returns a string summary containing important information about the model’s architecture, including the class name, total number of parameters, number of trainable parameters, the percentage of trainable parameters, the size of the model in bytes, and the data type of the model parameters.
- Parameters:model (torch.nn.Module) – The PyTorch model to summarize.
- Returns: A formatted string containing the model summary.
- Return type: str
Examples
>>> import torch
>>> class SimpleModel(torch.nn.Module):
... def __init__(self):
... super(SimpleModel, self).__init__()
... self.fc = torch.nn.Linear(10, 5)
...
... def forward(self, x):
... return self.fc(x)
>>> model = SimpleModel()
>>> print(model_summary(model))
Model structure:
SimpleModel(
(fc): Linear(in_features=10, out_features=5, bias=True)
)
Model summary: : Class Name: SimpleModel Total Number of model parameters: 55 Number of trainable parameters: 55 (100.0%) Size: 448 B Type: torch.float32