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.1.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.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gradviz-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 caa0a9489b1d352b831ba7c8a2ce5b234f52a621f8637ed2eb50b8c674b3603f
MD5 d5632e590d9c845134e4e0b14c15a0b9
BLAKE2b-256 a4fdafa26194d56eb7af76954982f8776dac93a45361934a3b4f2a91d51c8e25

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gradviz-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dd1478a15ee61467e37682fadbb4d11e5eef24de2842265953c9ec1d8bec74b1
MD5 49d63bb23faa6218886a146ef965711b
BLAKE2b-256 96cf605b1445867e5b85d75463512084773d08073ff13aaf7e3010a503a3dbb6

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