{"id":10305,"library":"torchsummary","title":"Torchsummary","description":"`torchsummary` provides a Keras-like `model.summary()` functionality for PyTorch models, displaying layer names, output shapes, parameter counts, and trainable parameters. It helps in quickly understanding the architecture and memory footprint of a neural network. The current version is 1.5.1, with releases occurring as needed for bug fixes and minor enhancements rather than a strict schedule.","status":"active","version":"1.5.1","language":"en","source_language":"en","source_url":"https://github.com/sksq96/torchsummary","tags":["pytorch","model-summary","deep-learning","nn","architecture-analysis"],"install":[{"cmd":"pip install torchsummary","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Used for progress bars internally, listed as a direct dependency.","package":"tqdm","optional":false},{"reason":"Peer dependency, required for model definition and operations but not listed in install_requires as users typically install it separately.","package":"torch","optional":false}],"imports":[{"note":"The 'summary' function is directly exposed at the top-level package.","wrong":"from torchsummary.torchsummary import summary","symbol":"summary","correct":"from torchsummary import summary"}],"quickstart":{"code":"import torch\nimport torch.nn as nn\nfrom torchsummary import summary\n\n# Define a simple model\nclass SimpleCNN(nn.Module):\n    def __init__(self):\n        super(SimpleCNN, self).__init__()\n        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)\n        self.relu1 = nn.ReLU()\n        self.pool1 = nn.MaxPool2d(kernel_size=2)\n        self.flatten = nn.Flatten()\n        # Calculate input size for linear layer: (28 - 5 + 1) / 2 = 12\n        # So, 10 channels * 12 * 12 pixels = 1440\n        self.fc1 = nn.Linear(10 * 12 * 12, 50)\n        self.relu2 = nn.ReLU()\n        self.fc2 = nn.Linear(50, 10)\n\n    def forward(self, x):\n        x = self.pool1(self.relu1(self.conv1(x)))\n        x = self.flatten(x)\n        x = self.relu2(self.fc1(x))\n        x = self.fc2(x)\n        return x\n\nmodel = SimpleCNN()\n\n# Determine device\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel.to(device)\n\n# Print summary for an input image of 1 channel, 28x28 pixels\n# input_size should be (channels, height, width) *without* the batch dimension\nprint(summary(model, input_size=(1, 28, 28), device=str(device)))\n","lang":"python","description":"This example defines a simple Convolutional Neural Network and uses `torchsummary.summary` to print its architecture, output shapes, and parameter counts. Note the importance of providing a correct `input_size` tuple (excluding batch dimension) and ensuring the model is on the specified device."},"warnings":[{"fix":"Carefully determine the expected input shape for your model's first layer, excluding the batch dimension, and pass it as a tuple to `input_size`.","message":"The `input_size` parameter must be a tuple representing the shape of a *single input sample* (excluding the batch dimension). For example, a batch of 64 images of size (3, 224, 224) would require `input_size=(3, 224, 224)`. Mismatches are a common cause of `RuntimeError` or incorrect summaries.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For models with highly dynamic control flow, consider using alternative tools like `torchinfo` (which uses a different tracing mechanism) or manually inspecting layer outputs.","message":"`torchsummary` internally uses `torch.jit.trace` to analyze the model. Models with dynamic control flow (e.g., if-statements or loops whose behavior depends on input data values) may not be correctly summarized or might raise `RuntimeError` during tracing.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Convert your `torch.device` object to a string before passing it: `device=str(your_torch_device_object)`.","message":"The `device` parameter in `summary()` expects a string (`\"cpu\"` or `\"cuda\"`) not a `torch.device` object directly.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Adjust `input_size` to correctly reflect the `(channels, height, width)` of your input data. For example, if the model expects 3 channels, `input_size=(3, H, W)`.","cause":"The `input_size` passed to `summary()` does not match the number of input channels expected by the model's first layer.","error":"RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[1, 1, 224, 224] to have 3 channels, but got 1 channels instead"},{"fix":"Replace `input_data` or `input_shape` with `input_size`.","cause":"This error typically occurs when attempting to use Keras-like argument names or non-existent arguments with `torchsummary.summary()`. The correct argument for specifying the input tensor shape (without the batch dimension) is `input_size`.","error":"TypeError: summary() got an unexpected keyword argument 'input_data'"},{"fix":"Convert your `torch.device` object to a string before passing it: `device=str(your_torch_device_object)`.","cause":"The `device` argument in `summary()` expects a string representation of the device (e.g., \"cpu\", \"cuda\"), not a `torch.device` object.","error":"AttributeError: 'torch.device' object has no attribute 'lower' or similar device-related error when passing device to summary()"}]}