Tensorgami
Draw the architecture and information flow of PyTorch models, locally, in Python: figures
for papers, reports and slides. Call tg.draw on a loaded model in a script or a notebook,
see the figure inline in Jupyter, and save it as PNG, SVG or PDF.
Install
pip install tensorgami
Tensorgami needs Python ≥ 3.11 and matplotlib. To draw a model it runs the model once with
PyTorch (≥ 2.2), which it does not install itself, so your CPU or CUDA build stays as it is.
If PyTorch isn't installed yet, install it first
(pytorch.org), or let pip pick the default build
with pip install "tensorgami[torch]".
The latest development version: pip install "git+https://github.com/AndreiUtina/TensorGami".
Quick start
import torch
from torch import nn
import tensorgami as tg
model = nn.Sequential(
nn.Conv2d(3, 16, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),
nn.Conv2d(16, 32, 3, padding=1), nn.ReLU(), nn.AdaptiveAvgPool2d(1),
nn.Flatten(), nn.Linear(32, 10),
)
fig = tg.draw(model, torch.randn(1, 3, 32, 32)) # runs the model once, leaves it untouched
fig # shows inline in Jupyter
fig.save("model.png", dpi=300) # or .svg / .pdf; nothing is saved before
The example input is what the model is called with: a tensor, a tuple of positional arguments, or a dict of keyword arguments, including the batch dimension.
What you can draw
tg.draw(model, x, level="stages") # less detail: 0 system … 3 operators
tg.draw(model, x, style="origami") # or "origami-extended", or a path to a TOML
tg.draw(model, x, direction="TB", legend=False) # top → bottom, no legend
tg.draw(model, x, wrap_width=1600) # fold into rows beyond 1600 px (None: never)
tg.draw(model, x, view="unrolled") # recurrence as a time × layer grid
tg.draw(model, x, view="volume") # CNN layers as tensor volumes
tg.draw(model, x, view="cell") # an LSTM / GRU cell as its gates
tg.draw(unet, x, u_shape=False) # keep a U-Net on one line (default: a U)
tg.draw(model, x, overrides={"classifier.6": {"label": "logits"}}) # rename a box
tg.draw(vae, x, annotations=[tg.Loss("KL divergence", "mu", "logvar")]) # a loss term
tg.draw({"generator": G, "discriminator": D}, {"generator": (z, y), "discriminator": (x, y)},
links=[("generator", "discriminator.input 0")], # several models, wired together
annotations=[tg.Loss("adversarial", "discriminator", trains=["generator", "discriminator"])])
- Levels of detail: the whole system, stages, layers or single operators. Repeated blocks fold into one frame marked ×N, activations fold into the layer before them, and shapes appear on the wires where they change.
- Views: block diagrams (
view="block"), recurrent networks unrolled over time ("unrolled"), CNNs as 3-D tensor volumes ("volume") and recurrent cells as their gates ("cell"). Encoder–decoders with nested skips (U-Net, pix2pix) fold into a U. - Styles:
default, andorigamiandorigami-extended(narrow boxes in the style of the survey figures). A style is a TOML file; your own can be passed by path. - Several models in one figure: GANs, VAEs and other training set-ups, with links between the models, extra inputs, loss terms with dashed training arrows, and notes.
Model families covered by the tests: CNN (AlexNet, ResNet), LSTM, ConvLSTM, cGAN, VAE, Transformer and pix2pix. The acceptance gallery shows each of them in every view and style. Every figure there is drawn automatically, and the tests check it.
Examples
Notebooks with every option, already run, are in
examples/:
alexnet.ipynb: levels, views, styles, layout options, saving, panels;resnet.ipynb: ResNet-18/34/50, how to read the figure, wide figures folded into rows;pix2pix.ipynb: a U-Net generator, a PatchGAN discriminator and both together;tour.ipynb: one model per family.
The pipeline's stages can also be used one by one, e.g. to inspect what was captured:
from tensorgami.importers.torch import trace
from tensorgami.transforms import simplify
graph = trace(model, torch.randn(1, 3, 32, 32))
print(graph.summary()) # every operation, its module and shape
print(simplify(graph, level=2).summary()) # layers: activations folded, repeats ×N
Background: the literature survey on how neural-network architectures are drawn, and the architecture of Tensorgami itself.
Development
git clone https://github.com/AndreiUtina/TensorGami && cd TensorGami
uv sync # the environment, with CPU PyTorch
uv run pytest # the tests, including the gallery
uv run ruff check . && uv run ruff format . # lint / format
uv run python docs/gallery/build_gallery.py # accept changed drawings: rebuild the gallery
cd examples && uv run --with nbclient --with nbformat --with ipykernel jupyter execute --inplace *.ipynb
Releases are described in docs/RELEASING.md, changes in the changelog.
License
MIT, see LICENSE. The example and test models adapted from torchvision and pytorch-CycleGAN-and-pix2pix keep their BSD licenses, see THIRD_PARTY_NOTICES.md. To cite Tensorgami, use CITATION.cff (GitHub's "Cite this repository").
Metadata
Release files for tensorgami 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tensorgami-0.1.0.tar.gz | 114.8 kB | With provenance | Details for tensorgami-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tensorgami-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for tensorgami-0.1.0-py3-none-any.whl |
Total release size: 258.4 kB
Release files / tensorgami-0.1.0.tar.gz
| Download URL | tensorgami-0.1.0.tar.gz |
|---|---|
| Size | 114.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
111f4eb7646c8af0746ecc77fc59f33a44cfae232987588784b526a07ef5747f
|
|
BLAKE2b-256 checksum How to use checksums |
d92404d8debdb4390d3af85c99fdb8683da88449b9a5d2ee0a3036bcdbf2c964
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 13, 2026.
Transparency logRelease files / tensorgami-0.1.0-py3-none-any.whl
| Download URL | tensorgami-0.1.0-py3-none-any.whl |
|---|---|
| Size | 143.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c0a740cb2922fd4d8cc82bb305c35d8fb42262ffbce308e224077a530164d869
|
|
BLAKE2b-256 checksum How to use checksums |
04e0d9e76358bed4cc9e44c80f25c713875de2b0d03b7676f07db37943b757f0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 13, 2026.
Transparency logSecurity for tensorgami 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for tensorgami-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 13, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.