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.9.tar.gz (25.2 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.9-py3-none-any.whl (27.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gradnet-0.1.9.tar.gz
  • Upload date:
  • Size: 25.2 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.9.tar.gz
Algorithm Hash digest
SHA256 38563d5db40460236bebb0da1efebe550230ff649e53346fc6fa951055882c5e
MD5 b91ead7d1d5e7cf767268b452cdc9a69
BLAKE2b-256 8a82b79347e501251816a1802a9c29d7d838663a4630cb80f7efeaa22e0e0154

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gradnet-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 27.5 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.9-py3-none-any.whl
Algorithm Hash digest
SHA256 4e14ab741fe4bb51d51903c8d74db2ebf05f50f755e5b0568214243bb5a93994
MD5 f3270af649fba4cfb9ec0b783725de2a
BLAKE2b-256 ebcc25b115bd1f3c3eba5ed7cc164697d975497c300410f64128cb648c9950c9

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