neuralogic.nn.module.gnn package
Submodules
neuralogic.nn.module.gnn.appnp module
- class APPNPConv(output_name: str, feature_name: str, edge_name: str, k: int, alpha: float, activation: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>, aggregation: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>)[source]
Bases:
ModuleApproximate Personalized Propagation of Neural Predictions layer from “Predict then Propagate: Graph Neural Networks meet Personalized PageRank”. Which can be expressed as:
\[\mathbf{x}^{0}_i = \mathbf{x}_i\]\[\mathbf{x}^{k}_i = \alpha \cdot \mathbf{x}^0_i + (1 - \alpha) \cdot {agg}_{j \in \mathcal{N}(i)}(\mathbf{x}^{k - 1}_j)\]\[\mathbf{x}^{\prime}_i = act(\mathbf{x}^{K}_i)\]Where act is an activation function and agg aggregation function.
The first part of the second equation that is “\(\alpha \cdot \mathbf{x}^0_i\)” is expressed in the logic form as:
R.<output_name>__<k>(V.I) <= R.<feature_name>(V.I)[<alpha>].fixed()
The second part of the second equation that is “\((1 - \alpha) \cdot {agg}_{j \in \mathcal{N}(i)}(\mathbf{x}^{k - 1}_j)\)” is expressed as:
R.<output_name>__<k>(V.I) <= (R.<output_name>__<k-1>(V.J)[1 - <alpha>].fixed(), R.<edge_name>(V.J, V.I))
Examples
The whole computation of this module (parametrized as
APPNPConv("h1", "h0", "_edge", 3, 0.1, Transformation.SIGMOID)) is as follows:metadata = Metadata(transformation=Transformation.IDENTITY, aggregation=Aggregation.SUM) (R.h1__1(V.I) <= R.h0(V.I)[0.1].fixed()) | metadata (R.h1__1(V.I) <= (R.h0(V.J)[0.9].fixed(), R._edge(V.J, V.I))) | metadata R.h1__1/1 [Transformation.IDENTITY] (R.h1__2(V.I) <= <0.1> R.h0(V.I)) | metadata (R.h1__2(V.I) <= (<0.9> R.h1__1(V.J), R._edge(V.J, V.I))) | metadata R.h1__2/1 [Transformation.IDENTITY] (R.h1(V.I) <= <0.1> R.h0(V.I)) | metadata (R.h1(V.I) <= (<0.9> R.h1__2(V.J), R._edge(V.J, V.I))) | metadata R.h1 / 1 [Transformation.SIGMOID]
- Parameters:
output_name (str) – Output (head) predicate name of the module.
feature_name (str) – Feature predicate name to get features from.
edge_name (str) – Edge predicate name to use for neighborhood relations.
k (int) – Number of iterations
alpha (float) – Teleport probability
activation (TransformationFunction) – Activation function of the output. Default:
Transformation.IDENTITYaggregation (AggregationFunction) – Aggregation function of nodes’ neighbors. Default:
Aggregation.SUM
neuralogic.nn.module.gnn.gatv2 module
- class GATv2Conv(in_channels: int, out_channels: int, output_name: str, feature_name: str, edge_name: str, share_weights: bool = False, activation: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>)[source]
Bases:
ModuleGATv2 layer from “How Attentive are Graph Attention Networks?”.
- Parameters:
in_channels (int) – Input feature size.
out_channels (int) – Output feature size.
output_name (str) – Output (head) predicate name of the module.
feature_name (str) – Feature predicate name to get features from.
edge_name (str) – Edge predicate name to use for neighborhood relations.
share_weights (bool) – Share weights in attention. Default:
Falseactivation (TransformationFunction) – Activation function of the output. Default:
Transformation.IDENTITY
neuralogic.nn.module.gnn.gcn module
- class GCNConv(in_channels: int, out_channels: int, output_name: str, feature_name: str, edge_name: str, activation: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>, aggregation: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>, add_self_loops: bool | None = None, normalize: bool = True)[source]
Bases:
ModuleGraph Convolutional layer from “Semi-supervised Classification with Graph Convolutional Networks”.
- Parameters:
in_channels (int) – Input feature size.
out_channels (int) – Output feature size.
output_name (str) – Output (head) predicate name of the module.
feature_name (str) – Feature predicate name to get features from.
edge_name (str) – Edge predicate name to use for neighborhood relations.
activation (TransformationFunction) – Activation function of the output. Default:
Transformation.IDENTITYaggregation (AggregationFunction) – Aggregation function of nodes’ neighbors. Default:
Aggregation.SUMadd_self_loops (bool | None) – Add self loops if either set to True or None (if normalize is True). Default:
Nonenormalize (bool) – Add normalization. Default :
True
neuralogic.nn.module.gnn.gen module
- class GENConv(in_channels: int, out_channels: int, output_name: str, feature_name: str, edge_name: str, aggregation: AggregationFunction = <neuralogic.core.constructs.function.softmax.SoftmaxAggregation object>, num_layers: int = 2, expansion: int = 2, eps: float = 1e-07, train_eps: bool = False, edge_dim: int | None = None)[source]
Bases:
ModuleGENConv layer from “DeeperGCN: All You Need to Train Deeper GCNs”.
- Parameters:
in_channels (int) – Input feature size.
out_channels (int) – Output feature size.
output_name (str) – Output (head) predicate name of the module.
feature_name (str) – Feature predicate name to get features from.
edge_name (str) – Edge predicate name to use for neighborhood relations.
aggregation (AggregationFunction) – The aggregation function. Default:
Aggregation.SOFTMAXnum_layers (int) – The number of MLP layers. Default:
2expansion (int) – The expansion factor of hidden channels in MLP. Default:
2eps (float) – \(\epsilon\)-value. Default:
0.0train_eps (bool) – Is
epstrainable parameter. Default:falseedge_dim (int | None) – Dimension of edge features (
Noneis projection toin_channelsis not needed). Default:None
neuralogic.nn.module.gnn.gin module
- class GINConv(in_channels: int, out_channels: int, output_name: str, feature_name: str, edge_name: str, activation: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>, aggregation: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>)[source]
Bases:
ModuleImplements the Graph Isomorphism Network (GIN) convolution layer. GIN is a powerful GNN layer that can distinguish between different graph structures.
neuralogic.nn.module.gnn.gine module
- class GINEConv(in_channels: int, feature_name: str, edge_name: str, nn_name: str, eps: float = 0.0, train_eps: bool = False, edge_dim: int | None = None)[source]
Bases:
ModuleGINEConv layer from “Strategies for Pre-training Graph Neural Networks”.
- Parameters:
in_channels (int) – Input feature size.
out_channels (int) – Output feature size.
feature_name (str) – Feature predicate name to get features from.
edge_name (str) – Edge predicate name to use for neighborhood relations.
nn_name (str) – Neural network predicate name.
eps (float) – \(\epsilon\)-value. Default:
0.0train_eps (bool) – Is
epstrainable parameter. Default:falseedge_dim (int | None) – Dimension of edge features (
Noneis projection toin_channelsis not needed). Default:None
neuralogic.nn.module.gnn.gsage module
- class SAGEConv(in_channels: int, out_channels: int, output_name: str, feature_name: str, edge_name: str, activation: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>, aggregation: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>)[source]
Bases:
ModuleGraphSAGE layer from “Inductive Representation Learning on Large Graphs”. Which can be expressed as:
\[\mathbf{x}^{\prime}_i = act(\mathbf{W}_1 \mathbf{x}_i + \mathbf{W}_2 \cdot {agg}_{j \in \mathcal{N}(i)}(\mathbf{x}_j)))\]Where act is an activation function, agg aggregation function and W’s are learnable parameters. This equation is translated into the logic form as:
(R.<output_name>(V.I)[<W1>] <= (R.<feature_name>(V.J), R.<edge_name>(V.J, V.I))) | [<aggregation>, Transformation.IDENTITY] (R.<output_name>(V.I)[<W2>] <= R.<feature_name>(V.I)) | [Transformation.IDENTITY] R.<output_name> / 1 | [<activation>]
- Parameters:
in_channels (int) – Input feature size.
out_channels (int) – Output feature size.
output_name (str) – Output (head) predicate name of the module.
feature_name (str) – Feature predicate name to get features from.
edge_name (str) – Edge predicate name to use for neighborhood relations.
activation (TransformationFunction) – Activation function of the output. Default:
Transformation.IDENTITYaggregation (AggregationFunction) – Aggregation function of nodes’ neighbors. Default:
Aggregation.AVG
neuralogic.nn.module.gnn.res_gated module
- class ResGatedGraphConv(in_channels: int, out_channels: int, output_name: str, feature_name: str, edge_name: str, gating_activation: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>, activation: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>, aggregation: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>)[source]
Bases:
ModuleResidual Gated Graph Convolutional layer from “Residual Gated Graph ConvNets”. Which can be expressed as:
\[\mathbf{x}^{\prime}_i = act(\mathbf{W}_1 \mathbf{x}_i + {agg}_{j \in \mathcal{N}(i)}(\eta_{i,j} \odot \mathbf{W}_2 \mathbf{x}_j))\]\[\mathbf{\eta}_{i,j} = gating\_act(\mathbf{W}_3 \mathbf{x}_i + \mathbf{W}_4 \mathbf{x}_j)\]Where act is an activation function, agg aggregation function, gating_act is a gating activation function and \(W_n\) are learnable parameters. This equation is translated into the logic form as:
(R.<output_name>__gate(V.I, V.J) <= (R.<feature_name>(V.I)[<W>], R.<feature_name>(V.J)[<W>])) | [Transformation.IDENTITY] R.<output_name>__gate / 2 | [<activation>] (R.<output_name>(V.I) <= R.<feature_name>(V.I)[<W>]) | [Transformation.IDENTITY] (R.<output_name>(V.I) <= ( R.<output_name>__gate(V.I, V.J), R.<feature_name>(V.J)[<W>], R.<edge_name>(V.J, V.I)) ) | Metadata(activation="elementproduct-identity", aggregation=<aggregation>) R.<output_name> / 1 | [<activation>]
Examples
The whole computation of this module (parametrized as
ResGatedGraphConv(1, 2, "h1", "h0", "_edge")) is as follows:metadata = Metadata(activation="elementproduct-identity", aggregation=Aggregation.SUM) (R.h1__gate(V.I, V.J) <= (R.h0(V.I)[2, 1], R.h0(V.J)[2, 1])) | [Transformation.IDENTITY] R.h1__gate / 2 | [Transformation.SIGMOID] (R.h1(V.I) <= R.h0(V.I)[2, 1]) | [Transformation.IDENTITY] (R.h1(V.I) <= (R.h1__gate(V.I, V.J), R.h0(V.J)[2, 1], R._edge(V.J, V.I))) | metadata R.h1 / 1 | [Transformation.IDENTITY]
- Parameters:
in_channels (int) – Input feature size.
out_channels (int) – Output feature size.
output_name (str) – Output (head) predicate name of the module.
feature_name (str) – Feature predicate name to get features from.
edge_name (str) – Edge predicate name to use for neighborhood relations.
gating_activation (TransformationFunction) – Gating activation function. Default:
Transformation.SIGMOIDactivation (TransformationFunction) – Activation function of the output. Default:
Transformation.IDENTITYaggregation (AggregationFunction) – Aggregation function of nodes’ neighbors. Default:
Aggregation.SUM
neuralogic.nn.module.gnn.rgcn module
- class RGCNConv(in_channels: int, out_channels: int, output_name: str, feature_name: str, edge_name: str | None, relations: ~typing.List[str], activation: ~neuralogic.core.constructs.function.function.TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>, aggregation: ~neuralogic.core.constructs.function.function.AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>)[source]
Bases:
ModuleRelational Graph Convolutional layer from Modeling Relational Data with Graph Convolutional Networks. Which can be expressed as:
\[\mathbf{x}^{\prime}_i = act(\mathbf{W_0} \cdot \mathbf{x}_i + \sum_{r \in \mathcal{R}} {agg}_{j \in \mathcal{N}_r(i)}(\mathbf{W_r} \cdot \mathbf{x}_j))\]Where act is an activation function, agg aggregation function (by default average), \(W_0\) is a learnable root parameter and \(W_r\) is a learnable parameter for each relation.
The first part of the equation that is “\(\mathbf{W_0} \cdot \mathbf{x}_i\)” can be expressed in the logic form as:
R.<output_name>(V.I) <= R.<feature_name>(V.I)[<W0>]
Another part of the equation that is “\({agg}_{j \in \mathcal{N}_r(i)}(\mathbf{W_r} \cdot \mathbf{x}_j)\)” can be expressed as:
R.<output_name>(V.I) <= (R.<feature_name>(V.J)[<Wr>], R.<edge_name>(V.J, relation, V.I))
where “relation” is a constant name, or as:
R.<output_name>(V.I) <= (R.<feature_name>(V.J)[<Wr>], R.<relation>(V.J, V.I))
The outer summation, together with summing it with the first part, is handled by aggregation of all rules with the same head (and substitution).
Examples
The whole computation of this module (parametrized as
RGCNConv(1, 2, "h1", "h0", "_edge", ["sibling", "parent"])) is as follows:metadata = Metadata(activation=Transformation.IDENTITY, aggregation=Aggregation.AVG) (R.h1(V.I) <= R.h0(V.I)[2, 1]) | metadata (R.h1(V.I) <= (R.h0(V.J)[2, 1], R._edge(V.J, sibling, V.I))) | metadata (R.h1(V.I) <= (R.h0(V.J)[2, 1], R._edge(V.J, parent, V.I))) | metadata R.h1 / 1 [Transformation.IDENTITY]
Module parametrized as
RGCNConv(1, 2, "h1", "h0", None, ["sibling", "parent"])translates into:metadata = Metadata(activation=Transformation.IDENTITY, aggregation=Aggregation.AVG) (R.h1(V.I) <= R.h0(V.I)[2, 1]) | metadata (R.h1(V.I) <= (R.h0(V.J)[2, 1], R.sibling(V.J, V.I))) | metadata (R.h1(V.I) <= (R.h0(V.J)[2, 1], R.parent(V.J, V.I))) | metadata R.h1 / 1 [Transformation.IDENTITY]
- Parameters:
in_channels (int) – Input feature size.
out_channels (int) – Output feature size.
output_name (str) – Output (head) predicate name of the module.
feature_name (str) – Feature predicate name to get features from.
edge_name (str | None) – Edge predicate name to use for neighborhood relations. When
None, elements fromrelationsare used instead.relations (List[str]) – List of relations’ names
activation (TransformationFunction) – Activation function of the output. Default:
Transformation.IDENTITYaggregation (AggregationFunction) – Aggregation function of nodes’ neighbors. Default:
Aggregation.SUM
neuralogic.nn.module.gnn.sg module
- class SGConv(in_channels: int, out_channels: int, output_name: str, feature_name: str, edge_name: str, k: int = 1, activation: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>, aggregation: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>)[source]
Bases:
ModuleSimple Graph Convolutional layer from “Simplifying Graph Convolutional Networks”. Which can be expressed as:
\[\mathbf{x}^{\prime}_i = act(\mathbf{W} \cdot {agg}_{j \in \mathcal{N}^k(i)}(\mathbf{x}_j))\]Where act is an activation function, agg aggregation function, W is a learnable parameter and \(\mathcal{N}^k(i)\) denotes nodes that are k hops away from the node i. This equation is translated into the logic form as:
(R.<output_name>(V.I)[<W>] <= ( R.<feature_name>(V.I<k>), R.<edge_name>(V.I<1>, V.I<0>), R.<edge_name>(V.I<2>, V.I<1>), ..., R.<edge_name>(V.I<k>, V.I<k-1>), )) | [<aggregation>, Transformation.IDENTITY] R.<output_name> / 1 | [<activation>]
Examples
The whole computation of this module (parametrized as
SGConv(2, 3, "h1", "h0", "_edge", 2)) is as follows:(R.h1(V.I0)[3, 2] <= (R.h0(V.I2), R._edge(V.I1, V.I0), R._edge(V.I2, V.I1))) | [Transformation.IDENTITY, Aggregation.SUM] R.h1 / 1 | [Transformation.IDENTITY]
Module parametrized as
SGConv(2, 3, "h1", "h0", "_edge", 1)translates into:(R.h1(V.I0)[3, 2] <= (R.h0(V.I1), R._edge(V.I1, V.I0))) | [Transformation.IDENTITY, Aggregation.SUM] R.h1 / 1 | [Transformation.IDENTITY]
- Parameters:
in_channels (int) – Input feature size.
out_channels (int) – Output feature size.
output_name (str) – Output (head) predicate name of the module.
feature_name (str) – Feature predicate name to get features from.
edge_name (str) – Edge predicate name to use for neighborhood relations.
k (int) – Number of hops. Default:
1activation (TransformationFunction) – Activation function of the output. Default:
Transformation.IDENTITYaggregation (AggregationFunction) – Aggregation function of nodes’ neighbors. Default:
Aggregation.SUM
neuralogic.nn.module.gnn.tag module
- class TAGConv(in_channels: int, out_channels: int, output_name: str, feature_name: str, edge_name: str, k: int = 2, activation: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>, aggregation: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>)[source]
Bases:
ModuleTopology Adaptive Graph Convolutional layer from “Topology Adaptive Graph Convolutional Networks”. Which can be expressed as:
\[\mathbf{x}^{\prime}_i = act(\sum_{k=0}^K \mathbf{W}_k \cdot {agg}_{j \in \mathcal{N}^k(i)}(\mathbf{x}_j))\]Where act is an activation function, agg aggregation function, Wk are learnable parameters and \(\mathcal{N}^k(i)\) denotes nodes that are k hops away from the node i. This equation is translated into the logic form as:
This equation is translated into the logic form as:
(R.<output_name>(V.I0)[<W0>] <= R.<feature_name>(V.I0)) | [<aggregation>, Transformation.IDENTITY] (R.<output_name>(V.I0)[<W1>] <= (R.<feature_name>(V.I1), R.<edge_name>(V.I1, V.I0))) | [<aggregation>, Transformation.IDENTITY] (R.<output_name>(V.I0)[<W2>] <= (R.<feature_name>(V.I2), R.<edge_name>(V.I1, V.I0), R.<edge_name>(V.I2, V.I1)) | [<aggregation>, Transformation.IDENTITY] ... (R.<output_name>(V.I0)[<Wk>] <= (R.<feature_name>(V.I<k>), R.<edge_name>(V.I1, V.I0), ..., R.<edge_name>(V.I<k>, V.I<k-1>)) | [<aggregation>, Transformation.IDENTITY] R.<output_name> / 1 | [<activation>]
Examples
The whole computation of this module (parametrized as
TAGConv(1, 2, "h1", "h0", "_edge")) is as follows:(R.h1(V.I0)[2, 2] <= R.h0(V.I0)) | [Aggregation.SUM, Transformation.IDENTITY] (R.h1(V.I0)[2, 1] <= (R.h0(V.I1), R._edge(V.I1, V.I0)) | [Aggregation.SUM, Transformation.IDENTITY] (R.h1(V.I0)[2, 1] <= (R.h0(V.I2), R._edge(V.I1, V.I0), R._edge(V.I2, V.I1)) | [Aggregation.SUM, Transformation.IDENTITY] R.h1 / 1 | [Transformation.IDENTITY]
Module parametrized as
TAGConv(1, 2, "h1", "h0", "_edge", 1)translates into:(R.h1(V.I0)[2, 1] <= R.h0(V.I0)) | [Aggregation.SUM, Transformation.IDENTITY] (R.h1(V.I0)[2, 1] <= (R.h0(V.I1), R._edge(V.I1, V.I0)) | [Aggregation.SUM, Transformation.IDENTITY] R.h1 / 1 | [Transformation.IDENTITY]
- Parameters:
in_channels (int) – Input feature size.
out_channels (int) – Output feature size.
output_name (str) – Output (head) predicate name of the module.
feature_name (str) – Feature predicate name to get features from.
edge_name (str) – Edge predicate name to use for neighborhood relations.
k (int) – Number of hops. Default:
2activation (TransformationFunction) – Activation function of the output. Default:
Transformation.IDENTITYaggregation (AggregationFunction) – Aggregation function of nodes’ neighbors. Default:
Aggregation.SUM
Module contents
Graph Neural Network modules.
Individual GNN layer implementations are imported at the parent
neuralogic.nn.module level via their respective submodules.