Skip to main content

Visualize and debug PyTorch model tensors

Project description

🧠 TensorViz

Visualize, debug, and inspect your PyTorch models like never before.

TensorViz is an AI-powered Python package that automatically logs and visualizes the internals of PyTorch models during forward/backward passes — including input/output shapes, data types, gradients, and more. It generates an interactive HTML report with grouped collapsible layers, graph SVGs, and structured inspection for every model layer.


🚀 Features

  • ✅ Automatic logging of layer name, input/output shapes, data type
  • ✅ Gradient stats (mean ± std) if .backward() is called
  • ✅ Grouped layer visualization using HTML collapsible <details> blocks
  • ✅ Inline graphviz model diagram rendering
  • ✅ Lightweight and dependency-minimal
  • ✅ CLI and Python API
  • ✅ Works with any torch.nn.Module including nn.Sequential, Transformer, etc.

📦 Installation

pip install tensorviz

You also need Graphviz installed for SVG model diagrams:

# Linux/macOS
sudo apt install graphviz        # or: brew install graphviz

# Python binding (optional for CLI)
pip install graphviz

✨ Quick Start

Python API

import torch
import torchvision.models as models
from tensorviz import visualize_model

model = models.resnet18()
dummy_input = torch.randn(1, 3, 224, 224)

visualize_model(model, dummy_input, save_dir="tensorviz_report")

CLI

tensorviz report model.pt --input-shape 1 3 224 224

This will create a folder tensorviz_report/ containing:

  • logs.json — layer logs
  • graph.svg — graphviz model diagram
  • report.html — interactive report

🧰 Usage Options

visualize_model(
    model,
    input_tensor=torch.randn(1, 3, 224, 224),
    output_file="tensorviz_report/report.html",
    include_grad=True,         # Capture gradient stats
    include_graph=True,        # Include model diagram SVG
    html_template="template.html.j2"  # Customizable template
)

🔍 Implementation Overview

TensorViz uses:

  • PyTorch forward hooks to log layer info at runtime
  • Optional backward hooks to gather .grad statistics
  • Jinja2 to render HTML reports
  • Graphviz (via torchviz or manual export) for diagrams
  • HTML5 tags like <details> and <summary> for collapsible layer groups

📁 Project Structure

tensorviz/
├── __init__.py
├── hooks.py               # Forward/backward hook logic
├── render.py              # HTML/Jinja2 rendering
├── svg_generator.py       # Graphviz integration
├── cli.py                 # Command line interface
├── templates/
│   └── report.html.j2     # HTML template
├── static/                # CSS, graph.svg (optional)
tests/
README.md
setup.py

📜 License

This project is licensed under the MIT License.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
...

See LICENSE for the full text.

Other Licenses you may optionally use:

  • Apache-2.0
  • BSD-3-Clause
  • GPL-3.0 (if strict copyleft is needed)

👥 Contributing

Pull requests are welcome! To contribute:

  1. Fork the repository
  2. Create a new branch
  3. Make changes with clear commits
  4. Ensure all tests pass (pytest)
  5. Submit a PR describing your enhancement or fix

🧪 Testing

pytest tests/

You can also inspect outputs manually by running:

python examples/run_resnet.py

🙋 FAQ

Q: Does it work with custom layers?
Yes! As long as your model inherits from torch.nn.Module, it will be logged.

Q: Can I use it with transformers?
Yes. TensorViz has been tested with torchvision, transformers, and custom model code.


✨ Acknowledgments


🔗 Related Projects


Made with ❤️ by Divyansh Saraswat

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tensorviz-0.1.5.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tensorviz-0.1.5-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file tensorviz-0.1.5.tar.gz.

File metadata

  • Download URL: tensorviz-0.1.5.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.0

File hashes

Hashes for tensorviz-0.1.5.tar.gz
Algorithm Hash digest
SHA256 da2d3d5ffb6ad02da49f448741e7f07d5d417a790e658c1c8492df6554609f28
MD5 739afbc85e3f37d06080486f6d997c0e
BLAKE2b-256 4d99f47622323cd287d0351970b59538cd7f6f33d2cde949bee8b6471365e616

See more details on using hashes here.

File details

Details for the file tensorviz-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: tensorviz-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.0

File hashes

Hashes for tensorviz-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 678b6e80b3413bff14c50335f8e272224af9042ca852cff9a0eb24e0fdcc42f1
MD5 3de33c410212e94e7f02a9d27471101e
BLAKE2b-256 661f9d25921c1427f07f3e2ff53061e91479769a21a457459d897a072a6d3a78

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page