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.7.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.7-py3-none-any.whl (27.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gradnet-0.1.7.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.7.tar.gz
Algorithm Hash digest
SHA256 ff2290041e0bee8d80f986844b590db877170d9552fc42a1a18000ebf2272b25
MD5 a968f7b73f688957bfdc518107c13edb
BLAKE2b-256 7fe0ec7f28b337bbd3eac58a3ede160780782cd168daa3ce934b2460026677a9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gradnet-0.1.7-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.7-py3-none-any.whl
Algorithm Hash digest
SHA256 4fea529a29bed51beab4c0555417f5595a7332f5a77a3627a2310590aeed3a78
MD5 205f760725efca7ee1df7cf621c0153a
BLAKE2b-256 6facfa570dce44bc6391773b5238da64ad2661f3c6fd24d5bfacd3acb3ecd113

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