Render diagrams for torch, jax, tensorflow, etc. tensors for understanding and debugging.
Project description
tensordiagram
A python library for visualizing tensors from torch, jax, tensorflow, numpy, etc. Helps with learning and debugging in notebokos and other contexts. It's built on top of the graphics backend, chalk.
install
pip install tensordiagram
Separately, you'll need to install cairo for png images:
# might need to install the base library first on debian
sudo apt-get install libcairo2-dev
# cairo python bindings
pip install ".[cairo]"
Or, for svg images:
pip install ".[svg]"
intro
For an in-depth guide, refer to this notebook.
import numpy as np
import tensordiagram as td
tensor = np.arange(12).reshape(3, 4)
diagram = td.to_diagram(tensor)
The diagram can be saved using render_png or render_svg:
diagram.render_png("output.png", height=300)
Style and annotate diagrams:
diagram \
.fill_values() \
.fill_color("lightblue") \
.annotate_dim_size(dim="row", color="lightgreen")
3d tensor:
papaya = "#ff9700"
tensor = np.arange(24).reshape((2, 3, 4))
diagram = td.to_diagram(tensor).fill_color(papaya)
Combine tensor and chalk diagrams for intricate outputs:
import random
import torch
color_names = [ "red", "blue", "green", "purple", "orange", "pink", "cyan"]
def random_colors_tensor(shape):
colors_array = np.empty(shape, dtype=object)
for index, _ in np.ndenumerate(colors_array):
colors_array[index] = random.choice(color_names)
return colors_array
color_tensor = random_colors_tensor(shape=(2, 3, 4))
# tensors
t = torch.arange(24).reshape((2, 3, 4))
slice_1 = t[0, :, :].unsqueeze(0)
slice_2 = t[:, 1, :].unsqueeze(1)
# tensor diagrams
t_d = td.to_diagram(t).fill_color(lambda idx, v: color_tensor[idx])
slice_1_d = td.to_diagram(slice_1).fill_color(lambda idx, v: color_tensor[idx])
slice_2_d = td.to_diagram(slice_2).fill_color(lambda idx, v: color_tensor[idx[0], 1, idx[2]])
# chalk diagrams
diagrams = []
for d in [t_d, slice_1_d, slice_2_d]:
diagrams.append(d.to_chalk_diagram().center_xy())
# composite diagram
composite = chalk.hcat(diagrams, 1.0)
# add background + display
composite = composite.pad(1.5).center_xy()
env = composite.get_envelope()
chalk.set_svg_height(300)
chalk.rectangle(env.width * 0.8, env.height).fill_color(Color("white")) + composite.translate(dx=env.width * 0.1, dy=0)
For more examples and documentation, refer to this guide.
dev
pip install -e ".[dev]"
test
setup
Visual regression tests compare rendered output against reference images stored in fixtures/.
To generate or update reference images:
# all reference images
python tests/generate_references.py --all
# list reference images
python tests/generate_references.py --list
# select reference images
python tests/generate_references.py 3d_tensor styled_gradient
run
All tests:
pytest
license
MIT
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tensordiagram-0.1.0.tar.gz.
File metadata
- Download URL: tensordiagram-0.1.0.tar.gz
- Upload date:
- Size: 22.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d2e9e93ce43a437e5dc777ce10e8a0518066fa9b4ccffc21d41f4e9412421a6a
|
|
| MD5 |
ffa9001366845e97b23b7b5f97856487
|
|
| BLAKE2b-256 |
fe2b394bfb0f8b7722db6e882c28d2aa469e9cc9ad6b1c9975df2780c850e73c
|
Provenance
The following attestation bundles were made for tensordiagram-0.1.0.tar.gz:
Publisher:
publish.yml on hardik-vala/tensordiagram
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tensordiagram-0.1.0.tar.gz -
Subject digest:
d2e9e93ce43a437e5dc777ce10e8a0518066fa9b4ccffc21d41f4e9412421a6a - Sigstore transparency entry: 642082514
- Sigstore integration time:
-
Permalink:
hardik-vala/tensordiagram@2f90a823872c45ea839fbfdf6379acf3acf2cb49 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/hardik-vala
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2f90a823872c45ea839fbfdf6379acf3acf2cb49 -
Trigger Event:
push
-
Statement type:
File details
Details for the file tensordiagram-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tensordiagram-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa8999d7613509feaed977fa21d30962539b3d8a84f068fb99893f139c558b16
|
|
| MD5 |
de30b480c833e3b140be4f090caca8cc
|
|
| BLAKE2b-256 |
7f1aef0b60cab2bc108c1526d23391c2576b24a458a3556e295a9f3a327da743
|
Provenance
The following attestation bundles were made for tensordiagram-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on hardik-vala/tensordiagram
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tensordiagram-0.1.0-py3-none-any.whl -
Subject digest:
fa8999d7613509feaed977fa21d30962539b3d8a84f068fb99893f139c558b16 - Sigstore transparency entry: 642082515
- Sigstore integration time:
-
Permalink:
hardik-vala/tensordiagram@2f90a823872c45ea839fbfdf6379acf3acf2cb49 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/hardik-vala
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2f90a823872c45ea839fbfdf6379acf3acf2cb49 -
Trigger Event:
push
-
Statement type: