Lightweight gradient visualizer for PyTorch: record gradient norms and plot flows by layer/param.
Project description
gradviz
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
Heatmap of gradient norms by layer
🔧 Features
- Record gradient L2 norms during training
- Works with any PyTorch model
- Save to CSV for later analysis
- Visualize:
- Line plots (by="layer" or by="param")
- 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
- GradViz(model, config=None) → main class
- .attach() → hook gradients
- .detach() → remove hooks
- .step() → call after optimizer.step()
- .set_epoch(epoch) → optionally record epoch index
- .save(path) → save collected gradients to CSV
- .plot(by="layer"|"param", topk=20) → line plots
- .heatmap(by="layer"|"param", at_step=...) → heatmap
📂 Examples
See examples/demo_mnist.py for a full MNIST training demo.
Project details
Release history Release notifications | RSS feed
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
caa0a9489b1d352b831ba7c8a2ce5b234f52a621f8637ed2eb50b8c674b3603f
|
|
| MD5 |
d5632e590d9c845134e4e0b14c15a0b9
|
|
| BLAKE2b-256 |
a4fdafa26194d56eb7af76954982f8776dac93a45361934a3b4f2a91d51c8e25
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd1478a15ee61467e37682fadbb4d11e5eef24de2842265953c9ec1d8bec74b1
|
|
| MD5 |
49d63bb23faa6218886a146ef965711b
|
|
| BLAKE2b-256 |
96cf605b1445867e5b85d75463512084773d08073ff13aaf7e3010a503a3dbb6
|