Skip to main content

rainbow-tensor

Colourful SVG visuals for tensor shapes, indexing, and operations, built for Jupyter notebooks and teaching.

PyPI version Python versions License Documentation

rt.shape(np.arange(8).reshape(2, 2, 2))

Shape (2, 2, 2)

Why rainbow-tensor

Tensor shapes are hard to hold in your head, and an index like (0, slice(None), 1) gives no hint of what it selects until you run it. A printed array is just a wall of numbers. rainbow-tensor draws the tensor as nested coloured frames and highlights exactly which elements an operation touches, so the structure and the result are clear at a glance.

Every axis keeps one colour through every view, so you can follow an axis as it moves, folds, or stretches. That makes it a fast way to learn how reshapes and reductions work, to teach shape transformations, and to debug a confusing indexing or broadcasting bug. The core imports no deep learning framework, so it stays light and works with plain NumPy or any array that exposes a shape.

Install

pip install rainbow-tensor

The distribution name is rainbow-tensor and the import name is rainbow_tensor.

Quick start

Run inside a Jupyter notebook or an IPython shell so the SVG is displayed. The convention is to import the package as rt.

import numpy as np
import rainbow_tensor as rt

x = np.arange(8).reshape(2, 2, 2)

rt.shape(x)                          # draw the structure
rt.index(x, (0, slice(None), 1))     # highlight what an index selects

Index (0, :, 1)

Each call returns a small result object. Its svg attribute holds the SVG string, its text attribute holds the explanation printed under the figure, and save writes the SVG to a file.

What it can show

Shape changing, combining, and broadcasting views draw the source and the result side by side, so the mapping between them is easy to follow.

  • Shapes and indexing with shape and index, covering integers, slices, ellipsis, new axes, boolean masks, and fancy integer arrays
  • Reshaping and moving axes with reshape, transpose, swapaxes, moveaxis, squeeze, and expand_dims
  • Reductions and math with sum, mean, matmul, and einsum
  • Combining with concatenate, stack, broadcast, repeat, and take
  • Output explanations with focus= on sums, means, matmul, and einsum
  • Memory layout with memory, including byte strides and data ownership
rt.sum(np.arange(12).reshape(3, 4), 0)

Sum over axis 0

rt.einsum("ij,jk->ik", np.arange(6).reshape(2, 3), np.arange(12).reshape(3, 4))

Einsum ij and jk to ik

Follow one output

Choose an output coordinate to highlight its contributing source cells and read the corresponding formula.

a = np.arange(6).reshape(2, 3)
b = np.arange(12).reshape(3, 4)

visual = rt.matmul(a, b, focus=(1, 2))
visual                             # highlights 3 * 2 + 4 * 6 + 5 * 10 = 80
visual.trace.terms                 # ordered, inspectable source references
rt.mean(a, axis=1, focus=(-1,))     # follow the last row
rt.memory(a.T)                     # explain strides and storage ownership

Start with the learning guide or run 07_explaining_outputs.ipynb.

Compare an index with its result

x = np.array([10, 20, 30])
rt.index(x, ([2, 0, 2],), show_result=True)  # source -> [30, 10, 30]

The comparison keeps source colours and highlights the result. Repeated picks stay repeated, and reverse slices retain their order. See the indexing guide for output-to-source coordinate lookup.

For optional notebook controls, install rainbow-tensor[interactive] and use rt.explore(rt.matmul, a, b, focus=(1, 2)). The interactive guide covers coordinate updates and export.

Keep previews small

Basic slice selections store ranges instead of expanding every selected coordinate. Reductions compute only the output groups a renderer requests. Math views also accept max_terms, defaulting to 10,000 terms per output cell. When a calculation exceeds that limit, the output shows ? with an explanation instead of performing a partial calculation. Use max_terms=None to opt into full evaluation.

Numerical previews use Python scalar arithmetic. Accumulation dtype, rounding, and overflow can differ from a framework's native kernels. Each math view explains that model and identifies any generated placeholder operands.

Themes

Pass theme="dark" to any call, or set a module default that every later call follows. A theme is a plain object you can tweak with variant, and a global axis ramp can be set once with set_default_axis_colors.

rt.shape(x, theme="dark")
rt.set_default_theme("dark")

Documentation

The full guide and API reference live at rainbow-tensor.zhixiangfeng.com.

Runnable notebooks for every feature group live in examples, and more sample images live in examples/images.

Development

pip install -e ".[dev]"
pytest
ruff check .
python -m build

License

MIT

Metadata

Release files for rainbow-tensor 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rainbow-tensor 1.1.0
File Size Uploaded
rainbow_tensor-1.1.0.tar.gz 94.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rainbow-tensor 1.1.0
File Interpreter ABI Platform
rainbow_tensor-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 166.1 kB

Release files / rainbow_tensor-1.1.0.tar.gz

Download URL rainbow_tensor-1.1.0.tar.gz
Size 94.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f0fb7fc76a2ba9e7eda8c13fc4a8d28fd81436435098d2c5fe37fb577776be65
BLAKE2b-256 checksum
How to use checksums
8f3351455c3f225f582225a3af444e82b6d36f2aea835e77be18774ca43cef93
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 Sep 21, 2026.

Transparency log

Release files / rainbow_tensor-1.1.0-py3-none-any.whl

Download URL rainbow_tensor-1.1.0-py3-none-any.whl
Size 71.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e2489930b9f310a200de95c796dc100cfa0864d6ba3662844631f82261c1b5d8
BLAKE2b-256 checksum
How to use checksums
816951d1c9b729c092917f519a196fb2f89327de91f3a09805c57ce3b0b3f09f
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 Sep 21, 2026.

Transparency log

Release history Release notifications | RSS feed

1.8.0

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.1

2 release files

This release

1.1.0 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.15.0

2 release files

0.14.0

2 release files

0.13.0

2 release files

0.12.0

2 release files

0.11.0

2 release files

0.10.8

2 release files

0.10.7

2 release files

0.10.6

2 release files

0.10.5

2 release files

0.10.4

2 release files

0.10.3

2 release files

0.10.2

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.2.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page