Skip to main content

mlx-graphs-explain

Explainability for Graph Neural Networks on Apple Silicon, built on mlx-graphs and MLX.

The API mirrors torch_geometric.explain, so PyG users should feel at home.

Installation

pip install mlx-graphs-explain

From source:

git clone https://github.com/mlx-graphs/mlx-graphs-explain.git && cd mlx-graphs-explain
pip install -e ".[dev]"

Algorithms

Algorithm Module Type Node mask Edge mask Task levels
Saliency algorithm.gradient gradient ✓ ✓ node, graph
IntegratedGradients algorithm.gradient gradient ✓ ✓ node, graph
GNNExplainer algorithm.gnn_explainer mask learning ✓ ✓ node, graph

PyG runs gradient methods through Captum (CaptumExplainer); MLX has no Captum, so Saliency and IntegratedGradients are native here.

Quickstart

import mlx.nn as nn
from mlx_graphs.nn import GCNConv

from mlx_graphs_explain import Explainer
from mlx_graphs_explain.algorithm import GNNExplainer


class GCN(nn.Module):
    def __init__(self, in_dim, hidden, out_dim):
        super().__init__()
        self.conv1 = GCNConv(in_dim, hidden)
        self.conv2 = GCNConv(hidden, out_dim)

    # accept `edge_weights` and pass it to the layers so edges can be explained
    def __call__(self, edge_index, node_features, edge_weights=None):
        h = nn.relu(self.conv1(edge_index, node_features, edge_weights))
        return self.conv2(edge_index, h, edge_weights)


model = GCN(...)  # train it first

explainer = Explainer(
    model=model,
    algorithm=GNNExplainer(epochs=200),
    explanation_type="model",
    node_mask_type="attributes",
    edge_mask_type="object",
    model_config=dict(
        mode="multiclass_classification",
        task_level="node",
        return_type="raw",
    ),
)

explanation = explainer(edge_index, node_features, index=10)
explanation.node_mask  # [num_nodes, num_features]
explanation.edge_mask  # [num_edges]

top = explanation.threshold("topk_hard", 10)
top.get_explanation_subgraph()  # edge_index of the 10 most important edges

Note the mlx-graphs argument order: explainer(edge_index, node_features, ...).

Evaluating explanations

from mlx_graphs_explain.metric import characterization_score, fidelity

pos, neg = fidelity(explainer, explanation.threshold("topk_hard", 10))
characterization_score(pos, neg)

groundtruth_auc scores a mask against a known ground truth, and mlx_graphs_explain.datasets.ba_shapes builds the BA-Shapes benchmark.

Visualizing explanations

pip install "mlx-graphs-explain[viz]"   # matplotlib + networkx
# the explanation subgraph: edge opacity = edge importance,
# explained node in red, nodes shaded by importance ("object" node masks)
explanation.threshold("topk", 12).visualize_graph("explanation.png")

# write each edge's mask value on it
explanation.visualize_graph("explanation.png", edge_labels=True)

# bar plot of feature importance ("attributes" node masks)
explanation.visualize_feature_importance("features.png", top_k=10)

Both methods match PyG's names and arguments. visualize_graph uses graphviz when it is installed (pip install graphviz and brew install graphviz), else networkx; pass backend="networkx" to choose. Pass highlight_index=False or show_node_mask=False to turn off the extras.

Example: BA-Shapes

python examples/ba_shapes_demo.py

Trains a 3-layer GCN on BA-Shapes, then explains 20 nodes inside "house" motifs. Edge AUC measures how well the edge mask finds the motif. It also saves ba_shapes_explanation.png: the edges GNNExplainer considers important (mask > 0.1) for a roof node, labelled with their mask values.

Reproducibility

On the Mac GPU, training may not be bit-for-bit reproducible even with mx.random.seed: message passing sums neighbour messages with parallel scatter-adds, whose order is not guaranteed to be the same between runs. The tiny floating-point differences add up over training, so two runs can give slightly different models and therefore slightly different explanations (e.g. GNNExplainer may weight two equivalent edges differently). For identical results on every run, use the CPU:

import mlx.core as mx

mx.set_default_device(mx.cpu)

Current limitations

  • Edge masks go through edge_weights. The explained model must accept an edge_weights keyword and pass it to its layers. For GCN-style layers the mask also changes the degree normalization, which affects fidelity scores on hard (thresholded) masks. A mask hook inside mlx-graphs' MessagePassing will remove both limitations.
  • Edge-level tasks (link prediction) and heterogeneous graphs are not supported yet.

Roadmap

  • PGExplainer, AttentionExplainer, GraphMaskExplainer
  • SubgraphX, counterfactual explainers
  • More benchmarks (Tree-Cycles, Tree-Grid, BA-2Motifs)

Development

pip install -e ".[dev]"
pytest
ruff check . && ruff format --check .

License

MIT

Metadata

Release files for mlx-graphs-explain 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mlx-graphs-explain 0.0.1
File Size Uploaded Provenance Actions
mlx_graphs_explain-0.0.1.tar.gz 24.3 kB With provenance Details for mlx_graphs_explain-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mlx-graphs-explain 0.0.1
File Interpreter ABI Platform Actions
mlx_graphs_explain-0.0.1-py3-none-any.whl Python 3 none any Details for mlx_graphs_explain-0.0.1-py3-none-any.whl

Total release size: 48.6 kB

Release files / mlx_graphs_explain-0.0.1.tar.gz

Download URL mlx_graphs_explain-0.0.1.tar.gz
Size 24.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c28c3e0c3957582d3980633b7dfc4864bc2e70de7519a3bf8958ba50e5acdf91
BLAKE2b-256 checksum
How to use checksums
2f54c87b4fb141f033d212f38c631c574147472bec1fbb7b1547379a0aae4c41
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Release files / mlx_graphs_explain-0.0.1-py3-none-any.whl

Download URL mlx_graphs_explain-0.0.1-py3-none-any.whl
Size 24.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67e3b345f0415ca414d77e5843af58aeaf037964d4a8ef4ed5c1137fce45da56
BLAKE2b-256 checksum
How to use checksums
d234fb080014fe5dc57b6c661862e393420fe284c5c196f6c08acd17e9b7b237
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Security for mlx-graphs-explain 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mlx_graphs_explain-0.0.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page