Skip to content

LINKX

LINKX(
    num_nodes: int,
    node_features_dim: int,
    hidden_features_dim: int,
    out_features_dim: int,
    num_layers: int,
    num_edge_layers: int = 1,
    num_node_layers: int = 1,
    dropout: float = 0.0,
)

Bases: Module

The LINKX model from the "Large Scale Learning on Non-Homophilous Graphs: New Benchmarks and Strong Simple Methods" paper.

\[ \mathbf{H}_{\mathbf{A}} &= \textrm{MLP}_{\mathbf{A}}(\mathbf{A}) \mathbf{H}_{\mathbf{X}} &= \textrm{MLP}_{\mathbf{X}}(\mathbf{X}) \mathbf{Y} &= \textrm{MLP}_{f} \left( \sigma \left( \mathbf{W} [\mathbf{H}_{\mathbf{A}}, \mathbf{H}_{\mathbf{X}}] + \mathbf{H}_{\mathbf{A}} + \mathbf{H}_{\mathbf{X}} \right) \right) \]

The first layer of \(\textrm{MLP}_{\mathbf{A}}\) multiplies the (sparse) adjacency matrix with a [num_nodes, hidden_features_dim] weight, i.e. node i gets the sum of the weights of the nodes with an edge towards it.

Parameters:

Name Type Description Default
num_nodes int

The number of nodes in the graph.

required
node_features_dim int

Size of the input node features.

required
hidden_features_dim int

Size of the hidden embeddings.

required
out_features_dim int

Size of the output.

required
num_layers int

Number of layers of \(\textrm{MLP}_{f}\).

required
num_edge_layers int

Number of layers of \(\textrm{MLP}_{\mathbf{A}}\). Default: 1

1
num_node_layers int

Number of layers of \(\textrm{MLP}_{\mathbf{X}}\). Default: 1

1
dropout float

Dropout probability of the hidden embeddings of \(\textrm{MLP}_{f}\). Default: 0.0

0.0

Example:

from mlx_graphs.nn import LINKX

model = LINKX(num_nodes, node_features_dim, 64, num_classes, num_layers=2)
logits = model(edge_index, node_features)

Methods:

Name Description
__call__

Computes the node predictions.

Source code in mlx_graphs/nn/models/linkx.py
def __init__(
    self,
    num_nodes: int,
    node_features_dim: int,
    hidden_features_dim: int,
    out_features_dim: int,
    num_layers: int,
    num_edge_layers: int = 1,
    num_node_layers: int = 1,
    dropout: float = 0.0,
):
    super().__init__()
    self.num_nodes = num_nodes
    self.node_features_dim = node_features_dim
    self.out_features_dim = out_features_dim
    self.num_edge_layers = num_edge_layers

    # same initialization as PyG's SparseLinear (kaiming uniform)
    bound = 1.0 / math.sqrt(num_nodes)
    self.edge_weight = mx.random.uniform(
        -bound, bound, (num_nodes, hidden_features_dim)
    )
    self.edge_bias = mx.random.uniform(-bound, bound, (hidden_features_dim,))
    self._propagate = SimpleConv(aggr="add")
    if num_edge_layers > 1:
        self.edge_norm = nn.BatchNorm(hidden_features_dim)
        self.edge_mlp = _ActFirstMLP([hidden_features_dim] * num_edge_layers)

    self.node_mlp = _ActFirstMLP(
        [node_features_dim] + [hidden_features_dim] * num_node_layers
    )
    self.cat_lin1 = nn.Linear(hidden_features_dim, hidden_features_dim)
    self.cat_lin2 = nn.Linear(hidden_features_dim, hidden_features_dim)
    self.final_mlp = _ActFirstMLP(
        [hidden_features_dim] * num_layers + [out_features_dim], dropout=dropout
    )

__call__

__call__(
    edge_index: array,
    node_features: Optional[array] = None,
    edge_weights: Optional[array] = None,
) -> array

Computes the node predictions.

Parameters:

Name Type Description Default
edge_index array

Input edge index of shape [2, num_edges]

required
node_features Optional[array]

Optional input node features. Without them, only the graph structure is used. Default: None

None
edge_weights Optional[array]

Optional weight of each edge. Default: None

None

Returns:

Type Description
array

An array of shape [num_nodes, out_features_dim].

Source code in mlx_graphs/nn/models/linkx.py
def __call__(
    self,
    edge_index: mx.array,
    node_features: Optional[mx.array] = None,
    edge_weights: Optional[mx.array] = None,
) -> mx.array:
    """Computes the node predictions.

    Args:
        edge_index: Input edge index of shape `[2, num_edges]`
        node_features: Optional input node features. Without them, only
            the graph structure is used. Default: ``None``
        edge_weights: Optional weight of each edge. Default: ``None``

    Returns:
        An array of shape ``[num_nodes, out_features_dim]``.
    """
    out = self._propagate(edge_index, self.edge_weight, edge_weights)
    out = out + self.edge_bias
    if "edge_mlp" in self:
        out = self.edge_mlp(self.edge_norm(nn.relu(out)))
    out = out + self.cat_lin1(out)

    if node_features is not None:
        x = self.node_mlp(node_features)
        out = out + x
        out = out + self.cat_lin2(x)

    return self.final_mlp(nn.relu(out))

from mlx_graphs.nn import LINKX · mlx_graphs/nn/models/linkx.py