DeepGraphInfomax
¶
DeepGraphInfomax(
hidden_features_dim: int,
encoder: Module,
summary: Callable[..., array],
corruption: Callable[..., Any],
)
Bases: Module
The Deep Graph Infomax model from the "Deep Graph Infomax" paper, based on a user-defined encoder \(\mathcal{E}\), summary (readout) function \(\mathcal{R}\) and corruption function \(\mathcal{C}\).
The encoder is trained to maximize the mutual information between node embeddings and a summary of the whole graph, by telling apart the embeddings of the graph from those of a corrupted graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_features_dim
|
int
|
The size of the latent node embeddings. |
required |
encoder
|
Module
|
The encoder module \(\mathcal{E}\). |
required |
summary
|
Callable[..., array]
|
The readout function \(\mathcal{R}\), called as
|
required |
corruption
|
Callable[..., Any]
|
The corruption function \(\mathcal{C}\), called with the inputs of the encoder and returning the corrupted inputs (a single array or a tuple, replacing the positional then the keyword arguments in order). |
required |
Example:
import mlx.core as mx
import mlx.nn as nn
from mlx_graphs.nn import GCN, DeepGraphInfomax
def corruption(edge_index, node_features):
perm = mx.random.permutation(node_features.shape[0])
return edge_index, node_features[perm]
model = DeepGraphInfomax(
hidden_features_dim=512,
encoder=GCN(node_features_dim, 512, num_layers=1, act="relu"),
summary=lambda z, *args, **kwargs: mx.sigmoid(z.mean(axis=0)),
corruption=corruption,
)
def loss_fn(model, edge_index, node_features):
pos_z, neg_z, summary = model(edge_index, node_features)
return model.loss(pos_z, neg_z, summary)
References
Methods:
| Name | Description |
|---|---|
__call__ |
Encodes the inputs and their corruption, and summarizes the graph. |
discriminate |
Scores each node embedding against the summary. |
loss |
The mutual information maximization objective (binary cross |
test |
Evaluates the embeddings with a downstream logistic regression: |
Source code in mlx_graphs/nn/models/deep_graph_infomax.py
__call__
¶
Encodes the inputs and their corruption, and summarizes the graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Positional inputs of the encoder (e.g. |
()
|
**kwargs
|
Any
|
Keyword inputs of the encoder |
{}
|
Returns:
| Type | Description |
|---|---|
array
|
A tuple |
...
|
graph, those of the corrupted graph, and the graph summary. |
Source code in mlx_graphs/nn/models/deep_graph_infomax.py
discriminate
¶
discriminate(z: array, summary: array, sigmoid: bool = True) -> array
Scores each node embedding against the summary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
array
|
The node embeddings, shape |
required |
summary
|
array
|
The summary vector, shape |
required |
sigmoid
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
array
|
One probability (or logit) per node. |
Source code in mlx_graphs/nn/models/deep_graph_infomax.py
loss
¶
The mutual information maximization objective (binary cross entropy of the discriminator).
Source code in mlx_graphs/nn/models/deep_graph_infomax.py
test
¶
test(
train_z: array,
train_y: array,
test_z: array,
test_y: array,
max_iter: int = 300,
learning_rate: float = 0.01,
weight_decay: float = 0.0,
) -> float
Evaluates the embeddings with a downstream logistic regression:
trains a multinomial logistic regression on train_z and returns
its accuracy on test_z.
The classifier is trained full-batch with Adam, in MLX.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_z
|
array
|
Embeddings of the training nodes |
required |
train_y
|
array
|
Integer labels of the training nodes |
required |
test_z
|
array
|
Embeddings of the test nodes |
required |
test_y
|
array
|
Integer labels of the test nodes |
required |
max_iter
|
int
|
Number of optimization steps. Default: |
300
|
learning_rate
|
float
|
Learning rate of Adam. Default: |
0.01
|
weight_decay
|
float
|
\(L_2\) penalty on the weights. Default: |
0.0
|
Returns:
| Type | Description |
|---|---|
float
|
The test accuracy. |
Source code in mlx_graphs/nn/models/deep_graph_infomax.py
from mlx_graphs.nn import DeepGraphInfomax · mlx_graphs/nn/models/deep_graph_infomax.py