Skip to main content

Trainable graph adjacency parameterizations with ODE integration and Lightning training helpers.

Project description

GradNet

GradNet provides differentiable parameterizations of graph adjacency matrices with explicit budget and structure constraints. It pairs these parameterizations with ODE solvers and a lightweight PyTorch Lightning training loop so you can prototype network optimization problems quickly.

A random network rewires itself with GradNet to optimize synchronization in the Kuramoto model.

Highlights

  • Learn dense or sparse adjacency updates with norm, sign, and symmetry constraints.
  • Projected parameterizations that stay differentiable and GPU friendly.
  • Torchdiffeq-backed integration utilities for graph-driven dynamical systems.
  • Minimal Lightning trainer that wraps custom loss functions in just a few lines.

Installation

Install the released package from PyPI:

pip install gradnet

To work off the latest sources instead, clone the repository and install in editable mode:

pip install -e .

GradNet targets Python 3.10+ and depends on PyTorch, PyTorch Lightning, torchdiffeq, NumPy, and tqdm (installed automatically by the command above). Install the optional NetworkX helpers with pip install gradnet[networkx] when you need conversions to networkx graphs or plotting utilities that rely on it.

Documentation

Full API documentation, tutorials, and background material live at gradnet.readthedocs.io.

Quickstart

Learn a constrained adjacency

import torch
from gradnet import GradNet

num_nodes = 10
model = GradNet(
    num_nodes=num_nodes,
    budget=1.0,
    undirected=True,
)

adjacency = model()  # full (num_nodes, num_nodes) tensor

Pass a sparse COO mask via the mask argument to switch to the sparse backend and optimize only selected edges.

Integrate a graph-driven ODE

from gradnet import integrate_ode

# simple linear dynamics \dot{x} = Ax

def vector_field(t, x, A):
    return A @ x

x0 = torch.randn(num_nodes)
t_grid = torch.linspace(0.0, 1.0, 51)
sol_t, sol_x = integrate_ode(model, vector_field, x0, t_grid)

Optimize with your own loss

from gradnet import GradNet, fit

# encourage sparse, small-magnitude updates
def loss_fn(g: GradNet):
    delta = g.get_delta_adj()
    return delta.abs().mean()

fit(gradnet=model, loss_fn=loss_fn, num_updates=200, learning_rate=1e-2)

The trainer handles optimizer setup, logging, and checkpointing while you focus on defining the objective.

Modules at a glance

  • gradnet.GradNet: wraps dense and sparse parameterizations, supports directed/undirected networks, masking, custom edge-building costs etc.
  • gradnet.integrate_ode: torchdiffeq-powered solver with adjoint and event support for adjacency-dependent dynamics.
  • gradnet.fit: PyTorch Lightning loop that optimizes a GradNet using user-supplied loss functions.
  • gradnet.utils: various helpers functions.

License

GradNet is released under the BSD 3-Clause License. See LICENSE for details.

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

gradnet-0.1.8.tar.gz (25.0 kB view details)

Uploaded Source

Built Distribution

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

gradnet-0.1.8-py3-none-any.whl (27.3 kB view details)

Uploaded Python 3

File details

Details for the file gradnet-0.1.8.tar.gz.

File metadata

  • Download URL: gradnet-0.1.8.tar.gz
  • Upload date:
  • Size: 25.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.8

File hashes

Hashes for gradnet-0.1.8.tar.gz
Algorithm Hash digest
SHA256 ac0b24a784cddc386ddfbed6fda1ba2e5fcf7bb5700901aa813c00caaf12e68e
MD5 6c34b07ce4b2e3832f955db603d768e4
BLAKE2b-256 8b1dbe673584c74bcd07e2effed0c3d5fcc2084f776bef9604aadbc77974dc93

See more details on using hashes here.

File details

Details for the file gradnet-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: gradnet-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 27.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.8

File hashes

Hashes for gradnet-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 9e940da28bf0f5eddc81f89b1fa83d938806c3c7c790ed1109dfdb9a4b8c24ff
MD5 fca0c41f633e7d56f3dc080a7f7f7c2a
BLAKE2b-256 f5b44b436d297c41148fb766dc5d3603e327db942d9a55f6774691f60ea934f4

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