{"id":6268,"library":"torchviz","title":"Torchviz","description":"Torchviz is a small Python package designed to create visualizations of PyTorch execution graphs and traces. It provides a way to visually inspect the computational flow of a neural network model, which is helpful for understanding architecture, debugging, and optimization. The current version is 0.0.3, with releases typically tied to PyTorch ecosystem updates.","status":"active","version":"0.0.3","language":"en","source_language":"en","source_url":"https://github.com/pytorch/pytorchviz","tags":["pytorch","visualization","deep-learning","neural-networks","computational-graph","graphviz"],"install":[{"cmd":"pip install torchviz","lang":"bash","label":"Install Python package"},{"cmd":"pip install graphviz\n# System-level Graphviz is also required (e.g., `brew install graphviz` on macOS, `sudo apt-get install graphviz` on Ubuntu)","lang":"bash","label":"Install Graphviz (Python package and system-level)"}],"dependencies":[{"reason":"Core PyTorch functionality for model definition and execution graphs.","package":"torch","optional":false},{"reason":"Required for rendering the computational graphs; both the Python `graphviz` package and the system-level Graphviz binaries are necessary.","package":"graphviz","optional":false}],"imports":[{"note":"This is the primary function for visualizing computational graphs from a PyTorch Variable.","symbol":"make_dot","correct":"from torchviz import make_dot"},{"note":"While available, `make_dot_from_trace` using `torch.jit.trace` is noted to be less reliable and 'does not always work', especially with newer PyTorch versions. Prefer `make_dot` for general use.","wrong":"from torchviz import make_dot_from_trace","symbol":"make_dot_from_trace","correct":"from torchviz import make_dot_from_trace"}],"quickstart":{"code":"import torch\nimport torch.nn as nn\nfrom torchviz import make_dot\n\n# Define a simple neural network\nclass SimpleNN(nn.Module):\n    def __init__(self):\n        super(SimpleNN, self).__init__()\n        self.fc1 = nn.Linear(10, 5)\n        self.fc2 = nn.Linear(5, 1)\n\n    def forward(self, x):\n        x = torch.relu(self.fc1(x))\n        x = self.fc2(x)\n        return x\n\n# Instantiate the model\nmodel = SimpleNN()\n\n# Generate a random input, ensuring requires_grad=True for graph generation\ninput_data = torch.randn(1, 10, requires_grad=True)\n\n# Perform a forward pass\noutput = model(input_data)\n\n# Visualize the computational graph\ngraph = make_dot(output, params=dict(model.named_parameters()))\n\n# To save the visualization to a file (e.g., PNG)\n# graph.render(\"computational_graph\", format=\"png\", cleanup=True)\n\n# In Jupyter/Colab, the graph can often be displayed directly by just calling it\n# graph","lang":"python","description":"This quickstart defines a simple PyTorch neural network, performs a forward pass with an input tensor that requires gradients, and then uses `make_dot` to generate and optionally save a visualization of the computational graph. Ensure Graphviz is installed on your system for rendering."},"warnings":[{"fix":"Install Graphviz on your system using your operating system's package manager or by downloading from the official Graphviz website (graphviz.org).","message":"Torchviz critically depends on the external Graphviz software package being installed on your operating system, not just the Python `graphviz` package. Without a system-wide Graphviz installation (e.g., `brew install graphviz` on macOS, `sudo apt-get install graphviz` on Ubuntu, or installing from graphviz.org for Windows), `make_dot` will not be able to render images, often failing silently or with obscure errors about missing executables.","severity":"breaking","affected_versions":"All versions"},{"fix":"Ensure that the input tensor used in the forward pass has `requires_grad=True` (e.g., `torch.randn(..., requires_grad=True)`).","message":"For `make_dot` to generate a meaningful computational graph, at least one of the input tensors to the operation being visualized must have `requires_grad=True`. If no tensor requires a gradient, PyTorch's autograd engine (which `torchviz` inspects) won't track operations, resulting in an empty or incomplete graph.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Prefer using `make_dot` with the output of a forward pass and `model.named_parameters()` for reliable graph visualization.","message":"The `make_dot_from_trace` function, intended for use with `torch.jit.trace`, is noted to be less robust and 'does not always work' according to the official documentation, especially with newer PyTorch versions. Past issues have reported it not working with PyTorch 1.0.","severity":"deprecated","affected_versions":"All versions, particularly problematic with PyTorch >= 1.0"},{"fix":"Ensure your PyTorch installation is version 1.9 or higher to fully utilize `show_attrs` and `show_saved`. If using an older version, omit these parameters.","message":"The `show_attrs=True` and `show_saved=True` parameters, which provide additional details about the graph nodes (attributes and saved tensors for the backward pass), are only supported with PyTorch versions 1.9 and later. Using them with older PyTorch versions might not have the intended effect or could lead to errors.","severity":"gotcha","affected_versions":"<1.9"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z","problems":[]}