Skip to main content

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

  • SGD / Adam / RMSProp built-in trajectory recorders
  • Jupyter widget support
  • Multi-trajectory comparison

License

MIT — built with ❤️ by Aman

Metadata

Release files for gradfield 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gradfield 0.5.0
File Size Uploaded Provenance Actions
gradfield-0.5.0.tar.gz 12.9 kB Without provenance Details for gradfield-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for gradfield 0.5.0
File Interpreter ABI Platform Actions
gradfield-0.5.0-py3-none-any.whl Python 3 none any Details for gradfield-0.5.0-py3-none-any.whl

Total release size: 26.4 kB

Release files / gradfield-0.5.0.tar.gz

Download URL gradfield-0.5.0.tar.gz
Size 12.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7e1ca42f7859f8477018e47dff18b7b45174b94835f97df44cc9617847689c0e
BLAKE2b-256 checksum
How to use checksums
f15569c34251d66568d821aafe7f62e869872a7b011af874b3918b452423dd07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / gradfield-0.5.0-py3-none-any.whl

Download URL gradfield-0.5.0-py3-none-any.whl
Size 13.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d9135ba517b06693ff1dd48212278486195b2da0445a81361b0d27030e715e93
BLAKE2b-256 checksum
How to use checksums
e6dd0b88c1362ad220567467a52ef203c7fa2fd5eb46d8ad91b22455d4551fb9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Security for gradfield 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page