sheaf_mpnn#
- class BaseSheafConv[source]
Bases:
MessagePassingShared base for all NSD and NSP sheaf convolution layers.
Factors out the parameterization and utilities that are identical across every variant in both model families:
W1/W2– bilateral stalk transforms (left d*d, right f*f).sigma– activation function (Tanh).reset_parameters()– Xavier init for W1, W2, and any map_generator._apply_stalk_transform(x)– computesW1 @ x @ W2._apply_norm(...)– abstract; each concrete subclass delegates to the appropriateapply_*_normfunction fromsheaf_mpnn.utils.
- Concrete subclasses must implement:
get_map_products(x_feat, edge_index) -> (self_map, cross_map)_apply_norm(self_map, cross_map, edge_index, num_nodes)forward(x_feat, x_stalk, edge_index) -> updated stalkmessage(...)
- Parameters:
- map_generator: Module
- weight_generator: Linear
- __init__(stalk_dim, in_channels, hidden_dim, context_dim=None, add_self_loops=False, add_lp=False, add_hp=False, sparse_learner=False)[source]
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- sigma: Module
- reset_parameters()[source]
Resets all learnable parameters of the module.
- message(z_dst, z_src, self_map, cross_map)[source]
Builds per-edge sheaf Laplacian messages.
- Parameters:
z_dst – Destination-node transformed stalks [E, d, f].
z_src – Source-node transformed stalks [E, d, f].
self_map – Normalized \(F_\mathrm{dst}^\top F_\mathrm{dst}\) per edge [E, d, d].
cross_map – Normalized \(F_\mathrm{dst}^\top F_\mathrm{src}\) per edge [E, d, d].
- Returns:
Per-edge messages [E, d, f].
- Return type:
- class DiagonalNSDConv[source]
Bases:
BaseNSDConvDiagonal NSD convolution layer.
- Parameters:
- __init__(stalk_dim, in_channels, hidden_dim, alpha=1.0, context_dim=None, add_self_loops=False, add_eps=True, dropout=0.0, add_lp=False, add_hp=False, sparse_learner=False)[source]
Initializes the shared NSD convolution parameters.
- Parameters:
stalk_dim (
int) – Number of LEARNED stalk dimensions (referenced). Withadd_lp/add_hpthe full stalk handled by the layer isfinal_d = stalk_dim + add_lp + add_hpand node states have shape[final_d, in_channels].in_channels (
int) – Feature dimension inside each stalk channel (f).hidden_dim (
int) – Unused by the reference-style linear map generator; kept for API compatibility.alpha (
float, default:1.0) – Initial residual diffusion step size.context_dim (
int|None, default:None) – Width of each node context vectorx_feat.add_self_loops (
bool, default:False) – Whether to add self-loops for degree normalization. Defaults toFalse; the normalization is already (deg+1)-augmented, so self-loops would augment twice.add_eps (
bool, default:True) – IfTrue, adds a learnable per-stalk rescalingepsin R^d (Remark 4, Appendix A.1). The skip termx_stalkis replaced by(1 + eps) * x_stalk, where eps is broadcast across the feature channel dimension. Initialized to zero so default behaviour is the standard NSD update.dropout (
float, default:0.0) – Dropout probability applied to the diffusion input (the skip path stays un-dropped), matching the reference per-layer dropout before the left/right weights.add_lp (
bool, default:False) – Appends one fixed stalk channel whose Laplacian block is the signless graph Laplacian D+A (sharpening), as in the reference--add_lp.add_hp (
bool, default:False) – Appends one fixed stalk channel whose Laplacian block is the standard graph Laplacian D-A (smoothing), as in the reference--add_hp.sparse_learner (
bool, default:False) – Uses the reference LocalConcatSheafLearnerVariant input reduction: the concatenated pair is viewed as[final_d, 2f]and summed over the stalk axis, shrinking the learner input from2*final_d*fto2f.
- get_map_products(x_feat, edge_index)[source]
Compute diagonal map products w_dst**2 and w_dst*w_src for all edges.
- Parameters:
x_feat – Node context features [N, context_dim].
edge_index – Graph connectivity [2, E].
- Returns:
(self_map, cross_map)both [E, d] – element-wise products.- Return type:
- message(z_dst, z_src, self_map, cross_map)[source]
Builds diagonal Laplacian messages.
- Parameters:
- Returns:
Per-edge Laplacian messages [E, d, f].
- Return type:
- class GeneralNSDConv[source]
Bases:
BaseNSDConvGeneralized NSD convolution layer.
- Parameters:
stalk_dim (
int)in_channels (
int)hidden_dim (
int)alpha (
float, default:1.0)add_self_loops (
bool, default:False)use_attention (
bool, default:False)add_eps (
bool, default:True)dropout (
float, default:0.0)add_lp (
bool, default:False)add_hp (
bool, default:False)sparse_learner (
bool, default:False)
- __init__(stalk_dim, in_channels, hidden_dim, alpha=1.0, context_dim=None, add_self_loops=False, use_attention=False, add_eps=True, dropout=0.0, add_lp=False, add_hp=False, sparse_learner=False)[source]
Initializes the shared NSD convolution parameters.
- Parameters:
stalk_dim (
int) – Number of LEARNED stalk dimensions (referenced). Withadd_lp/add_hpthe full stalk handled by the layer isfinal_d = stalk_dim + add_lp + add_hpand node states have shape[final_d, in_channels].in_channels (
int) – Feature dimension inside each stalk channel (f).hidden_dim (
int) – Unused by the reference-style linear map generator; kept for API compatibility.alpha (
float, default:1.0) – Initial residual diffusion step size.context_dim (
int|None, default:None) – Width of each node context vectorx_feat.add_self_loops (
bool, default:False) – Whether to add self-loops for degree normalization. Defaults toFalse; the normalization is already (deg+1)-augmented, so self-loops would augment twice.add_eps (
bool, default:True) – IfTrue, adds a learnable per-stalk rescalingepsin R^d (Remark 4, Appendix A.1). The skip termx_stalkis replaced by(1 + eps) * x_stalk, where eps is broadcast across the feature channel dimension. Initialized to zero so default behaviour is the standard NSD update.dropout (
float, default:0.0) – Dropout probability applied to the diffusion input (the skip path stays un-dropped), matching the reference per-layer dropout before the left/right weights.add_lp (
bool, default:False) – Appends one fixed stalk channel whose Laplacian block is the signless graph Laplacian D+A (sharpening), as in the reference--add_lp.add_hp (
bool, default:False) – Appends one fixed stalk channel whose Laplacian block is the standard graph Laplacian D-A (smoothing), as in the reference--add_hp.sparse_learner (
bool, default:False) – Uses the reference LocalConcatSheafLearnerVariant input reduction: the concatenated pair is viewed as[final_d, 2f]and summed over the stalk axis, shrinking the learner input from2*final_d*fto2f.use_attention (
bool, default:False)
- class LowRankNSDConv[source]
Bases:
BaseNSDConvLow-rank NSD convolution layer.
Parameterizes each restriction map as F = A @ B^T where A, B in R^{dxr}, bounding the effective rank of each map to at most
rank. This gives cheaper parameterization thanGeneralNSDConv(2*d*r vs d*d params per map) while keeping the full d-dimensional stalk.- The Laplacian products reduce to:
- restriction_maps_dst^T restriction_maps_dst
= right_dst (left_dst^T left_dst) right_dst^T [d, d]
- restriction_maps_dst^T restriction_maps_src
= right_dst (left_dst^T left_src) right_src^T [d, d]
- Parameters:
- __init__(stalk_dim, in_channels, hidden_dim, alpha=1.0, context_dim=None, add_self_loops=False, rank=1, add_eps=True, dropout=0.0, add_lp=False, add_hp=False, sparse_learner=False)[source]
Initializes a low-rank NSD convolution layer.
- Parameters:
stalk_dim (
int) – Stalk dimension.in_channels (
int) – Feature dimension inside each stalk channel.hidden_dim (
int) – Hidden width of the restriction-map generator MLP.alpha (
float, default:1.0) – Initial learnable diffusion step size. Defaults to 1.0.context_dim (
int|None, default:None) – Width ofx_feat. Defaults tod * in_channels.add_self_loops (
bool, default:False) – Whether to add self-loops for degree normalization. Defaults toFalse; the normalization is already (deg+1)-augmented, so self-loops would augment twice.rank (
int, default:1) – Rank of each restriction map F = A @ B^T. Must be positive.add_eps (
bool, default:True) – Learnable per-stalk skip rescaling (Remark 4). Defaults to True.dropout (
float, default:0.0) – Dropout probability on the diffusion input. Defaults to 0.0.add_hp (
bool, default:False) – Appends one fixed stalk channel diffusing with the standard Laplacian D-A (reference--add_hp).add_lp (
bool, default:False) – Appends one fixed stalk channel diffusing with the signless Laplacian D+A (reference--add_lp).sparse_learner (
bool, default:False) – Stalk-summed map-generator input (reference LocalConcatSheafLearnerVariant). Defaults to False.
- get_map_products(x_feat, edge_index)[source]
Compute low-rank map products F_dst^T F_dst and F_dst^T F_src for all edges.
Each map F = left @ right^T so F^T F = right (left^T left) right^T.
- Parameters:
x_feat – Node context features [N, context_dim].
edge_index – Graph connectivity [2, E].
- Returns:
(self_map, cross_map)both [E, d, d].- Return type:
- class OrthogonalNSDConv[source]
Bases:
BaseNSDConvOrthogonal NSD convolution layer.
- Parameters:
stalk_dim (
int)in_channels (
int)hidden_dim (
int)alpha (
float, default:1.0)add_self_loops (
bool, default:False)clamp_val (
float, default:10.0)use_attention (
bool, default:False)orth_strategy (
Literal['cayley','fasth','fasthpp','householder'], default:"cayley")add_eps (
bool, default:True)dropout (
float, default:0.0)add_lp (
bool, default:False)add_hp (
bool, default:False)sparse_learner (
bool, default:False)use_edge_weights (
bool, default:False)
- __init__(stalk_dim, in_channels, hidden_dim, alpha=1.0, context_dim=None, add_self_loops=False, clamp_val=10.0, use_attention=False, orth_strategy='cayley', add_eps=True, dropout=0.0, add_lp=False, add_hp=False, sparse_learner=False, use_edge_weights=False)[source]
Initializes an orthogonal NSD convolution layer.
The map generator outputs parameters for either: (1) entries of a skew-symmetric matrix (cayley), (2) Householder vectors (fasth), or (3) attention-based mappings. All parameterisations produce orthogonal
d x drestriction maps.- Parameters:
stalk_dim (
int) – Stalk dimension and orthogonal restriction-map matrix size.in_channels (
int) – Feature dimension inside each stalk channel.hidden_dim (
int) – Hidden width of the restriction-map generator MLP.alpha (
float, default:1.0) – Initial learnable diffusion step size. Defaults to 1.0.context_dim (
int|None, default:None) – Width ofx_feat. Defaults tod * in_channelswhen omitted.add_self_loops (
bool, default:False) – IfTrue, self-loops augment the degree used for normalization. Defaults toFalse.clamp_val (
float, default:10.0) – Maximum absolute value for clamping Cayley-transform parameters. Defaults to 10.0.use_attention (
bool, default:False) – IfTrue, uses the attention-based Cayley initialization from main. Defaults toFalse.orth_strategy (
Literal['cayley','fasth','fasthpp','householder'], default:"cayley") – “cayley” or “fasth”. Defaults to “cayley”.add_eps (
bool, default:True) – IfTrue, adds a learnable per-stalk rescalingeps in R^dto the skip term (Remark 4). Defaults toTrue.dropout (
float, default:0.0) – Dropout probability on the diffusion input. Defaults to 0.0.use_edge_weights (
bool, default:False) – Learns a symmetric per-edge scalar (reference EdgeWeightLearner) that rescales the orthogonal transport maps and replaces the combinatorial degree with the weighted one. Defaults toFalse.add_hp (
bool, default:False) – Appends one fixed stalk channel diffusing with the standard Laplacian D-A (reference--add_hp).add_lp (
bool, default:False) – Appends one fixed stalk channel diffusing with the signless Laplacian D+A (reference--add_lp).sparse_learner (
bool, default:False) – Stalk-summed map-generator input (reference LocalConcatSheafLearnerVariant). Defaults to False.
- get_map_products(x_feat, edge_index)[source]
Compute orthogonal cross-map W_dst^T W_src and identity self-map for all edges.
- Parameters:
x_feat – Node context features [N, context_dim].
edge_index – Graph connectivity [2, E].
- Returns:
(identity, cross_map)whereidentityis [E, d, d] andcross_mapis [E, d, d].self_mapis the identity because W^T W = I.- Return type:
- message(z_dst, z_src, self_map, cross_map)[source]
Builds orthogonal Laplacian messages.
- Parameters:
- Returns:
Per-edge Laplacian messages [E, d, f].
- Return type:
- class NSDModel[source]
Bases:
ModuleEnd-to-end Neural Sheaf Diffusion (NSD) model.
The wrapper lifts raw node features into stalk features, applies a stack of NSD convolution layers, and decodes the flattened stalk representation back to the requested output dimension.
- Parameters:
in_channels (
int)out_channels (
int)stalk_dim (
int, default:4)hidden_dim (
int, default:16)num_layers (
int, default:2)variant (
NSDVariant, default:NSDVariant.GENERAL)alpha (
float, default:1.0)add_self_loops (
bool, default:False)orth_strategy (
Literal['cayley','fasth','fasthpp','householder'], default:"cayley")rank (
int, default:1)add_eps (
bool, default:True)input_dropout (
float, default:0.0)dropout (
float, default:0.0)normalize_output (
bool, default:False)jknet (
bool, default:False)add_lp (
bool, default:False)add_hp (
bool, default:False)sparse_learner (
bool, default:False)second_linear (
bool, default:False)use_edge_weights (
bool, default:False)learn_alpha (
bool, default:True)
- __init__(in_channels, out_channels, stalk_dim=4, hidden_dim=16, num_layers=2, variant=NSDVariant.GENERAL, alpha=1.0, add_self_loops=False, orth_strategy='cayley', rank=1, add_eps=True, input_dropout=0.0, dropout=0.0, normalize_output=False, jknet=False, add_lp=False, add_hp=False, sparse_learner=False, second_linear=False, use_edge_weights=False, learn_alpha=True)[source]
Initializes an NSD model for node-level prediction.
- Parameters:
in_channels (
int) – Number of raw input features per node.out_channels (
int) – Number of output channels per node (e.g. num classes).stalk_dim (
int, default:4) – Stalk dimension. Each node is represented internally as a matrix with shape[stalk_dim, hidden_dim].hidden_dim (
int, default:16) – Feature dimension inside each stalk channel. The encoded node state has sized * hidden_dim.num_layers (
int, default:2) – Number of NSD convolution layers. Must be positive.variant (
NSDVariant, default:NSDVariant.GENERAL) – Restriction-map family.DIAGONALis cheapest,GENERALis most expressive,ORTHOGONALuses orthogonal maps (via Cayley or Householder parameterisation).GENERAL_ATTENTIONandORTHOGONAL_ATTENTIONuse an attention-based map initialisation.alpha (
float, default:1.0) – Initial diffusion step size per layer. Defaults to 1.0, matching the reference update at init.learn_alpha (
bool, default:True) – IfFalse, alpha stays fixed at its initial value (Bodnar-exact update: no step-size parameter); ifTrueit is learned. Defaults toTrue.add_self_loops (
bool, default:False) – IfTrue, self-loops are added to the graph before computing degree normalization in each layer. Defaults toFalse: the +1/+I degree augmentation now lives inside the normalization itself, so adding self-loops would augment twice.orth_strategy (
Literal['cayley','fasth','fasthpp','householder'], default:"cayley") – Orthogonality strategy for theORTHOGONALvariant: “cayley”, “fasth”, “fasthpp”, or “householder” (reference orgqr; fasthpp computes the same map natively). Defaults to “cayley”.rank (
int, default:1) – Rank of each restriction map for theLOW_RANKvariant. Must be positive. Ignored for other variants. Defaults to 1.add_eps (
bool, default:True) – IfTrue, each layer applies a learnable per-stalk rescalingeps in R^dto the skip term (Remark 4). Defaults toTrue(reference parity: the original NSD always gates the skip with1 + tanh(eps), eps initialized at zero).input_dropout (
float, default:0.0) – Dropout probability applied to raw input features before encoding. Defaults to 0.0.dropout (
float, default:0.0) – Dropout probability applied to stalk features between layers. Defaults to 0.0.normalize_output (
bool, default:False) – IfTrue, L2-normalise the representation before the decoder (Lv et al., 2021). IfjknetisTrue, each layer’s output is also normalised before concatenation. Defaults toFalse(reference parity: the original NSD decodes the raw representation).jknet (
bool, default:False) – IfTrue, collect hidden states from every layer and concatenate them before the decoder (Xu et al., 2018). Normalization is controlled bynormalize_output. Intended for link prediction. Defaults toFalse.add_lp (
bool, default:False) – Appends one fixed stalk channel diffusing with the signless Laplacian D+A (reference--add_lp).add_hp (
bool, default:False) – Appends one fixed stalk channel diffusing with the standard Laplacian D-A (reference--add_hp).sparse_learner (
bool, default:False) – Stalk-summed map-generator input (reference LocalConcatSheafLearnerVariant), shrinking the learner from 2*final_d*hidden to 2*hidden inputs.second_linear (
bool, default:False) – Adds a second encoder linear after the ELU+dropout stage (reference--second_linear).use_edge_weights (
bool, default:False) – Learned symmetric per-edge scalars rescaling orthogonal maps (reference edge_weights). Defaults to False.
- reset_parameters()[source]
- forward(x, edge_index)[source]
Runs the NSD encoder, diffusion layers, and decoder.
- class NSDVariant[source]
Bases:
Enum- DIAGONAL = 1
- GENERAL = 2
- ORTHOGONAL = 3
- GENERAL_ATTENTION = 4
- ORTHOGONAL_ATTENTION = 5
- LOW_RANK = 6
- property layer_class
NSD layers#
Diagonal NSD convolution layer. |
|
Generalized NSD convolution layer. |
|
Orthogonal NSD convolution layer. |
|
Low-rank NSD convolution layer. |
NSD model#
End-to-end Neural Sheaf Diffusion (NSD) model. |
|