GradNet (class)
The core differentiable adjacency parameterization exposed at the top level of
gradnet.
- class gradnet.GradNet(num_nodes, budget, mask=None, adj0=None, delta_sign='nonnegative', final_sign='free', directed=False, rand_init_weights=True, strict_budget=True, cost_matrix=None, cost_aggr_norm=1, *, device=None, dtype=None)[source]
Bases:
ModuleUser-facing GradNet: learn a constrained
deltaover a base adjacency.This thin wrapper owns the mask, cost matrix, and base adjacency
adj0, and delegates the trainable parameters to either a dense or sparse parameterization depending on mask layout.Construct a GradNet instance.
- Parameters:
num_nodes (int) – Number of nodes (matrix dimension).
budget (float | None) – Target cost-weighted p-norm of the perturbation. If
None, no budget normalization is enforced.mask (torch.Tensor | None, optional) – Active-entry mask. Dense masks result in a dense parameterization; sparse COO masks use the sparse backend. If
None, defaults to all-ones off-diagonal.adj0 (torch.Tensor | None, optional) – Base adjacency. If
None, uses a zero matrix matching the selected backend layout.delta_sign (str, optional) – Sign constraint for
delta. One of{"free", "nonnegative", "nonpositive"}.final_sign (str, optional) – Sign constraint applied to the returned adjacency. One of
{"free", "nonnegative", "nonpositive"}.directed (bool, optional) – If
False, symmetrizedeltaand expect a symmetric cost matrix.rand_init_weights (bool | float, optional) – Initialization mix coefficient
a. Cast to float and clamped to[0,1].a = 1.0orTrueyields fully randomU(0,1);a = 0.0orFalseyields uniform ones. Intermediate values yield interpolation.strict_budget (bool, optional) – If
True, always scale up/down to the exact budget. IfFalse, scale down only.cost_matrix (torch.Tensor | None, optional) – Per-entry costs for normalization; defaults to ones. In sparse backend mode, omitted costs remain implicit (unit costs) and no dense default matrix is materialized.
cost_aggr_norm (int, optional) – Aggregation norm
pfor the cost-weighted p-norm.device (torch.device | str | None, optional) – Target device for buffers/parameters. If
None, inferred from input tensors or defaults to CPU.dtype (torch.dtype | str | None, optional) – Target dtype for buffers/parameters. If
None, inferred from input tensors or from PyTorch defaults.
- extra_repr()[source]
Return the extra representation of the module.
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
- classmethod from_config(config)[source]
Rebuild a
GradNetfromexport_config()output.
- set_initial_state(delta_adj_raw_0)[source]
Forward to the parameterization’s
set_initial_stateand renormalize.
- should_renorm_after_step()[source]
Return whether post-update parameter renormalization is advised.
This is
Trueonly when the model enforces a budget and strict budget scaling is enabled.
- get_delta_adj(noise_amplitude=0.0)[source]
Return the normalized perturbation matrix
deltafrom the backend.- Parameters:
noise_amplitude (float, optional) – Standardized magnitude for the stochastic perturbation applied to the raw parameters before constraints. Defaults to 0 (deterministic).
- forward(noise_amplitude=0.0)[source]
Return the full adjacency
A = adj0 + delta.Handles dense/sparse combinations between
adj0anddeltaand returns either a dense or a sparse tensor accordingly. Whennoise_amplitude > 0the same stochastic perturbation asget_delta_adj()is injected before constraints.- Parameters:
noise_amplitude (float, optional) – Magnitude of the Gaussian noise applied to
delta_adj_rawprior to constraint handling.
Note
Additional convenience methods (for example to or eval) are inherited
directly from torch.nn.Module. Refer to the PyTorch module documentation for their full API.