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", "etching", or a TOML
tg.draw(model, x, variant="dark") # for dark slides; "greyscale" for print
tg.draw(model, x, palette="colorblind") # or "tab10", "vivid", "mono"
tg.draw(model, x, fit="slide") # a 16:9 slide: larger text, best layout
tg.draw(model, x, width="3.5in") # a paper's column, at true text size
tg.draw(model, x).animate().save("model.svg") # lights up as the model runs (.gif, .mp4)
tg.draw(model, x, activations=True).animate() # … with what each layer produces, below it
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, direction="TB", wrap_height=900) # top → bottom: columns beyond 900 px
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,etching(black ink, operations told apart by engraved textures: for black-and-white print), andorigamiandorigami-extended(narrow boxes in the style of the survey figures). A style is a TOML file; your own can be passed by path. Any style comes in adarkvariant for slides and agreyscaleone for black-and-white print, and in four palettes:colorblind(safe for colour-vision deficiency),tab10(matplotlib's colours),vivid(for slides and posters) andmono(one hue). - Slides:
fit="slide"draws a figure for a 16:9 slide, with text sized for a projector and the layout chosen to make it as large as possible; saved at the slide's size. - Animations:
fig.animate()lights the figure up as the model runs, layer after layer in the order data reaches them, with the data travelling along the wires between them and round the state loops of recurrent cells. In Jupyter it plays in a player (play/pause, step by step, a time slider, the speed); saved as an animated SVG (sharp at any size), a GIF, an MP4, a web page with the player, or one image per step for slides. Withactivations=Trueit also shows, under the figure, what each layer produces on your input: the most active feature maps, sequences as heatmaps, the output's values (gallery). - Papers:
width="3.5in"(or"8.9cm","252pt") lays a figure out for a column or text width with its text at true size (9 pt), and saves it exactly that wide, so LaTeX includes it without scaling. - 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;classics.ipynb: the ten most-cited deep-learning papers with an architecture figure (ResNet, Transformer, AlexNet, BERT, LSTM, U-Net, Faster R-CNN, ViT, LeNet-5, YOLO), each drawn by Tensorgami next to the paper's own figure.
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.2.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.2.0.tar.gz | 161.2 kB | With provenance | Details for tensorgami-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tensorgami-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for tensorgami-0.2.0-py3-none-any.whl |
Total release size: 355.5 kB
Release files / tensorgami-0.2.0.tar.gz
| Download URL | tensorgami-0.2.0.tar.gz |
|---|---|
| Size | 161.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c40197bb3834ac0f88e4f6708151bc96ca66770f82232b52b2e7a7b2228b487a
|
|
BLAKE2b-256 checksum How to use checksums |
068c6a0c88b2dfb04c1b1404c69aefd40d06baec6a272eec8809dadd40322d26
|
| 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 18, 2026.
Transparency logRelease files / tensorgami-0.2.0-py3-none-any.whl
| Download URL | tensorgami-0.2.0-py3-none-any.whl |
|---|---|
| Size | 194.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0614e70fa6639ef1c30ad1028f70cb682cad1ab83875dad519ea2368f49a99b9
|
|
BLAKE2b-256 checksum How to use checksums |
771e6df39158904e230445d8e9b0f6acbf94661947ca11b3763e41a5e922a0b4
|
| 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 18, 2026.
Transparency logSecurity for tensorgami 0.2.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.2.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 18, 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.