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 Alt text

Heatmap of gradient norms by layer Alt text

🔧 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.

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.0.tar.gz (6.8 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.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gradviz-0.1.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for gradviz-0.1.0.tar.gz
Algorithm Hash digest
SHA256 681f9b544d8788d767fa51d72fdffc7e138e8074cc7833d0e01d93db3dd660a6
MD5 9dcc8a10cc8aa7d937315247aee8fd23
BLAKE2b-256 f81438c25b848ddc7bd85099f006035bc19a0c2c27e5cdc9662e720397c8097f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gradviz-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for gradviz-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ff1d177e297a6d3357ce30fe45af3c655ced82eb29cc99054b4446931caa6e91
MD5 4337bc276eb2ee4955270d7fc829d5db
BLAKE2b-256 1e3c455875088dfe90253deefce7a4c051dd0125a9dcbd2e774d788d9df0aa30

See more details on using hashes here.

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