Skip to main content

Gradient field & loss landscape visualizer for PyTorch and NumPy

Project description

gradfield 🔥

Gradient field & loss landscape visualizer for PyTorch and NumPy

Visualize any function's loss surface, gradient arrows, and optimization trajectories — in an interactive 3D HTML chart. No server, no config, one function call.

Python PyPI License


Install

pip install gradfield

With PyTorch support:

pip install gradfield torch

Quick Start

Any Python function

from gradfield import sample_grid, render

field = sample_grid(
    lambda x, y: (1 - x)**2 + 100 * (y - x**2)**2,  # Rosenbrock
    x_range=(-2, 2),
    y_range=(-1, 3),
    resolution=80,
)

render(field, title="Rosenbrock Banana")
# Opens an interactive HTML in your browser ✓

PyTorch model loss landscape

import torch.nn as nn
from gradfield import model_loss_fn, sample_grid, render

landscape = model_loss_fn(
    model=my_model,
    loss_fn=nn.CrossEntropyLoss(),
    inputs=X_batch,
    targets=y_batch,
    scale=0.5,
)

field = sample_grid(landscape, resolution=50)
render(field, title="My Model Loss Landscape")

With optimization trajectories

import numpy as np

traj = np.array([[2.0, 2.0], [1.5, 1.5], [0.5, 0.5], [0.0, 0.0]])

render(field, trajectories=[traj], title="Gradient Descent Path")

API Reference

sample_grid(fn, x_range, y_range, resolution)

Sample a function over a 2D grid using NumPy central differences for gradients.

sample_grid_torch(fn, x_range, y_range, resolution)

Same as above but uses torch.autograd for exact gradients.

model_loss_fn(model, loss_fn, inputs, targets, scale)

Wraps a PyTorch model into a 2D landscape function using random filter-normalized directions (Li et al. 2018).

render(field, trajectories, title, output, colorscale, show_gradients)

Render the sampled field as an interactive Plotly HTML with:

  • 3D surface plot
  • 2D contour map
  • Gradient vector field overlay
  • Optimization trajectory paths

Examples

cd examples
python example_numpy.py       # Rosenbrock + gradient descent
python example_torch_model.py # MLP on XOR loss landscape

Roadmap

  • C++ accelerated grid sampler via pybind11
  • 1D loss curve along a direction
  • SGD / Adam / RMSProp built-in trajectory recorders
  • Jupyter widget support
  • Multi-trajectory comparison

License

MIT — built with ❤️ by Aman

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

gradfield-0.2.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

gradfield-0.2.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file gradfield-0.2.0.tar.gz.

File metadata

  • Download URL: gradfield-0.2.0.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for gradfield-0.2.0.tar.gz
Algorithm Hash digest
SHA256 176e562f2be92f2905542d7c46cadd882063891392b3264d13e4a8f51e0186a5
MD5 6d41ac97150174b1968c286f96ba7648
BLAKE2b-256 d22a3a7059f7b59bff0d37fdd0dea0654c6e7b36cc5dfb032354e1f50c7a4088

See more details on using hashes here.

File details

Details for the file gradfield-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: gradfield-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for gradfield-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6744b822d09594658a2ea4f23ca2452cc9ffcb510581d82dbb34e22dd48f6d3e
MD5 aacd21cb471bcf62a7e4c62856ec9365
BLAKE2b-256 884ded3f7d26fddd2aa86266ea59a1f53a5baf6967b5404cc6812db54dfc5fde

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