Skip to main content

Lightweight gradient visualizer for PyTorch: record gradient norms and plot flows by layer/param.

Project description

gradviz

PyPI version License: MIT

Lightweight gradient visualizer for PyTorch
Record gradient norms per parameter/layer during training, save them to CSV, and visualize gradient flow with line plots or heatmaps.


🚀 Why gradviz?

Training deep neural networks often runs into vanishing/exploding gradients problems. Debugging these issues usually means writing boilerplate hooks or printing raw tensors.
gradviz makes this simple:

  • Attach once to your model
  • Train as usual
  • Save & visualize gradient norms across layers or parameters

Perfect for students, researchers, and anyone learning how backpropagation behaves.


📦 Installation

pip install gradviz

⚡ Quickstart

from gradviz import GradViz

gv = GradViz(model)
gv.attach()

for epoch in range(epochs):
    gv.set_epoch(epoch)
    for x, y in loader:
        opt.zero_grad()
        loss = model(x).loss(y)
        loss.backward()
        opt.step()
        gv.step()

gv.detach()
gv.save("gradviz.csv")
gv.plot(by="layer", topk=20)
gv.heatmap(by="layer", at_step=1000)

🖼️ Example Plots

Line plot of gradient norms over steps Line plot

Heatmap of gradient norms by layer Heatmap

🔧 Features

  • Record gradient L2 norms during training
  • Works with any PyTorch model
  • Save to CSV for later analysis
  • Visualize:
    1. Line plots (by="layer" or by="param")
    2. Heatmaps at specific steps
  • Lightweight (depends only on torch, numpy, pandas, matplotlib)
  • Command-line interface (CLI) included

CLI Usage

gradviz plot gradviz.csv --by layer --topk 15
gradviz heatmap gradviz.csv --by param --step 500

API Reference

from gradviz import GradViz, GradVizConfig
  1. GradViz(model, config=None) → main class
  2. .attach() → hook gradients
  3. .detach() → remove hooks
  4. .step() → call after optimizer.step()
  5. .set_epoch(epoch) → optionally record epoch index
  6. .save(path) → save collected gradients to CSV
  7. .plot(by="layer"|"param", topk=20) → line plots
  8. .heatmap(by="layer"|"param", at_step=...) → heatmap

📂 Examples

See examples/demo_mnist.py for a full MNIST training demo.

PyPI version License: MIT GitHub Release

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

gradviz-0.1.2.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

gradviz-0.1.2-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file gradviz-0.1.2.tar.gz.

File metadata

  • Download URL: gradviz-0.1.2.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gradviz-0.1.2.tar.gz
Algorithm Hash digest
SHA256 494931cfbd642ee4bda294f124e54fc7ec99c067cad0ba721f8b5a24c361bf1e
MD5 849acff81297b820b798b39ad5942bc9
BLAKE2b-256 638e002b87076f7974327b5b4a77c1eababa8955f0859a1cc89e95f9919603a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for gradviz-0.1.2.tar.gz:

Publisher: publish.yml on namanchhaparia06/gradviz

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gradviz-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: gradviz-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gradviz-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4e9da9f0934bd018256f31a78d728ffc31448c3a4c04941e210e5c18fd86a51d
MD5 5d9ca37c7227b193553a9a295c771c00
BLAKE2b-256 3ba16b4f0cfb8c3b51fc828ac7ebacc9337525ac991bf52e5ec35200316a9f07

See more details on using hashes here.

Provenance

The following attestation bundles were made for gradviz-0.1.2-py3-none-any.whl:

Publisher: publish.yml on namanchhaparia06/gradviz

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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