Skip to content

Turn any AI agent into an AI Scientist. The #1 Agent Skills library for science, used by 250,000+ scientists worldwide. 177 ready-to-use validated skills plus 100+ scientific databases covering biology, chemistry, medicine, and drug discovery. Compatible with Cursor, Claude Code, Codex, Pi, Antigravity, and the open Agent Skills standard.

Update torch-geometric skill

Timothy Kassisc39f71686de8
diff --git a/scientific-skills/torch-geometric/SKILL.md b/scientific-skills/torch-geometric/SKILL.md
index 513d6549..2f15e823 100644
--- a/scientific-skills/torch-geometric/SKILL.md
+++ b/scientific-skills/torch-geometric/SKILL.md
@@ -1,546 +1,396 @@
---
name: torch-geometric
-description: Graph Neural Networks (PyG). Node/graph classification, link prediction, GCN, GAT, GraphSAGE, heterogeneous graphs, molecular property prediction, for geometric deep learning.
-license: MIT license
-metadata:
- skill-author: K-Dense Inc.
+description: "Guide for building Graph Neural Networks with PyTorch Geometric (PyG). Use this skill whenever the user asks about graph neural networks, GNNs, node classification, link prediction, graph classification, message passing networks, heterogeneous graphs, neighbor sampling, or any task involving torch_geometric / PyG. Also trigger when you see imports from torch_geometric, or the user mentions graph convolutions (GCN, GAT, GraphSAGE, GIN), graph data structures, or working with relational/network data. Even if the user just says 'graph learning' or 'geometric deep learning', use this skill."
---
# PyTorch Geometric (PyG)
-## Overview
+PyG is the standard library for Graph Neural Networks built on PyTorch. It provides data structures for graphs, 60+ GNN layer implementations, scalable mini-batch training, and support for heterogeneous graphs.
-PyTorch Geometric is a library built on PyTorch for developing and training Graph Neural Networks (GNNs). Apply this skill for deep learning on graphs and irregular structures, including mini-batch processing, multi-GPU training, and geometric deep learning applications.
+Install: `uv add torch_geometric` (or `uv pip install torch_geometric`; requires PyTorch). Optional: `pyg-lib`, `torch-scatter`, `torch-sparse`, `torch-cluster` for accelerated ops.
-## When to Use This Skill
-
-This skill should be used when working with:
-- **Graph-based machine learning**: Node classification, graph classification, link prediction
-- **Molecular property prediction**: Drug discovery, chemical property prediction
-- **Social network analysis**: Community detection, influence prediction
-- **Citation networks**: Paper classification, recommendation systems
-- **3D geometric data**: Point clouds, meshes, molecular structures
-- **Heterogeneous graphs**: Multi-type nodes and edges (e.g., knowledge graphs)
-- **Large-scale graph learning**: Neighbor sampling, distributed training
-
-## Quick Start
-
-### Installation
-
-```bash
-uv pip install torch_geometric
-```
+## Core Concepts
-For additional dependencies (sparse operations, clustering):
-```bash
-uv pip install pyg_lib torch_scatter torch_sparse torch_cluster torch_spline_conv -f https://data.pyg.org/whl/torch-${TORCH}+${CUDA}.html
-```
+### Graph Data: `Data` and `HeteroData`
-### Basic Graph Creation
+A graph lives in a `Data` object. The key attributes:
```python
-import torch
from torch_geometric.data import Data
-# Create a simple graph with 3 nodes
-edge_index = torch.tensor([[0, 1, 1, 2], # source nodes
- [1, 0, 2, 1]], dtype=torch.long) # target nodes
-x = torch.tensor([[-1], [0], [1]], dtype=torch.float) # node features
-
-data = Data(x=x, edge_index=edge_index)
-print(f"Nodes: {data.num_nodes}, Edges: {data.num_edges}")
+data = Data(
+ x=node_features, # [num_nodes, num_node_features]
+ edge_index=edge_index, # [2, num_edges] — COO format, dtype=torch.long
+ edge_attr=edge_features, # [num_edges, num_edge_features]
+ y=labels, # node-level [num_nodes, *] or graph-level [1, *]
+ pos=positions, # [num_nodes, num_dimensions] (for point clouds/spatial)
+)
```
-### Loading a Benchmark Dataset
+**`edge_index` format is critical**: it's a `[2, num_edges]` tensor where `edge_index[0]` = source nodes, `edge_index[1]` = target nodes. It is NOT a list of tuples. If you have edge pairs as rows, transpose and call `.contiguous()`:
```python
-from torch_geometric.datasets import Planetoid
-
-# Load Cora citation network
-dataset = Planetoid(root='/tmp/Cora', name='Cora')
-data = dataset[0] # Get the first (and only) graph
-
-print(f"Dataset: {dataset}")
-print(f"Nodes: {data.num_nodes}, Edges: {data.num_edges}")
-print(f"Features: {data.num_node_features}, Classes: {dataset.num_classes}")
+# If edges are [[src1, dst1], [src2, dst2], ...] — transpose first:
+edge_index = edge_pairs.t().contiguous()
```
-## Core Concepts
+For undirected graphs, include both directions: edge (0,1) needs both `[0,1]` and `[1,0]` in edge_index.
-### Data Structure
+For heterogeneous graphs, use `HeteroData` — see the Heterogeneous Graphs section below.
-PyG represents graphs using the `torch_geometric.data.Data` class with these key attributes:
+### Datasets
-- **`data.x`**: Node feature matrix `[num_nodes, num_node_features]`
-- **`data.edge_index`**: Graph connectivity in COO format `[2, num_edges]`
-- **`data.edge_attr`**: Edge feature matrix `[num_edges, num_edge_features]` (optional)
-- **`data.y`**: Target labels for nodes or graphs
-- **`data.pos`**: Node spatial positions `[num_nodes, num_dimensions]` (optional)
-- **Custom attributes**: Can add any attribute (e.g., `data.train_mask`, `data.batch`)
+PyG bundles many standard datasets that auto-download and preprocess:
-**Important**: These attributes are not mandatory—extend Data objects with custom attributes as needed.
-
-### Edge Index Format
+```python
+from torch_geometric.datasets import Planetoid, TUDataset
-Edges are stored in COO (coordinate) format as a `[2, num_edges]` tensor:
-- First row: source node indices
-- Second row: target node indices
+# Single-graph node classification (Cora, Citeseer, Pubmed)
+dataset = Planetoid(root='./data', name='Cora')
+data = dataset[0] # single graph with train/val/test masks
-```python
-# Edge list: (0→1), (1→0), (1→2), (2→1)
-edge_index = torch.tensor([[0, 1, 1, 2],
- [1, 0, 2, 1]], dtype=torch.long)
+# Multi-graph classification (ENZYMES, MUTAG, IMDB-BINARY, etc.)
+dataset = TUDataset(root='./data', name='ENZYMES')
+# dataset[0], dataset[1], ... are individual graphs
```
-### Mini-Batch Processing
+Common datasets by task:
+- **Node classification**: Planetoid (Cora/Citeseer/Pubmed), OGB (ogbn-arxiv, ogbn-products, ogbn-mag)
+- **Graph classification**: TUDataset (MUTAG, ENZYMES, PROTEINS, IMDB-BINARY), OGB (ogbg-molhiv)
+- **Link prediction**: OGB (ogbl-collab, ogbl-citation2)
+- **Molecular**: QM7, QM9, MoleculeNet
+- **Point cloud/mesh**: ShapeNet, ModelNet10/40, FAUST
-PyG handles batching by creating block-diagonal adjacency matrices, concatenating multiple graphs into one large disconnected graph:
+### Transforms
-- Adjacency matrices are stacked diagonally
-- Node features are concatenated along the node dimension
-- A `batch` vector maps each node to its source graph
-- No padding needed—computationally efficient
+Transforms preprocess or augment graph data, analogous to torchvision transforms:
```python
-from torch_geometric.loader import DataLoader
-
-loader = DataLoader(dataset, batch_size=32, shuffle=True)
-for batch in loader:
- print(f"Batch size: {batch.num_graphs}")
- print(f"Total nodes: {batch.num_nodes}")
- # batch.batch maps nodes to graphs
+import torch_geometric.transforms as T
+
+# Common transforms
+T.NormalizeFeatures() # Row-normalize node features to sum to 1
+T.ToUndirected() # Add reverse edges to make graph undirected
+T.AddSelfLoops() # Add self-loop edges
+T.KNNGraph(k=6) # Build k-NN graph from point cloud positions
+T.RandomJitter(0.01) # Random noise augmentation on positions
+T.Compose([...]) # Chain multiple transforms
+
+# Apply as pre_transform (once, saved to disk) or transform (every access)
+dataset = ShapeNet(root='./data', pre_transform=T.KNNGraph(k=6),
+ transform=T.RandomJitter(0.01))
```
-## Building Graph Neural Networks
+## Building GNN Models
-### Message Passing Paradigm
+### Quick Start: Using Built-in Layers
-GNNs in PyG follow a neighborhood aggregation scheme:
-1. Transform node features
-2. Propagate messages along edges
-3. Aggregate messages from neighbors
-4. Update node representations
+The fastest way to build a GNN — stack conv layers from `torch_geometric.nn`:
-### Using Pre-Built Layers
-
-PyG provides 40+ convolutional layers. Common ones include:
-
-**GCNConv** (Graph Convolutional Network):
```python
-from torch_geometric.nn import GCNConv
+import torch
import torch.nn.functional as F
+from torch_geometric.nn import GCNConv
class GCN(torch.nn.Module):
- def __init__(self, num_features, num_classes):
+ def __init__(self, in_channels, hidden_channels, out_channels):
super().__init__()
- self.conv1 = GCNConv(num_features, 16)
- self.conv2 = GCNConv(16, num_classes)
-
- def forward(self, data):
- x, edge_index = data.x, data.edge_index
- x = self.conv1(x, edge_index)
- x = F.relu(x)
- x = F.dropout(x, training=self.training)
- x = self.conv2(x, edge_index)
- return F.log_softmax(x, dim=1)
-```
+ self.conv1 = GCNConv(in_channels, hidden_channels)
+ self.conv2 = GCNConv(hidden_channels, out_channels)
-**GATConv** (Graph Attention Network):
-```python
-from torch_geometric.nn import GATConv
-
-class GAT(torch.nn.Module):
- def __init__(self, num_features, num_classes):
- super().__init__()
- self.conv1 = GATConv(num_features, 8, heads=8, dropout=0.6)
- self.conv2 = GATConv(8 * 8, num_classes, heads=1, concat=False, dropout=0.6)
-
- def forward(self, data):
- x, edge_index = data.x, data.edge_index
- x = F.dropout(x, p=0.6, training=self.training)
- x = F.elu(self.conv1(x, edge_index))
- x = F.dropout(x, p=0.6, training=self.training)
+ def forward(self, x, edge_index):
+ x = self.conv1(x, edge_index).relu()
+ x = F.dropout(x, p=0.5, training=self.training)
x = self.conv2(x, edge_index)
- return F.log_softmax(x, dim=1)
+ return x
```
-**GraphSAGE**:
-```python
-from torch_geometric.nn import SAGEConv
+**Important**: PyG conv layers do NOT include activation functions — apply them yourself after each layer. This is by design for flexibility.
-class GraphSAGE(torch.nn.Module):
- def __init__(self, num_features, num_classes):
- super().__init__()
- self.conv1 = SAGEConv(num_features, 64)
- self.conv2 = SAGEConv(64, num_classes)
-
- def forward(self, data):
- x, edge_index = data.x, data.edge_index
- x = self.conv1(x, edge_index)
- x = F.relu(x)
- x = F.dropout(x, training=self.training)
- x = self.conv2(x, edge_index)
- return F.log_softmax(x, dim=1)
-```
+### Choosing a Conv Layer
-### Custom Message Passing Layers
+Pick based on your task and graph structure:
-For custom layers, inherit from `MessagePassing`:
+| Layer | Best for | Key idea |
+|-------|----------|----------|
+| `GCNConv` | Homogeneous, semi-supervised node classification | Spectral-inspired, degree-normalized aggregation |
+| `GATConv` / `GATv2Conv` | When neighbor importance varies | Attention-weighted messages |
+| `SAGEConv` | Large graphs, inductive settings | Sampling-friendly, learnable aggregation |
+| `GINConv` | Graph classification, maximizing expressiveness | As powerful as WL test |
+| `TransformerConv` | Rich edge features, complex interactions | Multi-head attention with edge features |
+| `EdgeConv` | Point clouds, dynamic graphs | MLP on edge features (x_i, x_j - x_i) |
+| `RGCNConv` | Heterogeneous with many relation types | Relation-specific weight matrices |
+| `HGTConv` | Heterogeneous graphs | Type-specific attention |
-```python
-from torch_geometric.nn import MessagePassing
-from torch_geometric.utils import add_self_loops, degree
+All conv layers accept `(x, edge_index)` at minimum. Many also accept `edge_attr` for edge features.
-class CustomConv(MessagePassing):
- def __init__(self, in_channels, out_channels):
- super().__init__(aggr='add') # "add", "mean", or "max"
- self.lin = torch.nn.Linear(in_channels, out_channels)
-
- def forward(self, x, edge_index):
- # Add self-loops to adjacency matrix
- edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0))
-
- # Transform node features
- x = self.lin(x)
-
- # Compute normalization
- row, col = edge_index
- deg = degree(col, x.size(0), dtype=x.dtype)
- deg_inv_sqrt = deg.pow(-0.5)
- norm = deg_inv_sqrt[row] * deg_inv_sqrt[col]
+### Lazy Initialization
- # Propagate messages
- return self.propagate(edge_index, x=x, norm=norm)
+Use `-1` for input channels to let PyG infer dimensions automatically — especially useful for heterogeneous models:
- def message(self, x_j, norm):
- # x_j: features of source nodes
- return norm.view(-1, 1) * x_j
+```python
+conv = SAGEConv((-1, -1), 64) # Input dims inferred on first forward pass
+# Initialize lazy modules:
+with torch.no_grad():
+ out = model(data.x, data.edge_index)
```
-Key methods:
-- **`forward()`**: Main entry point
-- **`message()`**: Constructs messages from source to target nodes
-- **`aggregate()`**: Aggregates messages (usually don't override—set `aggr` parameter)
-- **`update()`**: Updates node embeddings after aggregation
-
-**Variable naming convention**: Appending `_i` or `_j` to tensor names automatically maps them to target or source nodes.
-
-## Working with Datasets
+### High-Level Model APIs
-### Loading Built-in Datasets
-
-PyG provides extensive benchmark datasets:
+For common architectures, PyG provides ready-made model classes:
```python
-# Citation networks (node classification)
-from torch_geometric.datasets import Planetoid
-dataset = Planetoid(root='/tmp/Cora', name='Cora') # or 'CiteSeer', 'PubMed'
-
-# Graph classification
-from torch_geometric.datasets import TUDataset
-dataset = TUDataset(root='/tmp/ENZYMES', name='ENZYMES')
-
-# Molecular datasets
-from torch_geometric.datasets import QM9
-dataset = QM9(root='/tmp/QM9')
+from torch_geometric.nn import GraphSAGE, GCN, GAT, GIN
-# Large-scale datasets
-from torch_geometric.datasets import Reddit
-dataset = Reddit(root='/tmp/Reddit')
+model = GraphSAGE(
+ in_channels=dataset.num_features,
+ hidden_channels=64,
+ out_channels=dataset.num_classes,
+ num_layers=2,
+)
```
-Check `references/datasets_reference.md` for a comprehensive list.
+### Custom Layers via MessagePassing
-### Creating Custom Datasets
+To implement a novel GNN layer, subclass `MessagePassing`. The framework is:
-For datasets that fit in memory, inherit from `InMemoryDataset`:
+1. `propagate()` orchestrates the message passing
+2. `message()` defines what info flows along each edge (the phi function)
+3. `aggregate()` combines messages at each node (sum/mean/max)
+4. `update()` transforms the aggregated result (the gamma function)
```python
-from torch_geometric.data import InMemoryDataset, Data
-import torch
-
-class MyOwnDataset(InMemoryDataset):
- def __init__(self, root, transform=None, pre_transform=None):
- super().__init__(root, transform, pre_transform)
- self.load(self.processed_paths[0])
-
- @property
- def raw_file_names(self):
- return ['my_data.csv'] # Files needed in raw_dir
-
- @property
- def processed_file_names(self):
- return ['data.pt'] # Files in processed_dir
-
- def download(self):
- # Download raw data to self.raw_dir
- pass
-
- def process(self):
- # Read data, create Data objects
- data_list = []
-
- # Example: Create a simple graph
- edge_index = torch.tensor([[0, 1], [1, 0]], dtype=torch.long)
- x = torch.randn(2, 16)
- y = torch.tensor([0], dtype=torch.long)
-
- data = Data(x=x, edge_index=edge_index, y=y)
- data_list.append(data)
+from torch_geometric.nn import MessagePassing
+from torch_geometric.utils import add_self_loops, degree
- # Apply pre_filter and pre_transform
- if self.pre_filter is not None:
- data_list = [d for d in data_list if self.pre_filter(d)]
+class MyConv(MessagePassing):
+ def __init__(self, in_channels, out_channels):
+ super().__init__(aggr='add') # "add", "mean", or "max"
+ self.lin = torch.nn.Linear(in_channels, out_channels)
- if self.pre_transform is not None:
- data_list = [self.pre_transform(d) for d in data_list]
+ def forward(self, x, edge_index):
+ # Pre-processing before message passing
+ x = self.lin(x)
+ # Start message passing
+ return self.propagate(edge_index, x=x)
- # Save processed data
- self.save(data_list, self.processed_paths[0])
+ def message(self, x_j):
+ # x_j: features of source nodes for each edge [num_edges, features]
+ # The _j suffix auto-indexes source nodes, _i indexes target nodes
+ return x_j
```
-For large datasets that don't fit in memory, inherit from `Dataset` and implement `len()` and `get(idx)`.
+**The `_i` / `_j` convention**: any tensor passed to `propagate()` can be auto-indexed by appending `_i` (target/central node) or `_j` (source/neighbor node) in the `message()` signature. So if you pass `x=...` to propagate, you can access `x_i` and `x_j` in message().
-### Loading Graphs from CSV
+Read `references/message_passing.md` for the full GCN and EdgeConv implementation examples.
-```python
-import pandas as pd
-import torch
-from torch_geometric.data import HeteroData
+## Task-Specific Patterns
-# Load nodes
-nodes_df = pd.read_csv('nodes.csv')
-x = torch.tensor(nodes_df[['feat1', 'feat2']].values, dtype=torch.float)
-
-# Load edges
-edges_df = pd.read_csv('edges.csv')
-edge_index = torch.tensor([edges_df['source'].values,
- edges_df['target'].values], dtype=torch.long)
-
-data = Data(x=x, edge_index=edge_index)
-```
-
-## Training Workflows
-
-### Node Classification (Single Graph)
+### Node Classification
```python
-import torch
-import torch.nn.functional as F
-from torch_geometric.datasets import Planetoid
-
-# Load dataset
-dataset = Planetoid(root='/tmp/Cora', name='Cora')
-data = dataset[0]
-
-# Create model
-model = GCN(dataset.num_features, dataset.num_classes)
-optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)
-
-# Training
+# Full-batch training on a single graph (e.g., Cora)
model.train()
for epoch in range(200):
optimizer.zero_grad()
- out = model(data)
- loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask])
+ out = model(data.x, data.edge_index)
+ loss = F.cross_entropy(out[data.train_mask], data.y[data.train_mask])
loss.backward()
optimizer.step()
- if epoch % 10 == 0:
- print(f'Epoch {epoch}, Loss: {loss.item():.4f}')
-
# Evaluation
model.eval()
-pred = model(data).argmax(dim=1)
-correct = (pred[data.test_mask] == data.y[data.test_mask]).sum()
-acc = int(correct) / int(data.test_mask.sum())
-print(f'Test Accuracy: {acc:.4f}')
+pred = model(data.x, data.edge_index).argmax(dim=1)
+acc = (pred[data.test_mask] == data.y[data.test_mask]).float().mean()
```
-### Graph Classification (Multiple Graphs)
+### Graph Classification
+
+Multiple graphs — use `DataLoader` for mini-batching and global pooling to get graph-level representations:
```python
-from torch_geometric.datasets import TUDataset
from torch_geometric.loader import DataLoader
-from torch_geometric.nn import global_mean_pool
+from torch_geometric.nn import GCNConv, global_mean_pool
+
+loader = DataLoader(dataset, batch_size=32, shuffle=True)
class GraphClassifier(torch.nn.Module):
- def __init__(self, num_features, num_classes):
+ def __init__(self, in_ch, hidden_ch, out_ch):
super().__init__()
- self.conv1 = GCNConv(num_features, 64)
- self.conv2 = GCNConv(64, 64)
- self.lin = torch.nn.Linear(64, num_classes)
-
- def forward(self, data):
- x, edge_index, batch = data.x, data.edge_index, data.batch
+ self.conv1 = GCNConv(in_ch, hidden_ch)
+ self.conv2 = GCNConv(hidden_ch, hidden_ch)
+ self.lin = torch.nn.Linear(hidden_ch, out_ch)
+
+ def forward(self, x, edge_index, batch):
+ x = self.conv1(x, edge_index).relu()
+ x = self.conv2(x, edge_index).relu()
+ x = global_mean_pool(x, batch) # [num_graphs_in_batch, hidden_ch]
+ return self.lin(x)
+
+# Training loop
+for data in loader:
+ out = model(data.x, data.edge_index, data.batch)
+ loss = F.cross_entropy(out, data.y)
+```
- x = self.conv1(x, edge_index)
- x = F.relu(x)
- x = self.conv2(x, edge_index)
- x = F.relu(x)
+PyG's `DataLoader` batches multiple graphs by creating block-diagonal adjacency matrices. The `batch` tensor maps each node to its graph index. Pooling ops (`global_mean_pool`, `global_max_pool`, `global_add_pool`) use this to aggregate per-graph.
- # Global pooling (aggregate node features to graph-level)
- x = global_mean_pool(x, batch)
+### Link Prediction
- x = self.lin(x)
- return F.log_softmax(x, dim=1)
+Split edges into train/val/test, use negative sampling:
-# Load dataset
-dataset = TUDataset(root='/tmp/ENZYMES', name='ENZYMES')
-loader = DataLoader(dataset, batch_size=32, shuffle=True)
+```python
+from torch_geometric.transforms import RandomLinkSplit
-model = GraphClassifier(dataset.num_features, dataset.num_classes)
-optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
+transform = RandomLinkSplit(
+ num_val=0.1,
+ num_test=0.1,
+ is_undirected=True,
+ add_negative_train_samples=False,
+)
+train_data, val_data, test_data = transform(data)
-# Training
-model.train()
-for epoch in range(100):
- total_loss = 0
- for batch in loader:
- optimizer.zero_grad()
- out = model(batch)
- loss = F.nll_loss(out, batch.y)
- loss.backward()
- optimizer.step()
- total_loss += loss.item()
-
- if epoch % 10 == 0:
- print(f'Epoch {epoch}, Loss: {total_loss / len(loader):.4f}')
+# Encode nodes, then score edges
+z = model.encode(train_data.x, train_data.edge_index)
+# Positive edges
+pos_score = (z[train_data.edge_label_index[0]] * z[train_data.edge_label_index[1]]).sum(dim=1)
```
-### Large-Scale Graphs with Neighbor Sampling
+Read `references/link_prediction.md` for the complete link prediction guide: GAE/VGAE autoencoders, full training loops, LinkNeighborLoader for large graphs, heterogeneous link prediction, and evaluation metrics.
+
+## Scaling to Large Graphs
-For large graphs, use `NeighborLoader` to sample subgraphs:
+For graphs that don't fit in GPU memory, use neighbor sampling via `NeighborLoader`:
```python
from torch_geometric.loader import NeighborLoader
-# Create a neighbor sampler
train_loader = NeighborLoader(
data,
- num_neighbors=[25, 10], # Sample 25 neighbors for 1st hop, 10 for 2nd hop
- batch_size=128,
- input_nodes=data.train_mask,
+ num_neighbors=[15, 10], # Sample 15 neighbors in hop 1, 10 in hop 2
+ batch_size=128, # Number of seed nodes per batch
+ input_nodes=data.train_mask, # Which nodes to sample from
+ shuffle=True,
)
-# Training
-model.train()
for batch in train_loader:
- optimizer.zero_grad()
- out = model(batch)
- # Only compute loss on seed nodes (first batch_size nodes)
- loss = F.nll_loss(out[:batch.batch_size], batch.y[:batch.batch_size])
- loss.backward()
- optimizer.step()
+ batch = batch.to(device)
+ out = model(batch.x, batch.edge_index)
+ # Only use first batch_size nodes for loss (these are the seed nodes)
+ loss = F.cross_entropy(out[:batch.batch_size], batch.y[:batch.batch_size])
```
-**Important**:
-- Output subgraphs are directed
-- Node indices are relabeled (0 to batch.num_nodes - 1)
-- Only use seed node predictions for loss computation
-- Sampling beyond 2-3 hops is generally not feasible
+**Key points about NeighborLoader**:
+- `num_neighbors` list length should match GNN depth (number of message passing layers)
+- Seed nodes are always the first `batch.batch_size` nodes in the output
+- `batch.n_id` maps relabeled indices back to original node IDs
+- Works for both `Data` and `HeteroData`
+- For link prediction, use `LinkNeighborLoader` instead
+- Sampling more than 2-3 hops is generally infeasible (exponential blowup)
-## Advanced Features
+Other scalability options: `ClusterLoader` (ClusterGCN), `GraphSAINTSampler`, `ShaDowKHopSampler`. For multi-GPU training, DDP, PyTorch Lightning integration, and `torch.compile` support, read `references/scaling.md`.
-### Heterogeneous Graphs
+## Heterogeneous Graphs
-For graphs with multiple node and edge types, use `HeteroData`:
+For graphs with multiple node and edge types (social networks, knowledge graphs, recommendation):
```python
from torch_geometric.data import HeteroData
data = HeteroData()
-# Add node features for different types
-data['paper'].x = torch.randn(100, 128) # 100 papers with 128 features
-data['author'].x = torch.randn(200, 64) # 200 authors with 64 features
+# Node features — indexed by node type string
+data['user'].x = torch.randn(1000, 64)
+data['movie'].x = torch.randn(500, 128)
-# Add edges for different types (source_type, edge_type, target_type)
-data['author', 'writes', 'paper'].edge_index = torch.randint(0, 200, (2, 500))
-data['paper', 'cites', 'paper'].edge_index = torch.randint(0, 100, (2, 300))
+# Edge indices — indexed by (src_type, edge_type, dst_type) triplet
+data['user', 'rates', 'movie'].edge_index = torch.randint(0, 500, (2, 3000))
+data['user', 'follows', 'user'].edge_index = torch.randint(0, 1000, (2, 5000))
-print(data)
+# Access convenience dicts
+data.x_dict # {'user': tensor, 'movie': tensor}
+data.edge_index_dict # {('user','rates','movie'): tensor, ...}
+data.metadata() # ([node_types], [edge_types])
```
-Convert homogeneous models to heterogeneous:
+### Three ways to build heterogeneous GNNs
+
+**1. Auto-convert with `to_hetero()`** — write a homogeneous model, convert automatically:
```python
-from torch_geometric.nn import to_hetero
+from torch_geometric.nn import SAGEConv, to_hetero
+
+class GNN(torch.nn.Module):
+ def __init__(self, hidden_channels, out_channels):
+ super().__init__()
+ self.conv1 = SAGEConv((-1, -1), hidden_channels)
+ self.conv2 = SAGEConv((-1, -1), out_channels)
-# Define homogeneous model
-model = GNN(...)
+ def forward(self, x, edge_index):
+ x = self.conv1(x, edge_index).relu()
+ x = self.conv2(x, edge_index)
+ return x
-# Convert to heterogeneous
+model = GNN(64, dataset.num_classes)
model = to_hetero(model, data.metadata(), aggr='sum')
-# Use as normal
+# Now accepts dicts:
out = model(data.x_dict, data.edge_index_dict)
```
-Or use `HeteroConv` for custom edge-type-specific operations:
+Use `(-1, -1)` for bipartite input channels (source, target may differ). Lazy init handles the rest.
+
+**2. `HeteroConv` wrapper** — different conv per edge type:
```python
-from torch_geometric.nn import HeteroConv, GCNConv, SAGEConv
+from torch_geometric.nn import HeteroConv, GCNConv, SAGEConv, GATConv
-class HeteroGNN(torch.nn.Module):
- def __init__(self, metadata):
- super().__init__()
- self.conv1 = HeteroConv({
- ('paper', 'cites', 'paper'): GCNConv(-1, 64),
- ('author', 'writes', 'paper'): SAGEConv((-1, -1), 64),
- }, aggr='sum')
-
- self.conv2 = HeteroConv({
- ('paper', 'cites', 'paper'): GCNConv(64, 32),
- ('author', 'writes', 'paper'): SAGEConv((64, 64), 32),
- }, aggr='sum')
-
- def forward(self, x_dict, edge_index_dict):
- x_dict = self.conv1(x_dict, edge_index_dict)
- x_dict = {key: F.relu(x) for key, x in x_dict.items()}
- x_dict = self.conv2(x_dict, edge_index_dict)
- return x_dict
+conv = HeteroConv({
+ ('paper', 'cites', 'paper'): GCNConv(-1, 64),
+ ('author', 'writes', 'paper'): SAGEConv((-1, -1), 64),
+ ('paper', 'rev_writes', 'author'): GATConv((-1, -1), 64, add_self_loops=False),
+}, aggr='sum')
```
-### Transforms
-
-Apply transforms to modify graph structure or features:
+**3. Native heterogeneous operators** like `HGTConv`:
```python
-from torch_geometric.transforms import NormalizeFeatures, AddSelfLoops, Compose
-
-# Single transform
-transform = NormalizeFeatures()
-dataset = Planetoid(root='/tmp/Cora', name='Cora', transform=transform)
-
-# Compose multiple transforms
-transform = Compose([
- AddSelfLoops(),
- NormalizeFeatures(),
-])
-dataset = Planetoid(root='/tmp/Cora', name='Cora', transform=transform)
+from torch_geometric.nn import HGTConv
+conv = HGTConv(hidden_channels, hidden_channels, data.metadata(), num_heads=4)
```
-Common transforms:
-- **Structure**: `ToUndirected`, `AddSelfLoops`, `RemoveSelfLoops`, `KNNGraph`, `RadiusGraph`
-- **Features**: `NormalizeFeatures`, `NormalizeScale`, `Center`
-- **Sampling**: `RandomNodeSplit`, `RandomLinkSplit`
-- **Positional Encoding**: `AddLaplacianEigenvectorPE`, `AddRandomWalkPE`
+**Important for heterogeneous graphs**:
+- Use `T.ToUndirected()` to add reverse edge types for bidirectional message flow
+- Disable `add_self_loops` in bipartite conv layers (different source/dest types) — use skip connections instead: `conv(x, edge_index) + lin(x)`
+- For NeighborLoader on HeteroData, specify `input_nodes` as `('node_type', mask)` tuple
+- `num_neighbors` can be a dict keyed by edge type for fine-grained control
+
+Read `references/heterogeneous.md` for complete examples including training loops and NeighborLoader usage with heterogeneous graphs.
+
+## Custom Datasets
+
+For loading your own data into PyG:
+
+- **Quick (no class needed)**: Create `Data` objects directly and pass a list to `DataLoader`
+- **Reusable (fits in RAM)**: Subclass `InMemoryDataset` — override `raw_file_names`, `processed_file_names`, `download()`, `process()`
+- **Large (disk-backed)**: Subclass `Dataset` — also override `len()` and `get()`
+- **From CSV**: Load node/edge tables with pandas, build mappings to consecutive indices, assemble into `Data` or `HeteroData`
+- **From NetworkX**: `from_networkx(G)` converts a NetworkX graph directly
+- **From scipy sparse**: `from_scipy_sparse_matrix(adj)` extracts edge_index
-See `references/transforms_reference.md` for the full list.
+Read `references/custom_datasets.md` for complete examples with all patterns, CSV loading with encoders, and the MovieLens walkthrough.
-### Model Explainability
+## Explainability
-PyG provides explainability tools to understand model predictions:
+PyG provides `torch_geometric.explain` for interpreting GNN predictions:
```python
from torch_geometric.explain import Explainer, GNNExplainer
-# Create explainer
explainer = Explainer(
model=model,
algorithm=GNNExplainer(epochs=200),
- explanation_type='model', # or 'phenomenon'
+ explanation_type='model',
node_mask_type='attributes',
edge_mask_type='object',
model_config=dict(
@@ -550,125 +400,22 @@ explainer = Explainer(
),
)
-# Generate explanation for a specific node
-node_idx = 10
-explanation = explainer(data.x, data.edge_index, index=node_idx)
-
-# Visualize
-print(f'Node {node_idx} explanation:')
-print(f'Important edges: {explanation.edge_mask.topk(5).indices}')
-print(f'Important features: {explanation.node_mask[node_idx].topk(5).indices}')
+explanation = explainer(data.x, data.edge_index, index=10)
+explanation.visualize_graph() # Important subgraph
+explanation.visualize_feature_importance(top_k=10) # Feature importance
```
-### Pooling Operations
-
-For hierarchical graph representations:
-
-```python
-from torch_geometric.nn import TopKPooling, global_mean_pool
-
-class HierarchicalGNN(torch.nn.Module):
- def __init__(self, num_features, num_classes):
- super().__init__()
- self.conv1 = GCNConv(num_features, 64)
- self.pool1 = TopKPooling(64, ratio=0.8)
- self.conv2 = GCNConv(64, 64)
- self.pool2 = TopKPooling(64, ratio=0.8)
- self.lin = torch.nn.Linear(64, num_classes)
-
- def forward(self, data):
- x, edge_index, batch = data.x, data.edge_index, data.batch
-
- x = F.relu(self.conv1(x, edge_index))
- x, edge_index, _, batch, _, _ = self.pool1(x, edge_index, None, batch)
-
- x = F.relu(self.conv2(x, edge_index))
- x, edge_index, _, batch, _, _ = self.pool2(x, edge_index, None, batch)
-
- x = global_mean_pool(x, batch)
- x = self.lin(x)
- return F.log_softmax(x, dim=1)
-```
-
-## Common Patterns and Best Practices
-
-### Check Graph Properties
-
-```python
-# Undirected check
-from torch_geometric.utils import is_undirected
-print(f"Is undirected: {is_undirected(data.edge_index)}")
-
-# Connected components
-from torch_geometric.utils import connected_components
-print(f"Connected components: {connected_components(data.edge_index)}")
-
-# Contains self-loops
-from torch_geometric.utils import contains_self_loops
-print(f"Has self-loops: {contains_self_loops(data.edge_index)}")
-```
-
-### GPU Training
-
-```python
-device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
-model = model.to(device)
-data = data.to(device)
-
-# For DataLoader
-for batch in loader:
- batch = batch.to(device)
- # Train...
-```
-
-### Save and Load Models
-
-```python
-# Save
-torch.save(model.state_dict(), 'model.pth')
-
-# Load
-model = GCN(num_features, num_classes)
-model.load_state_dict(torch.load('model.pth'))
-model.eval()
-```
-
-### Layer Capabilities
-
-When choosing layers, consider these capabilities:
-- **SparseTensor**: Supports efficient sparse matrix operations
-- **edge_weight**: Handles one-dimensional edge weights
-- **edge_attr**: Processes multi-dimensional edge features
-- **Bipartite**: Works with bipartite graphs (different source/target dimensions)
-- **Lazy**: Enables initialization without specifying input dimensions
-
-See the GNN cheatsheet at `references/layer_capabilities.md`.
-
-## Resources
-
-### Bundled References
-
-This skill includes detailed reference documentation:
-
-- **`references/layers_reference.md`**: Complete listing of all 40+ GNN layers with descriptions and capabilities
-- **`references/datasets_reference.md`**: Comprehensive dataset catalog organized by category
-- **`references/transforms_reference.md`**: All available transforms and their use cases
-- **`references/api_patterns.md`**: Common API patterns and coding examples
-
-### Scripts
-
-Utility scripts are provided in `scripts/`:
-
-- **`scripts/visualize_graph.py`**: Visualize graph structure using networkx and matplotlib
-- **`scripts/create_gnn_template.py`**: Generate boilerplate code for common GNN architectures
-- **`scripts/benchmark_model.py`**: Benchmark model performance on standard datasets
-
-Execute scripts directly or read them for implementation patterns.
+Available algorithms: `GNNExplainer` (optimization-based), `PGExplainer` (parametric, trained), `CaptumExplainer` (gradient-based via Captum), `AttentionExplainer` (attention weights). Works for both homogeneous and heterogeneous graphs.
-### Official Resources
+Read `references/explainability.md` for all algorithms, heterogeneous explanations, evaluation metrics, and PGExplainer training.
-- **Documentation**: https://pytorch-geometric.readthedocs.io/
-- **GitHub**: https://github.com/pyg-team/pytorch_geometric
-- **Tutorials**: https://pytorch-geometric.readthedocs.io/en/latest/get_started/introduction.html
-- **Examples**: https://github.com/pyg-team/pytorch_geometric/tree/master/examples
+## Common Pitfalls
+1. **edge_index shape**: Must be `[2, num_edges]`, not `[num_edges, 2]`. Transpose if needed.
+2. **Forgetting activations**: Conv layers don't include ReLU/etc — add them manually.
+3. **Self-loops in hetero bipartite**: Don't use `add_self_loops=True` when source and dest node types differ. Use skip connections instead.
+4. **NeighborLoader slicing**: Only the first `batch.batch_size` nodes are your seed nodes. Slice predictions and labels accordingly.
+5. **Undirected graphs**: If your graph is undirected, include edges in both directions in `edge_index`, or use `T.ToUndirected()`.
+6. **Lazy init**: Models with `-1` input channels need one forward pass with `torch.no_grad()` before training to initialize parameters.
+7. **Global pooling for graph tasks**: Use `global_mean_pool(x, batch)` (not manual reshape) to aggregate node features to graph-level.
+8. **num_neighbors alignment**: Keep `len(num_neighbors)` equal to the number of GNN layers. More hops than layers wastes compute; fewer means wasted model capacity.
diff --git a/scientific-skills/torch-geometric/references/custom_datasets.md b/scientific-skills/torch-geometric/references/custom_datasets.md
new file mode 100644
index 00000000..e3b91f66
--- /dev/null
+++ b/scientific-skills/torch-geometric/references/custom_datasets.md
@@ -0,0 +1,238 @@
+# Custom Datasets — Full Reference
+
+How to create your own graph datasets and load graph data from raw sources (CSV, pandas, numpy, etc.).
+
+## Quick: No Dataset Class Needed
+
+For synthetic data or one-off graphs, skip the dataset machinery — just create `Data` objects and pass them to `DataLoader`:
+
+```python
+from torch_geometric.data import Data
+from torch_geometric.loader import DataLoader
+
+data_list = [Data(x=..., edge_index=..., y=...) for _ in range(100)]
+loader = DataLoader(data_list, batch_size=32)
+```
+
+## InMemoryDataset (fits in RAM)
+
+For reusable datasets that fit in CPU memory. Override 4 methods:
+
+```python
+from torch_geometric.data import InMemoryDataset, download_url
+
+class MyDataset(InMemoryDataset):
+ def __init__(self, root, transform=None, pre_transform=None, pre_filter=None):
+ super().__init__(root, transform, pre_transform, pre_filter)
+ self.load(self.processed_paths[0])
+
+ @property
+ def raw_file_names(self):
+ # Files in raw_dir that must exist to skip download()
+ return ['data.csv']
+
+ @property
+ def processed_file_names(self):
+ # Files in processed_dir that must exist to skip process()
+ return ['data.pt']
+
+ def download(self):
+ # Download raw files to self.raw_dir
+ download_url('https://example.com/data.csv', self.raw_dir)
+
+ def process(self):
+ # Read raw data and create a list of Data objects
+ data_list = [...]
+
+ if self.pre_filter is not None:
+ data_list = [d for d in data_list if self.pre_filter(d)]
+ if self.pre_transform is not None:
+ data_list = [self.pre_transform(d) for d in data_list]
+
+ # save() collates list into one big Data + slices dict, then saves
+ self.save(data_list, self.processed_paths[0])
+```
+
+**Directory structure created automatically:**
+```
+root/
+├── raw/ # raw_dir — downloaded files go here
+│ └── data.csv
+└── processed/ # processed_dir — processed .pt files go here
+ └── data.pt
+```
+
+**Key behaviors:**
+- `download()` runs only if files in `raw_file_names` are missing from `raw_dir`
+- `process()` runs only if files in `processed_file_names` are missing from `processed_dir`
+- If you change `pre_transform`, delete the `processed/` directory to reprocess
+
+## Dataset (doesn't fit in RAM)
+
+For very large datasets, save each graph individually:
+
+```python
+import os.path as osp
+import torch
+from torch_geometric.data import Dataset, download_url
+
+class LargeDataset(Dataset):
+ def __init__(self, root, transform=None, pre_transform=None):
+ super().__init__(root, transform, pre_transform)
+
+ @property
+ def raw_file_names(self):
+ return ['graph_data.csv']
+
+ @property
+ def processed_file_names(self):
+ return [f'data_{i}.pt' for i in range(1000)]
+
+ def download(self):
+ download_url('...', self.raw_dir)
+
+ def process(self):
+ for idx in range(1000):
+ data = Data(...) # Build graph from raw data
+ if self.pre_filter is not None and not self.pre_filter(data):
+ continue
+ if self.pre_transform is not None:
+ data = self.pre_transform(data)
+ torch.save(data, osp.join(self.processed_dir, f'data_{idx}.pt'))
+
+ def len(self):
+ return 1000
+
+ def get(self, idx):
+ return torch.load(osp.join(self.processed_dir, f'data_{idx}.pt'))
+```
+
+## Loading Graphs from CSV
+
+A common pattern: load node/edge data from CSV files into a HeteroData object.
+
+### Step 1: Load node features
+
+```python
+import pandas as pd
+import torch
+
+def load_node_csv(path, index_col, encoders=None):
+ df = pd.read_csv(path, index_col=index_col)
+ # Map original IDs to consecutive 0..N-1 indices
+ mapping = {idx: i for i, idx in enumerate(df.index.unique())}
+
+ x = None
+ if encoders is not None:
+ xs = [encoder(df[col]) for col, encoder in encoders.items()]
+ x = torch.cat(xs, dim=-1)
+
+ return x, mapping
+```
+
+### Step 2: Load edges
+
+```python
+def load_edge_csv(path, src_index_col, src_mapping, dst_index_col, dst_mapping,
+ encoders=None):
+ df = pd.read_csv(path)
+ src = [src_mapping[idx] for idx in df[src_index_col]]
+ dst = [dst_mapping[idx] for idx in df[dst_index_col]]
+ edge_index = torch.tensor([src, dst])
+
+ edge_attr = None
+ if encoders is not None:
+ edge_attrs = [encoder(df[col]) for col, encoder in encoders.items()]
+ edge_attr = torch.cat(edge_attrs, dim=-1)
+
+ return edge_index, edge_attr
+```
+
+### Step 3: Assemble HeteroData
+
+```python
+from torch_geometric.data import HeteroData
+
+# Load nodes
+movie_x, movie_mapping = load_node_csv('movies.csv', 'movieId',
+ encoders={'genres': GenresEncoder()})
+_, user_mapping = load_node_csv('ratings.csv', 'userId')
+
+# Load edges
+edge_index, edge_label = load_edge_csv('ratings.csv',
+ src_index_col='userId', src_mapping=user_mapping,
+ dst_index_col='movieId', dst_mapping=movie_mapping,
+ encoders={'rating': IdentityEncoder(dtype=torch.long)})
+
+# Build HeteroData
+data = HeteroData()
+data['user'].num_nodes = len(user_mapping)
+data['movie'].x = movie_x
+data['user', 'rates', 'movie'].edge_index = edge_index
+data['user', 'rates', 'movie'].edge_label = edge_label
+```
+
+### Common Encoders
+
+```python
+class IdentityEncoder:
+ """Encode a numeric column as-is."""
+ def __init__(self, dtype=None):
+ self.dtype = dtype
+ def __call__(self, df):
+ return torch.from_numpy(df.values).view(-1, 1).to(self.dtype)
+
+class GenresEncoder:
+ """Multi-hot encode a pipe-separated categorical column."""
+ def __init__(self, sep='|'):
+ self.sep = sep
+ def __call__(self, df):
+ genres = set(g for col in df.values for g in col.split(self.sep))
+ mapping = {genre: i for i, genre in enumerate(genres)}
+ x = torch.zeros(len(df), len(mapping))
+ for i, col in enumerate(df.values):
+ for genre in col.split(self.sep):
+ x[i, mapping[genre]] = 1
+ return x
+```
+
+For text features, use sentence-transformers:
+
+```python
+from sentence_transformers import SentenceTransformer
+
+class SequenceEncoder:
+ def __init__(self, model_name='all-MiniLM-L6-v2'):
+ self.model = SentenceTransformer(model_name)
+ @torch.no_grad()
+ def __call__(self, df):
+ return self.model.encode(df.values, convert_to_tensor=True).cpu()
+```
+
+## From NetworkX
+
+```python
+from torch_geometric.utils import from_networkx
+import networkx as nx
+
+G = nx.karate_club_graph()
+data = from_networkx(G)
+# Node attributes become data.x, edge attributes become data.edge_attr
+```
+
+## From scipy sparse adjacency matrix
+
+```python
+from torch_geometric.utils import from_scipy_sparse_matrix
+
+edge_index, edge_attr = from_scipy_sparse_matrix(adj_matrix)
+data = Data(x=features, edge_index=edge_index)
+```
+
+## Featureless Nodes
+
+If nodes have no features, common options:
+- Use `torch.nn.Embedding` to learn features during training
+- Set `data['node_type'].num_nodes = N` (for HeteroData)
+- Use structural features: degree, clustering coefficient, etc.
+- Use `data.x = torch.eye(num_nodes)` (one-hot, only for small graphs)
diff --git a/scientific-skills/torch-geometric/references/datasets_reference.md b/scientific-skills/torch-geometric/references/datasets_reference.md
deleted file mode 100644
index 344cc9b0..00000000
--- a/scientific-skills/torch-geometric/references/datasets_reference.md
+++ /dev/null
@@ -1,574 +0,0 @@
-# PyTorch Geometric Datasets Reference
-
-This document provides a comprehensive catalog of all datasets available in `torch_geometric.datasets`.
-
-## Citation Networks
-
-### Planetoid
-**Usage**: Node classification, semi-supervised learning
-**Networks**: Cora, CiteSeer, PubMed
-**Description**: Citation networks where nodes are papers and edges are citations
-- **Cora**: 2,708 nodes, 5,429 edges, 7 classes, 1,433 features
-- **CiteSeer**: 3,327 nodes, 4,732 edges, 6 classes, 3,703 features
-- **PubMed**: 19,717 nodes, 44,338 edges, 3 classes, 500 features
-
-```python
-from torch_geometric.datasets import Planetoid
-dataset = Planetoid(root='/tmp/Cora', name='Cora')
-```
-
-### Coauthor
-**Usage**: Node classification on collaboration networks
-**Networks**: CS, Physics
-**Description**: Co-authorship networks from Microsoft Academic Graph
-- **CS**: 18,333 nodes, 81,894 edges, 15 classes (computer science)
-- **Physics**: 34,493 nodes, 247,962 edges, 5 classes (physics)
-
-```python
-from torch_geometric.datasets import Coauthor
-dataset = Coauthor(root='/tmp/CS', name='CS')
-```
-
-### Amazon
-**Usage**: Node classification on product networks
-**Networks**: Computers, Photo
-**Description**: Amazon co-purchase networks where nodes are products
-- **Computers**: 13,752 nodes, 245,861 edges, 10 classes
-- **Photo**: 7,650 nodes, 119,081 edges, 8 classes
-
-```python
-from torch_geometric.datasets import Amazon
-dataset = Amazon(root='/tmp/Computers', name='Computers')
-```
-
-### CitationFull
-**Usage**: Citation network analysis
-**Networks**: Cora, Cora_ML, DBLP, PubMed
-**Description**: Full citation networks without sampling
-
-```python
-from torch_geometric.datasets import CitationFull
-dataset = CitationFull(root='/tmp/Cora', name='Cora')
-```
-
-## Graph Classification
-
-### TUDataset
-**Usage**: Graph classification, graph kernel benchmarks
-**Description**: Collection of 120+ graph classification datasets
-- **MUTAG**: 188 graphs, 2 classes (molecular compounds)
-- **PROTEINS**: 1,113 graphs, 2 classes (protein structures)
-- **ENZYMES**: 600 graphs, 6 classes (protein enzymes)
-- **IMDB-BINARY**: 1,000 graphs, 2 classes (social networks)
-- **REDDIT-BINARY**: 2,000 graphs, 2 classes (discussion threads)
-- **COLLAB**: 5,000 graphs, 3 classes (scientific collaborations)
-- **NCI1**: 4,110 graphs, 2 classes (chemical compounds)
-- **DD**: 1,178 graphs, 2 classes (protein structures)
-
-```python
-from torch_geometric.datasets import TUDataset
-dataset = TUDataset(root='/tmp/ENZYMES', name='ENZYMES')
-```
-
-### MoleculeNet
-**Usage**: Molecular property prediction
-**Datasets**: Over 10 molecular benchmark datasets
-**Description**: Comprehensive molecular machine learning benchmarks
-- **ESOL**: Aqueous solubility (regression)
-- **FreeSolv**: Hydration free energy (regression)
-- **Lipophilicity**: Octanol/water distribution (regression)
-- **BACE**: Binding results (classification)
-- **BBBP**: Blood-brain barrier penetration (classification)
-- **HIV**: HIV inhibition (classification)
-- **Tox21**: Toxicity prediction (multi-task classification)
-- **ToxCast**: Toxicology forecasting (multi-task classification)
-- **SIDER**: Side effects (multi-task classification)
-- **ClinTox**: Clinical trial toxicity (multi-task classification)
-
-```python
-from torch_geometric.datasets import MoleculeNet
-dataset = MoleculeNet(root='/tmp/ESOL', name='ESOL')
-```
-
-## Molecular and Chemical Datasets
-
-### QM7b
-**Usage**: Molecular property prediction (quantum mechanics)
-**Description**: 7,211 molecules with up to 7 heavy atoms
-- Properties: Atomization energies, electronic properties
-
-```python
-from torch_geometric.datasets import QM7b
-dataset = QM7b(root='/tmp/QM7b')
-```
-
-### QM9
-**Usage**: Molecular property prediction (quantum mechanics)
-**Description**: ~130,000 molecules with up to 9 heavy atoms (C, O, N, F)
-- Properties: 19 quantum chemical properties including HOMO, LUMO, gap, energy
-
-```python
-from torch_geometric.datasets import QM9
-dataset = QM9(root='/tmp/QM9')
-```
-
-### ZINC
-**Usage**: Molecular generation, property prediction
-**Description**: ~250,000 drug-like molecular graphs
-- Properties: Constrained solubility, molecular weight
-
-```python
-from torch_geometric.datasets import ZINC
-dataset = ZINC(root='/tmp/ZINC', subset=True)
-```
-
-### AQSOL
-**Usage**: Aqueous solubility prediction
-**Description**: ~10,000 molecules with solubility measurements
-
-```python
-from torch_geometric.datasets import AQSOL
-dataset = AQSOL(root='/tmp/AQSOL')
-```
-
-### MD17
-**Usage**: Molecular dynamics, force field learning
-**Description**: Molecular dynamics trajectories for small molecules
-- Molecules: Benzene, Uracil, Naphthalene, Aspirin, Salicylic acid, etc.
-
-```python
-from torch_geometric.datasets import MD17
-dataset = MD17(root='/tmp/MD17', name='benzene')
-```
-
-### PCQM4Mv2
-**Usage**: Large-scale molecular property prediction
-**Description**: 3.8M molecules from PubChem for quantum chemistry
-- Part of OGB Large-Scale Challenge
-
-```python
-from torch_geometric.datasets import PCQM4Mv2
-dataset = PCQM4Mv2(root='/tmp/PCQM4Mv2')
-```
-
-## Social Networks
-
-### Reddit
-**Usage**: Large-scale node classification
-**Description**: Reddit posts from September 2014
-- 232,965 nodes, 11,606,919 edges, 41 classes
-- Features: TF-IDF of post content
-
-```python
-from torch_geometric.datasets import Reddit
-dataset = Reddit(root='/tmp/Reddit')
-```
-
-### Reddit2
-**Usage**: Large-scale node classification
-**Description**: Updated Reddit dataset with more posts
-
-```python
-from torch_geometric.datasets import Reddit2
-dataset = Reddit2(root='/tmp/Reddit2')
-```
-
-### Twitch
-**Usage**: Node classification, social network analysis
-**Networks**: DE, EN, ES, FR, PT, RU
-**Description**: Twitch user networks by language
-
-```python
-from torch_geometric.datasets import Twitch
-dataset = Twitch(root='/tmp/Twitch', name='DE')
-```
-
-### Facebook
-**Usage**: Social network analysis, node classification
-**Description**: Facebook page-page networks
-
-```python
-from torch_geometric.datasets import FacebookPagePage
-dataset = FacebookPagePage(root='/tmp/Facebook')
-```
-
-### GitHub
-**Usage**: Social network analysis
-**Description**: GitHub developer networks
-
-```python
-from torch_geometric.datasets import GitHub
-dataset = GitHub(root='/tmp/GitHub')
-```
-
-## Knowledge Graphs
-
-### Entities
-**Usage**: Link prediction, knowledge graph embeddings
-**Datasets**: AIFB, MUTAG, BGS, AM
-**Description**: RDF knowledge graphs with typed relations
-
-```python
-from torch_geometric.datasets import Entities
-dataset = Entities(root='/tmp/AIFB', name='AIFB')
-```
-
-### WordNet18
-**Usage**: Link prediction on semantic networks
-**Description**: Subset of WordNet with 18 relations
-- 40,943 entities, 151,442 triplets
-
-```python
-from torch_geometric.datasets import WordNet18
-dataset = WordNet18(root='/tmp/WordNet18')
-```
-
-### WordNet18RR
-**Usage**: Link prediction (no inverse relations)
-**Description**: Refined version without inverse relations
-
-```python
-from torch_geometric.datasets import WordNet18RR
-dataset = WordNet18RR(root='/tmp/WordNet18RR')
-```
-
-### FB15k-237
-**Usage**: Link prediction on Freebase
-**Description**: Subset of Freebase with 237 relations
-- 14,541 entities, 310,116 triplets
-
-```python
-from torch_geometric.datasets import FB15k_237
-dataset = FB15k_237(root='/tmp/FB15k')
-```
-
-## Heterogeneous Graphs
-
-### OGB_MAG
-**Usage**: Heterogeneous graph learning, node classification
-**Description**: Microsoft Academic Graph with multiple node/edge types
-- Node types: paper, author, institution, field of study
-- 1M+ nodes, 21M+ edges
-
-```python
-from torch_geometric.datasets import OGB_MAG
-dataset = OGB_MAG(root='/tmp/OGB_MAG')
-```
-
-### MovieLens
-**Usage**: Recommendation systems, link prediction
-**Versions**: 100K, 1M, 10M, 20M
-**Description**: User-movie rating networks
-- Node types: user, movie
-- Edge types: rates
-
-```python
-from torch_geometric.datasets import MovieLens
-dataset = MovieLens(root='/tmp/MovieLens', model_name='100k')
-```
-
-### IMDB
-**Usage**: Heterogeneous graph learning
-**Description**: IMDB movie network
-- Node types: movie, actor, director
-
-```python
-from torch_geometric.datasets import IMDB
-dataset = IMDB(root='/tmp/IMDB')
-```
-
-### DBLP
-**Usage**: Heterogeneous graph learning, node classification
-**Description**: DBLP bibliography network
-- Node types: author, paper, term, conference
-
-```python
-from torch_geometric.datasets import DBLP
-dataset = DBLP(root='/tmp/DBLP')
-```
-
-### LastFM
-**Usage**: Heterogeneous recommendation
-**Description**: LastFM music network
-- Node types: user, artist, tag
-
-```python
-from torch_geometric.datasets import LastFM
-dataset = LastFM(root='/tmp/LastFM')
-```
-
-## Temporal Graphs
-
-### BitcoinOTC
-**Usage**: Temporal link prediction, trust networks
-**Description**: Bitcoin OTC trust network over time
-
-```python
-from torch_geometric.datasets import BitcoinOTC
-dataset = BitcoinOTC(root='/tmp/BitcoinOTC')
-```
-
-### ICEWS18
-**Usage**: Temporal knowledge graph completion
-**Description**: Integrated Crisis Early Warning System events
-
-```python
-from torch_geometric.datasets import ICEWS18
-dataset = ICEWS18(root='/tmp/ICEWS18')
-```
-
-### GDELT
-**Usage**: Temporal event forecasting
-**Description**: Global Database of Events, Language, and Tone
-
-```python
-from torch_geometric.datasets import GDELT
-dataset = GDELT(root='/tmp/GDELT')
-```
-
-### JODIEDataset
-**Usage**: Dynamic graph learning
-**Datasets**: Reddit, Wikipedia, MOOC, LastFM
-**Description**: Temporal interaction networks
-
-```python
-from torch_geometric.datasets import JODIEDataset
-dataset = JODIEDataset(root='/tmp/JODIE', name='Reddit')
-```
-
-## 3D Meshes and Point Clouds
-
-### ShapeNet
-**Usage**: 3D shape classification and segmentation
-**Description**: Large-scale 3D CAD model dataset
-- 16,881 models across 16 categories
-- Part-level segmentation labels
-
-```python
-from torch_geometric.datasets import ShapeNet
-dataset = ShapeNet(root='/tmp/ShapeNet', categories=['Airplane'])
-```
-
-### ModelNet
-**Usage**: 3D shape classification
-**Versions**: ModelNet10, ModelNet40
-**Description**: CAD models for 3D object classification
-- ModelNet10: 4,899 models, 10 categories
-- ModelNet40: 12,311 models, 40 categories
-
-```python
-from torch_geometric.datasets import ModelNet
-dataset = ModelNet(root='/tmp/ModelNet', name='10')
-```
-
-### FAUST
-**Usage**: 3D shape matching, correspondence
-**Description**: Human body scans for shape analysis
-- 100 meshes of 10 people in 10 poses
-
-```python
-from torch_geometric.datasets import FAUST
-dataset = FAUST(root='/tmp/FAUST')
-```
-
-### CoMA
-**Usage**: 3D mesh deformation
-**Description**: Facial expression meshes
-- 20,466 3D face scans with expressions
-
-```python
-from torch_geometric.datasets import CoMA
-dataset = CoMA(root='/tmp/CoMA')
-```
-
-### S3DIS
-**Usage**: 3D semantic segmentation
-**Description**: Stanford Large-Scale 3D Indoor Spaces
-- 6 areas, 271 rooms, point cloud data
-
-```python
-from torch_geometric.datasets import S3DIS
-dataset = S3DIS(root='/tmp/S3DIS', test_area=6)
-```
-
-## Image and Vision Datasets
-
-### MNISTSuperpixels
-**Usage**: Graph-based image classification
-**Description**: MNIST images as superpixel graphs
-- 70,000 graphs (60k train, 10k test)
-
-```python
-from torch_geometric.datasets import MNISTSuperpixels
-dataset = MNISTSuperpixels(root='/tmp/MNIST')
-```
-
-### Flickr
-**Usage**: Image description, node classification
-**Description**: Flickr image network
-- 89,250 nodes, 899,756 edges
-
-```python
-from torch_geometric.datasets import Flickr
-dataset = Flickr(root='/tmp/Flickr')
-```
-
-### PPI
-**Usage**: Protein-protein interaction prediction
-**Description**: Multi-graph protein interaction networks
-- 24 graphs, 2,373 nodes total
-
-```python
-from torch_geometric.datasets import PPI
-dataset = PPI(root='/tmp/PPI', split='train')
-```
-
-## Small Classic Graphs
-
-### KarateClub
-**Usage**: Community detection, visualization
-**Description**: Zachary's karate club network
-- 34 nodes, 78 edges, 2 communities
-
-```python
-from torch_geometric.datasets import KarateClub
-dataset = KarateClub()
-```
-
-## Open Graph Benchmark (OGB)
-
-PyG integrates seamlessly with OGB datasets:
-
-### Node Property Prediction
-- **ogbn-products**: Amazon product network (2.4M nodes)
-- **ogbn-proteins**: Protein association network (132K nodes)
-- **ogbn-arxiv**: Citation network (169K nodes)
-- **ogbn-papers100M**: Large citation network (111M nodes)
-- **ogbn-mag**: Heterogeneous academic graph
-
-### Link Property Prediction
-- **ogbl-ppa**: Protein association networks
-- **ogbl-collab**: Collaboration networks
-- **ogbl-ddi**: Drug-drug interaction network
-- **ogbl-citation2**: Citation network
-- **ogbl-wikikg2**: Wikidata knowledge graph
-
-### Graph Property Prediction
-- **ogbg-molhiv**: Molecular HIV activity prediction
-- **ogbg-molpcba**: Molecular bioassays (multi-task)
-- **ogbg-ppa**: Protein function prediction
-- **ogbg-code2**: Code abstract syntax trees
-
-```python
-from torch_geometric.datasets import OGB_MAG, OGB_PPA
-# or
-from ogb.nodeproppred import PygNodePropPredDataset
-dataset = PygNodePropPredDataset(name='ogbn-arxiv')
-```
-
-## Synthetic Datasets
-
-### FakeDataset
-**Usage**: Testing, debugging
-**Description**: Generates random graph data
-
-```python
-from torch_geometric.datasets import FakeDataset
-dataset = FakeDataset(num_graphs=100, avg_num_nodes=50)
-```
-
-### StochasticBlockModelDataset
-**Usage**: Community detection benchmarks
-**Description**: Graphs generated from stochastic block models
-
-```python
-from torch_geometric.datasets import StochasticBlockModelDataset
-dataset = StochasticBlockModelDataset(root='/tmp/SBM', num_graphs=1000)
-```
-
-### ExplainerDataset
-**Usage**: Testing explainability methods
-**Description**: Synthetic graphs with known explanation ground truth
-
-```python
-from torch_geometric.datasets import ExplainerDataset
-dataset = ExplainerDataset(num_graphs=1000)
-```
-
-## Materials Science
-
-### QM8
-**Usage**: Molecular property prediction
-**Description**: Electronic properties of small molecules
-
-```python
-from torch_geometric.datasets import QM8
-dataset = QM8(root='/tmp/QM8')
-```
-
-## Biological Networks
-
-### PPI (Protein-Protein Interaction)
-Already listed above under Image and Vision Datasets
-
-### STRING
-**Usage**: Protein interaction networks
-**Description**: Known and predicted protein-protein interactions
-
-```python
-# Available through external sources or custom loading
-```
-
-## Usage Tips
-
-1. **Start with small datasets**: Use Cora, KarateClub, or ENZYMES for prototyping
-2. **Citation networks**: Planetoid datasets are perfect for node classification
-3. **Graph classification**: TUDataset provides diverse benchmarks
-4. **Molecular**: QM9, ZINC, MoleculeNet for chemistry applications
-5. **Large-scale**: Use Reddit, OGB datasets with NeighborLoader
-6. **Heterogeneous**: OGB_MAG, MovieLens, IMDB for multi-type graphs
-7. **Temporal**: JODIE, ICEWS for dynamic graph learning
-8. **3D**: ShapeNet, ModelNet, S3DIS for geometric learning
-
-## Common Patterns
-
-### Loading with Transforms
-```python
-from torch_geometric.datasets import Planetoid
-from torch_geometric.transforms import NormalizeFeatures
-
-dataset = Planetoid(root='/tmp/Cora', name='Cora',
- transform=NormalizeFeatures())
-```
-
-### Train/Val/Test Splits
-```python
-# For datasets with pre-defined splits
-data = dataset[0]
-train_data = data[data.train_mask]
-val_data = data[data.val_mask]
-test_data = data[data.test_mask]
-
-# For graph classification
-from torch_geometric.loader import DataLoader
-train_dataset = dataset[:int(len(dataset) * 0.8)]
-test_dataset = dataset[int(len(dataset) * 0.8):]
-train_loader = DataLoader(train_dataset, batch_size=32)
-```
-
-### Custom Data Loading
-```python
-from torch_geometric.data import Data, Dataset
-
-class MyCustomDataset(Dataset):
- def __init__(self, root, transform=None):
- super().__init__(root, transform)
- # Your initialization
-
- def len(self):
- return len(self.data_list)
-
- def get(self, idx):
- # Load and return data object
- return self.data_list[idx]
-```
diff --git a/scientific-skills/torch-geometric/references/explainability.md b/scientific-skills/torch-geometric/references/explainability.md
new file mode 100644
index 00000000..b090ba59
--- /dev/null
+++ b/scientific-skills/torch-geometric/references/explainability.md
@@ -0,0 +1,208 @@
+# GNN Explainability — Full Reference
+
+PyG provides `torch_geometric.explain` for interpreting GNN predictions. The module includes a unified `Explainer` interface, several explanation algorithms, visualization, and evaluation metrics.
+
+## The Explainer Interface
+
+The `Explainer` class is the central entry point. Configure it with:
+1. An explanation **algorithm** (GNNExplainer, PGExplainer, CaptumExplainer, etc.)
+2. An **explanation type** (`"model"` — explain model predictions, or `"phenomenon"` — explain dataset patterns)
+3. **Mask types** — which parts of the input to explain (nodes, edges, features)
+4. **Post-processing** — how to threshold masks (top-k, hard, etc.)
+
+```python
+from torch_geometric.explain import Explainer, GNNExplainer
+
+explainer = Explainer(
+ model=model,
+ algorithm=GNNExplainer(epochs=200),
+ explanation_type='model', # 'model' or 'phenomenon'
+ node_mask_type='attributes', # 'object', 'common_attributes', 'attributes', or None
+ edge_mask_type='object', # 'object' or None
+ model_config=dict(
+ mode='multiclass_classification', # 'binary_classification', 'multiclass_classification', 'regression'
+ task_level='node', # 'node', 'edge', 'graph'
+ return_type='log_probs', # 'log_probs', 'probs', 'raw'
+ ),
+)
+```
+
+**Mask types explained:**
+- `'object'`: One mask value per node/edge (which nodes/edges matter?)
+- `'attributes'`: One mask value per node feature dimension (which features matter?)
+- `'common_attributes'`: Same feature mask shared across all nodes
+- `None`: Don't generate this mask type
+
+## Generating Explanations
+
+### Node classification
+
+```python
+# Explain prediction for node at index 10
+explanation = explainer(data.x, data.edge_index, index=10)
+
+print(explanation.node_mask) # [num_nodes, num_features] — importance per feature per node
+print(explanation.edge_mask) # [num_edges] — importance per edge
+```
+
+### Graph classification
+
+```python
+explainer = Explainer(
+ model=model,
+ algorithm=GNNExplainer(epochs=200),
+ explanation_type='model',
+ edge_mask_type='object',
+ model_config=dict(
+ mode='multiclass_classification',
+ task_level='graph',
+ return_type='raw',
+ ),
+)
+
+explanation = explainer(data.x, data.edge_index)
+```
+
+## Visualization
+
+```python
+# Visualize which features are most important (bar chart)
+explanation.visualize_feature_importance(top_k=10)
+# Saves to 'feature_importance.png' by default, or pass path=
+
+# Visualize the important subgraph
+explanation.visualize_graph()
+# Saves to 'graph.png' by default, or pass path=
+```
+
+## Available Algorithms
+
+### GNNExplainer
+
+Learns soft masks via optimization. Works for node and graph-level tasks. The most widely used algorithm.
+
+```python
+from torch_geometric.explain import GNNExplainer
+
+algorithm = GNNExplainer(epochs=200, lr=0.01)
+```
+
+### PGExplainer
+
+A parametric (trained) explainer — learns a neural network that generates edge masks. Must be trained before use, but then generalizes to new graphs. Only supports edge masks (no node masks).
+
+```python
+from torch_geometric.explain import PGExplainer
+
+explainer = Explainer(
+ model=model,
+ algorithm=PGExplainer(epochs=30, lr=0.003),
+ explanation_type='phenomenon', # PGExplainer explains phenomena
+ edge_mask_type='object',
+ model_config=dict(
+ mode='regression',
+ task_level='graph',
+ return_type='raw',
+ ),
+ threshold_config=dict(threshold_type='topk', value=10),
+)
+
+# Train the explainer first
+for epoch in range(30):
+ for batch in loader:
+ loss = explainer.algorithm.train(
+ epoch, model, batch.x, batch.edge_index, target=batch.target
+ )
+
+# Then explain
+explanation = explainer(data.x, data.edge_index)
+```
+
+### CaptumExplainer
+
+Wraps the [Captum](https://captum.ai/) library, giving access to gradient-based attribution methods. Works with both homogeneous and heterogeneous graphs.
+
+```python
+from torch_geometric.explain import CaptumExplainer
+
+# Supports: 'IntegratedGradients', 'Saliency', 'Deconvolution',
+# 'ShapleyValueSampling', 'GuidedBackprop', etc.
+algorithm = CaptumExplainer('IntegratedGradients')
+```
+
+Requires `pip install captum` (or `uv add captum`).
+
+### AttentionExplainer
+
+Uses attention weights from attention-based GNNs (GATConv, TransformerConv) as edge explanations. No training needed — just reads existing attention scores.
+
+```python
+from torch_geometric.explain import AttentionExplainer
+
+algorithm = AttentionExplainer()
+```
+
+## Heterogeneous Graph Explanations
+
+For heterogeneous models, the explainer returns `HeteroExplanation` with per-type masks:
+
+```python
+from torch_geometric.explain import Explainer, CaptumExplainer
+
+explainer = Explainer(
+ model=hetero_model,
+ algorithm=CaptumExplainer('IntegratedGradients'),
+ explanation_type='model',
+ node_mask_type='attributes',
+ edge_mask_type='object',
+ model_config=dict(
+ mode='multiclass_classification',
+ task_level='node',
+ return_type='probs',
+ ),
+)
+
+hetero_explanation = explainer(
+ data.x_dict,
+ data.edge_index_dict,
+ index=torch.tensor([1, 3]),
+)
+
+# Access per-type masks
+hetero_explanation.node_mask_dict # {'paper': tensor, 'author': tensor, ...}
+hetero_explanation.edge_mask_dict # {('paper','cites','paper'): tensor, ...}
+```
+
+## Evaluation Metrics
+
+```python
+from torch_geometric.explain import unfaithfulness, fidelity, characterization_score
+
+# Unfaithfulness: how much does the explanation change the prediction?
+# Lower is better (0 = perfectly faithful)
+score = unfaithfulness(explainer, explanation)
+
+# Fidelity: measures explanation quality via positive/negative fidelity
+pos_fidelity, neg_fidelity = fidelity(explainer, explanation)
+
+# Characterization score: combined metric
+char_score = characterization_score(pos_fidelity, neg_fidelity)
+```
+
+## Post-Processing Masks
+
+Control how raw mask values are converted to final explanations:
+
+```python
+explainer = Explainer(
+ ...,
+ threshold_config=dict(
+ threshold_type='topk', # 'topk', 'hard', or None
+ value=10, # Top-10 edges for 'topk', threshold value for 'hard'
+ ),
+)
+```
+
+- `'topk'`: Keep only top-k highest-scored elements
+- `'hard'`: Binary threshold — elements above `value` are kept
+- `None`: Return raw continuous mask values
diff --git a/scientific-skills/torch-geometric/references/heterogeneous.md b/scientific-skills/torch-geometric/references/heterogeneous.md
new file mode 100644
index 00000000..b8525371
--- /dev/null
+++ b/scientific-skills/torch-geometric/references/heterogeneous.md
@@ -0,0 +1,241 @@
+# Heterogeneous Graph Learning — Full Reference
+
+## Creating HeteroData
+
+```python
+from torch_geometric.data import HeteroData
+
+data = HeteroData()
+
+# Node features — keyed by node type string
+data['paper'].x = ... # [num_papers, num_features_paper]
+data['author'].x = ... # [num_authors, num_features_author]
+data['institution'].x = ... # [num_institutions, num_features_institution]
+
+# Edge indices — keyed by (source_type, edge_type, dest_type) triplet
+data['paper', 'cites', 'paper'].edge_index = ... # [2, num_edges]
+data['author', 'writes', 'paper'].edge_index = ... # [2, num_edges]
+data['author', 'affiliated_with', 'institution'].edge_index = ... # [2, num_edges]
+
+# Edge features (optional)
+data['paper', 'cites', 'paper'].edge_attr = ... # [num_edges, num_edge_features]
+
+# Additional node attributes
+data['paper'].y = ... # labels
+data['paper'].train_mask = ... # boolean mask
+```
+
+### Accessing data
+
+```python
+# Single store access
+data['paper'] # NodeStore for papers
+data['paper', 'cites', 'paper'] # EdgeStore for cites edges
+data['paper', 'paper'] # Shorthand if edge type is unambiguous
+data['cites'] # Shorthand if edge type name is unique
+
+# Dict access for model input
+data.x_dict # {'paper': tensor, 'author': tensor, ...}
+data.edge_index_dict # {('paper','cites','paper'): tensor, ...}
+data.edge_attr_dict
+
+# Metadata
+node_types, edge_types = data.metadata()
+
+# Modify
+data['paper'].year = ... # Add new attribute
+del data['field_of_study'] # Delete node type
+del data['has_topic'] # Delete edge type
+
+# Convert
+data.to('cuda:0') # Transfer to GPU
+data.to_homogeneous() # Convert to typed homogeneous graph
+```
+
+### Transforms on HeteroData
+
+```python
+import torch_geometric.transforms as T
+
+data = T.ToUndirected()(data) # Add reverse edge types
+data = T.AddSelfLoops()(data) # Add self-loops for same-type edges
+data = T.NormalizeFeatures()(data) # Normalize features across all types
+```
+
+`ToUndirected()` is important — it creates reverse edge types (e.g., `('paper', 'rev_writes', 'author')`) so messages flow in both directions.
+
+## Building Heterogeneous GNN Models
+
+### Option 1: Auto-convert with `to_hetero()`
+
+Write a standard homogeneous GNN, then convert:
+
+```python
+from torch_geometric.nn import SAGEConv, to_hetero
+import torch_geometric.transforms as T
+from torch_geometric.datasets import OGB_MAG
+
+dataset = OGB_MAG(root='./data', preprocess='metapath2vec', transform=T.ToUndirected())
+data = dataset[0]
+
+class GNN(torch.nn.Module):
+ def __init__(self, hidden_channels, out_channels):
+ super().__init__()
+ # Use (-1, -1) for lazy init with bipartite support
+ self.conv1 = SAGEConv((-1, -1), hidden_channels)
+ self.conv2 = SAGEConv((-1, -1), out_channels)
+
+ def forward(self, x, edge_index):
+ x = self.conv1(x, edge_index).relu()
+ x = self.conv2(x, edge_index)
+ return x
+
+model = GNN(64, dataset.num_classes)
+model = to_hetero(model, data.metadata(), aggr='sum')
+
+# Initialize lazy modules
+with torch.no_grad():
+ out = model(data.x_dict, data.edge_index_dict)
+```
+
+With skip-connections (important for attention-based models):
+
+```python
+from torch_geometric.nn import GATConv, Linear, to_hetero
+
+class GAT(torch.nn.Module):
+ def __init__(self, hidden_channels, out_channels):
+ super().__init__()
+ self.conv1 = GATConv((-1, -1), hidden_channels, add_self_loops=False)
+ self.lin1 = Linear(-1, hidden_channels)
+ self.conv2 = GATConv((-1, -1), out_channels, add_self_loops=False)
+ self.lin2 = Linear(-1, out_channels)
+
+ def forward(self, x, edge_index):
+ # Skip connection replaces self-loops for bipartite message passing
+ x = self.conv1(x, edge_index) + self.lin1(x)
+ x = x.relu()
+ x = self.conv2(x, edge_index) + self.lin2(x)
+ return x
+
+model = GAT(64, dataset.num_classes)
+model = to_hetero(model, data.metadata(), aggr='sum')
+```
+
+### Option 2: HeteroConv wrapper (different conv per edge type)
+
+```python
+from torch_geometric.nn import HeteroConv, GCNConv, SAGEConv, GATConv, Linear
+
+class HeteroGNN(torch.nn.Module):
+ def __init__(self, hidden_channels, out_channels, num_layers):
+ super().__init__()
+
+ self.convs = torch.nn.ModuleList()
+ for _ in range(num_layers):
+ conv = HeteroConv({
+ ('paper', 'cites', 'paper'): GCNConv(-1, hidden_channels),
+ ('author', 'writes', 'paper'): SAGEConv((-1, -1), hidden_channels),
+ ('paper', 'rev_writes', 'author'): GATConv((-1, -1), hidden_channels,
+ add_self_loops=False),
+ }, aggr='sum')
+ self.convs.append(conv)
+
+ self.lin = Linear(hidden_channels, out_channels)
+
+ def forward(self, x_dict, edge_index_dict):
+ for conv in self.convs:
+ x_dict = conv(x_dict, edge_index_dict)
+ x_dict = {key: x.relu() for key, x in x_dict.items()}
+ return self.lin(x_dict['paper'])
+
+model = HeteroGNN(64, dataset.num_classes, num_layers=2)
+with torch.no_grad():
+ out = model(data.x_dict, data.edge_index_dict)
+```
+
+### Option 3: HGTConv (native heterogeneous operator)
+
+```python
+from torch_geometric.nn import HGTConv, Linear
+
+class HGT(torch.nn.Module):
+ def __init__(self, hidden_channels, out_channels, num_heads, num_layers):
+ super().__init__()
+
+ self.lin_dict = torch.nn.ModuleDict()
+ for node_type in data.node_types:
+ self.lin_dict[node_type] = Linear(-1, hidden_channels)
+
+ self.convs = torch.nn.ModuleList()
+ for _ in range(num_layers):
+ conv = HGTConv(hidden_channels, hidden_channels, data.metadata(),
+ num_heads, group='sum')
+ self.convs.append(conv)
+
+ self.lin = Linear(hidden_channels, out_channels)
+
+ def forward(self, x_dict, edge_index_dict):
+ for node_type, x in x_dict.items():
+ x_dict[node_type] = self.lin_dict[node_type](x).relu_()
+ for conv in self.convs:
+ x_dict = conv(x_dict, edge_index_dict)
+ return self.lin(x_dict['paper'])
+```
+
+## Training with HeteroData
+
+### Full-batch
+
+```python
+def train():
+ model.train()
+ optimizer.zero_grad()
+ out = model(data.x_dict, data.edge_index_dict)
+ mask = data['paper'].train_mask
+ loss = F.cross_entropy(out['paper'][mask], data['paper'].y[mask])
+ loss.backward()
+ optimizer.step()
+ return float(loss)
+```
+
+### Mini-batch with NeighborLoader
+
+```python
+from torch_geometric.loader import NeighborLoader
+
+train_loader = NeighborLoader(
+ data,
+ num_neighbors=[15] * 2, # per hop (applies to all edge types)
+ batch_size=128,
+ input_nodes=('paper', data['paper'].train_mask),
+)
+
+# Fine-grained neighbor control per edge type:
+# num_neighbors = {key: [15] * 2 for key in data.edge_types}
+
+def train():
+ model.train()
+ total_examples = total_loss = 0
+ for batch in train_loader:
+ optimizer.zero_grad()
+ batch = batch.to(device)
+ batch_size = batch['paper'].batch_size
+ out = model(batch.x_dict, batch.edge_index_dict)
+ loss = F.cross_entropy(out['paper'][:batch_size],
+ batch['paper'].y[:batch_size])
+ loss.backward()
+ optimizer.step()
+ total_examples += batch_size
+ total_loss += float(loss) * batch_size
+ return total_loss / total_examples
+```
+
+HGTLoader is also available for type-aware sampling:
+
+```python
+from torch_geometric.loader import HGTLoader
+
+loader = HGTLoader(data, num_samples=[512] * 2, batch_size=128,
+ input_nodes=('paper', data['paper'].train_mask))
+```
diff --git a/scientific-skills/torch-geometric/references/layers_reference.md b/scientific-skills/torch-geometric/references/layers_reference.md
deleted file mode 100644
index e4658945..00000000
--- a/scientific-skills/torch-geometric/references/layers_reference.md
+++ /dev/null
@@ -1,485 +0,0 @@
-# PyTorch Geometric Neural Network Layers Reference
-
-This document provides a comprehensive reference of all neural network layers available in `torch_geometric.nn`.
-
-## Layer Capability Flags
-
-When selecting layers, consider these capability flags:
-
-- **SparseTensor**: Supports `torch_sparse.SparseTensor` format for efficient sparse operations
-- **edge_weight**: Handles one-dimensional edge weight data
-- **edge_attr**: Processes multi-dimensional edge feature information
-- **Bipartite**: Works with bipartite graphs (different source/target node dimensions)
-- **Static**: Operates on static graphs with batched node features
-- **Lazy**: Enables initialization without specifying input channel dimensions
-
-## Convolutional Layers
-
-### Standard Graph Convolutions
-
-**GCNConv** - Graph Convolutional Network layer
-- Implements spectral graph convolution with symmetric normalization
-- Supports: SparseTensor, edge_weight, Bipartite, Lazy
-- Use for: Citation networks, social networks, general graph learning
-- Example: `GCNConv(in_channels, out_channels, improved=False, cached=True)`
-
-**SAGEConv** - GraphSAGE layer
-- Inductive learning via neighborhood sampling and aggregation
-- Supports: SparseTensor, Bipartite, Lazy
-- Use for: Large graphs, inductive learning, heterogeneous features
-- Example: `SAGEConv(in_channels, out_channels, aggr='mean')`
-
-**GATConv** - Graph Attention Network layer
-- Multi-head attention mechanism for adaptive neighbor weighting
-- Supports: SparseTensor, edge_attr, Bipartite, Static, Lazy
-- Use for: Tasks requiring variable neighbor importance
-- Example: `GATConv(in_channels, out_channels, heads=8, dropout=0.6)`
-
-**GraphConv** - Simple graph convolution (Morris et al.)
-- Basic message passing with optional edge weights
-- Supports: SparseTensor, edge_weight, Bipartite, Lazy
-- Use for: Baseline models, simple graph structures
-- Example: `GraphConv(in_channels, out_channels, aggr='add')`
-
-**GINConv** - Graph Isomorphism Network layer
-- Maximally powerful GNN for graph isomorphism testing
-- Supports: Bipartite
-- Use for: Graph classification, molecular property prediction
-- Example: `GINConv(nn.Sequential(nn.Linear(in_channels, out_channels), nn.ReLU()))`
-
-**TransformerConv** - Graph Transformer layer
-- Combines graph structure with transformer attention
-- Supports: SparseTensor, Bipartite, Lazy
-- Use for: Long-range dependencies, complex graphs
-- Example: `TransformerConv(in_channels, out_channels, heads=8, beta=True)`
-
-**ChebConv** - Chebyshev spectral graph convolution
-- Uses Chebyshev polynomials for efficient spectral filtering
-- Supports: SparseTensor, edge_weight, Bipartite, Lazy
-- Use for: Spectral graph learning, efficient convolutions
-- Example: `ChebConv(in_channels, out_channels, K=3)`
-
-**SGConv** - Simplified Graph Convolution
-- Pre-computes fixed number of propagation steps
-- Supports: SparseTensor, edge_weight, Bipartite, Lazy
-- Use for: Fast training, shallow models
-- Example: `SGConv(in_channels, out_channels, K=2)`
-
-**APPNP** - Approximate Personalized Propagation of Neural Predictions
-- Separates feature transformation from propagation
-- Supports: SparseTensor, edge_weight, Lazy
-- Use for: Deep propagation without oversmoothing
-- Example: `APPNP(K=10, alpha=0.1)`
-
-**ARMAConv** - ARMA graph convolution
-- Uses ARMA filters for graph filtering
-- Supports: SparseTensor, edge_weight, Bipartite, Lazy
-- Use for: Advanced spectral methods
-- Example: `ARMAConv(in_channels, out_channels, num_stacks=3, num_layers=2)`
-
-**GATv2Conv** - Improved Graph Attention Network
-- Fixes static attention computation issue in GAT
-- Supports: SparseTensor, edge_attr, Bipartite, Static, Lazy
-- Use for: Better attention learning than original GAT
-- Example: `GATv2Conv(in_channels, out_channels, heads=8)`
-
-**SuperGATConv** - Self-supervised Graph Attention
-- Adds self-supervised attention mechanism
-- Supports: SparseTensor, edge_attr, Bipartite, Static, Lazy
-- Use for: Self-supervised learning, limited labels
-- Example: `SuperGATConv(in_channels, out_channels, heads=8)`
-
-**GMMConv** - Gaussian Mixture Model Convolution
-- Uses Gaussian kernels in pseudo-coordinate space
-- Supports: Bipartite
-- Use for: Point clouds, spatial data
-- Example: `GMMConv(in_channels, out_channels, dim=3, kernel_size=5)`
-
-**SplineConv** - Spline-based convolution
-- B-spline basis functions for spatial filtering
-- Supports: Bipartite
-- Use for: Irregular grids, continuous spaces
-- Example: `SplineConv(in_channels, out_channels, dim=2, kernel_size=5)`
-
-**NNConv** - Neural Network Convolution
-- Edge features processed by neural networks
-- Supports: edge_attr, Bipartite
-- Use for: Rich edge features, molecular graphs
-- Example: `NNConv(in_channels, out_channels, nn=edge_nn, aggr='mean')`
-
-**CGConv** - Crystal Graph Convolution
-- Designed for crystalline materials
-- Supports: Bipartite
-- Use for: Materials science, crystal structures
-- Example: `CGConv(in_channels, dim=3, batch_norm=True)`
-
-**EdgeConv** - Edge Convolution (Dynamic Graph CNN)
-- Dynamically computes edges based on feature space
-- Supports: Static
-- Use for: Point clouds, dynamic graphs
-- Example: `EdgeConv(nn=edge_nn, aggr='max')`
-
-**PointNetConv** - PointNet++ convolution
-- Local and global feature learning for point clouds
-- Use for: 3D point cloud processing
-- Example: `PointNetConv(local_nn, global_nn)`
-
-**ResGatedGraphConv** - Residual Gated Graph Convolution
-- Gating mechanism with residual connections
-- Supports: edge_attr, Bipartite, Lazy
-- Use for: Deep GNNs, complex features
-- Example: `ResGatedGraphConv(in_channels, out_channels)`
-
-**GENConv** - Generalized Graph Convolution
-- Generalizes multiple GNN variants
-- Supports: SparseTensor, edge_weight, edge_attr, Bipartite, Lazy
-- Use for: Flexible architecture exploration
-- Example: `GENConv(in_channels, out_channels, aggr='softmax', num_layers=2)`
-
-**FiLMConv** - Feature-wise Linear Modulation
-- Conditions on global features
-- Supports: Bipartite, Lazy
-- Use for: Conditional generation, multi-task learning
-- Example: `FiLMConv(in_channels, out_channels, num_relations=5)`
-
-**PANConv** - Path Attention Network
-- Attention over multi-hop paths
-- Supports: SparseTensor, Lazy
-- Use for: Complex connectivity patterns
-- Example: `PANConv(in_channels, out_channels, filter_size=3)`
-
-**ClusterGCNConv** - Cluster-GCN convolution
-- Efficient training via graph clustering
-- Supports: edge_attr, Lazy
-- Use for: Very large graphs
-- Example: `ClusterGCNConv(in_channels, out_channels)`
-
-**MFConv** - Multi-scale Feature Convolution
-- Aggregates features at multiple scales
-- Supports: SparseTensor, Lazy
-- Use for: Multi-scale patterns
-- Example: `MFConv(in_channels, out_channels)`
-
-**RGCNConv** - Relational Graph Convolution
-- Handles multiple edge types
-- Supports: SparseTensor, edge_weight, Lazy
-- Use for: Knowledge graphs, heterogeneous graphs
-- Example: `RGCNConv(in_channels, out_channels, num_relations=10)`
-
-**FAConv** - Frequency Adaptive Convolution
-- Adaptive filtering in spectral domain
-- Supports: SparseTensor, Lazy
-- Use for: Spectral graph learning
-- Example: `FAConv(in_channels, eps=0.1, dropout=0.5)`
-
-### Molecular and 3D Convolutions
-
-**SchNet** - Continuous-filter convolutional layer
-- Designed for molecular dynamics
-- Use for: Molecular property prediction, 3D molecules
-- Example: `SchNet(hidden_channels=128, num_filters=64, num_interactions=6)`
-
-**DimeNet** - Directional Message Passing
-- Uses directional information and angles
-- Use for: 3D molecular structures, chemical properties
-- Example: `DimeNet(hidden_channels=128, out_channels=1, num_blocks=6)`
-
-**PointTransformerConv** - Point cloud transformer
-- Transformer for 3D point clouds
-- Use for: 3D vision, point cloud segmentation
-- Example: `PointTransformerConv(in_channels, out_channels)`
-
-### Hypergraph Convolutions
-
-**HypergraphConv** - Hypergraph convolution
-- Operates on hyperedges (edges connecting multiple nodes)
-- Supports: Lazy
-- Use for: Multi-way relationships, chemical reactions
-- Example: `HypergraphConv(in_channels, out_channels)`
-
-**HGTConv** - Heterogeneous Graph Transformer
-- Transformer for heterogeneous graphs with multiple types
-- Supports: Lazy
-- Use for: Heterogeneous networks, knowledge graphs
-- Example: `HGTConv(in_channels, out_channels, metadata, heads=8)`
-
-## Aggregation Operators
-
-**Aggr** - Base aggregation class
-- Flexible aggregation across nodes
-
-**SumAggregation** - Sum aggregation
-- Example: `SumAggregation()`
-
-**MeanAggregation** - Mean aggregation
-- Example: `MeanAggregation()`
-
-**MaxAggregation** - Max aggregation
-- Example: `MaxAggregation()`
-
-**SoftmaxAggregation** - Softmax-weighted aggregation
-- Learnable attention weights
-- Example: `SoftmaxAggregation(learn=True)`
-
-**PowerMeanAggregation** - Power mean aggregation
-- Learnable power parameter
-- Example: `PowerMeanAggregation(learn=True)`
-
-**LSTMAggregation** - LSTM-based aggregation
-- Sequential processing of neighbors
-- Example: `LSTMAggregation(in_channels, out_channels)`
-
-**SetTransformerAggregation** - Set Transformer aggregation
-- Transformer for permutation-invariant aggregation
-- Example: `SetTransformerAggregation(in_channels, out_channels)`
-
-**MultiAggregation** - Multiple aggregations
-- Combines multiple aggregation methods
-- Example: `MultiAggregation(['mean', 'max', 'std'])`
-
-## Pooling Layers
-
-### Global Pooling
-
-**global_mean_pool** - Global mean pooling
-- Averages node features per graph
-- Example: `global_mean_pool(x, batch)`
-
-**global_max_pool** - Global max pooling
-- Max over node features per graph
-- Example: `global_max_pool(x, batch)`
-
-**global_add_pool** - Global sum pooling
-- Sums node features per graph
-- Example: `global_add_pool(x, batch)`
-
-**global_sort_pool** - Global sort pooling
-- Sorts and concatenates top-k nodes
-- Example: `global_sort_pool(x, batch, k=30)`
-
-**GlobalAttention** - Global attention pooling
-- Learnable attention weights for aggregation
-- Example: `GlobalAttention(gate_nn)`
-
-**Set2Set** - Set2Set pooling
-- LSTM-based attention mechanism
-- Example: `Set2Set(in_channels, processing_steps=3)`
-
-### Hierarchical Pooling
-
-**TopKPooling** - Top-k pooling
-- Keeps top-k nodes based on projection scores
-- Example: `TopKPooling(in_channels, ratio=0.5)`
-
-**SAGPooling** - Self-Attention Graph Pooling
-- Uses self-attention for node selection
-- Example: `SAGPooling(in_channels, ratio=0.5)`
-
-**ASAPooling** - Adaptive Structure Aware Pooling
-- Structure-aware node selection
-- Example: `ASAPooling(in_channels, ratio=0.5)`
-
-**PANPooling** - Path Attention Pooling
-- Attention over paths for pooling
-- Example: `PANPooling(in_channels, ratio=0.5)`
-
-**EdgePooling** - Edge contraction pooling
-- Pools by contracting edges
-- Example: `EdgePooling(in_channels)`
-
-**MemPooling** - Memory-based pooling
-- Learnable cluster assignments
-- Example: `MemPooling(in_channels, out_channels, heads=4, num_clusters=10)`
-
-**avg_pool** / **max_pool** - Average/Max pool with clustering
-- Pools nodes within clusters
-- Example: `avg_pool(cluster, data)`
-
-## Normalization Layers
-
-**BatchNorm** - Batch normalization
-- Normalizes features across batch
-- Example: `BatchNorm(in_channels)`
-
-**LayerNorm** - Layer normalization
-- Normalizes features per sample
-- Example: `LayerNorm(in_channels)`
-
-**InstanceNorm** - Instance normalization
-- Normalizes per sample and graph
-- Example: `InstanceNorm(in_channels)`
-
-**GraphNorm** - Graph normalization
-- Graph-specific normalization
-- Example: `GraphNorm(in_channels)`
-
-**PairNorm** - Pair normalization
-- Prevents oversmoothing in deep GNNs
-- Example: `PairNorm(scale_individually=False)`
-
-**MessageNorm** - Message normalization
-- Normalizes messages during passing
-- Example: `MessageNorm(learn_scale=True)`
-
-**DiffGroupNorm** - Differentiable Group Normalization
-- Learnable grouping for normalization
-- Example: `DiffGroupNorm(in_channels, groups=10)`
-
-## Model Architectures
-
-### Pre-Built Models
-
-**GCN** - Complete Graph Convolutional Network
-- Multi-layer GCN with dropout
-- Example: `GCN(in_channels, hidden_channels, num_layers, out_channels)`
-
-**GraphSAGE** - Complete GraphSAGE model
-- Multi-layer SAGE with dropout
-- Example: `GraphSAGE(in_channels, hidden_channels, num_layers, out_channels)`
-
-**GIN** - Complete Graph Isomorphism Network
-- Multi-layer GIN for graph classification
-- Example: `GIN(in_channels, hidden_channels, num_layers, out_channels)`
-
-**GAT** - Complete Graph Attention Network
-- Multi-layer GAT with attention
-- Example: `GAT(in_channels, hidden_channels, num_layers, out_channels, heads=8)`
-
-**PNA** - Principal Neighbourhood Aggregation
-- Combines multiple aggregators and scalers
-- Example: `PNA(in_channels, hidden_channels, num_layers, out_channels)`
-
-**EdgeCNN** - Edge Convolution CNN
-- Dynamic graph CNN for point clouds
-- Example: `EdgeCNN(out_channels, num_layers=3, k=20)`
-
-### Auto-Encoders
-
-**GAE** - Graph Auto-Encoder
-- Encodes graphs into latent space
-- Example: `GAE(encoder)`
-
-**VGAE** - Variational Graph Auto-Encoder
-- Probabilistic graph encoding
-- Example: `VGAE(encoder)`
-
-**ARGA** - Adversarially Regularized Graph Auto-Encoder
-- GAE with adversarial regularization
-- Example: `ARGA(encoder, discriminator)`
-
-**ARGVA** - Adversarially Regularized Variational Graph Auto-Encoder
-- VGAE with adversarial regularization
-- Example: `ARGVA(encoder, discriminator)`
-
-### Knowledge Graph Embeddings
-
-**TransE** - Translating embeddings
-- Learns entity and relation embeddings
-- Example: `TransE(num_nodes, num_relations, hidden_channels)`
-
-**RotatE** - Rotational embeddings
-- Embeddings in complex space
-- Example: `RotatE(num_nodes, num_relations, hidden_channels)`
-
-**ComplEx** - Complex embeddings
-- Complex-valued embeddings
-- Example: `ComplEx(num_nodes, num_relations, hidden_channels)`
-
-**DistMult** - Bilinear diagonal model
-- Simplified bilinear model
-- Example: `DistMult(num_nodes, num_relations, hidden_channels)`
-
-## Utility Layers
-
-**Sequential** - Sequential container
-- Chains multiple layers
-- Example: `Sequential('x, edge_index', [(GCNConv(16, 64), 'x, edge_index -> x'), nn.ReLU()])`
-
-**JumpingKnowledge** - Jumping knowledge connections
-- Combines representations from all layers
-- Modes: 'cat', 'max', 'lstm'
-- Example: `JumpingKnowledge(mode='cat')`
-
-**DeepGCNLayer** - Deep GCN layer wrapper
-- Enables very deep GNNs with skip connections
-- Example: `DeepGCNLayer(conv, norm, act, block='res+', dropout=0.1)`
-
-**MLP** - Multi-layer perceptron
-- Standard feedforward network
-- Example: `MLP([in_channels, 64, 64, out_channels], dropout=0.5)`
-
-**Linear** - Lazy linear layer
-- Linear transformation with lazy initialization
-- Example: `Linear(in_channels, out_channels, bias=True)`
-
-## Dense Layers
-
-For dense (non-sparse) graph representations:
-
-**DenseGCNConv** - Dense GCN layer
-**DenseSAGEConv** - Dense SAGE layer
-**DenseGINConv** - Dense GIN layer
-**DenseGraphConv** - Dense graph convolution
-
-These are useful when working with small, fully-connected, or densely represented graphs.
-
-## Usage Tips
-
-1. **Start simple**: Begin with GCNConv or GATConv for most tasks
-2. **Consider data type**: Use molecular layers (SchNet, DimeNet) for 3D structures
-3. **Check capabilities**: Match layer capabilities to your data (edge features, bipartite, etc.)
-4. **Deep networks**: Use normalization (PairNorm, LayerNorm) and JumpingKnowledge for deep GNNs
-5. **Large graphs**: Use scalable layers (SAGE, Cluster-GCN) with neighbor sampling
-6. **Heterogeneous**: Use RGCNConv, HGTConv, or to_hetero() conversion
-7. **Lazy initialization**: Use lazy layers when input dimensions vary or are unknown
-
-## Common Patterns
-
-### Basic GNN
-```python
-from torch_geometric.nn import GCNConv, global_mean_pool
-
-class GNN(torch.nn.Module):
- def __init__(self, in_channels, hidden_channels, out_channels):
- super().__init__()
- self.conv1 = GCNConv(in_channels, hidden_channels)
- self.conv2 = GCNConv(hidden_channels, out_channels)
-
- def forward(self, x, edge_index, batch):
- x = self.conv1(x, edge_index).relu()
- x = self.conv2(x, edge_index)
- return global_mean_pool(x, batch)
-```
-
-### Deep GNN with Normalization
-```python
-class DeepGNN(torch.nn.Module):
- def __init__(self, in_channels, hidden_channels, num_layers, out_channels):
- super().__init__()
- self.convs = torch.nn.ModuleList()
- self.norms = torch.nn.ModuleList()
-
- self.convs.append(GCNConv(in_channels, hidden_channels))
- self.norms.append(LayerNorm(hidden_channels))
-
- for _ in range(num_layers - 2):
- self.convs.append(GCNConv(hidden_channels, hidden_channels))
- self.norms.append(LayerNorm(hidden_channels))
-
- self.convs.append(GCNConv(hidden_channels, out_channels))
- self.jk = JumpingKnowledge(mode='cat')
-
- def forward(self, x, edge_index, batch):
- xs = []
- for conv, norm in zip(self.convs[:-1], self.norms):
- x = conv(x, edge_index)
- x = norm(x)
- x = F.relu(x)
- xs.append(x)
-
- x = self.convs[-1](x, edge_index)
- xs.append(x)
-
- x = self.jk(xs)
- return global_mean_pool(x, batch)
-```
diff --git a/scientific-skills/torch-geometric/references/link_prediction.md b/scientific-skills/torch-geometric/references/link_prediction.md
new file mode 100644
index 00000000..1159f2ab
--- /dev/null
+++ b/scientific-skills/torch-geometric/references/link_prediction.md
@@ -0,0 +1,226 @@
+# Link Prediction — Full Reference
+
+Link prediction is the task of predicting missing or future edges in a graph. Common applications: social network friend suggestion, knowledge graph completion, drug-target interaction.
+
+## Edge Splitting
+
+Use `RandomLinkSplit` to split edges into train/val/test while maintaining graph structure:
+
+```python
+import torch_geometric.transforms as T
+
+transform = T.RandomLinkSplit(
+ num_val=0.1, # 10% of edges for validation
+ num_test=0.1, # 10% of edges for test
+ is_undirected=True, # Set True for undirected graphs
+ add_negative_train_samples=False, # Generate negatives on-the-fly during training
+ neg_sampling_ratio=1.0, # 1 negative per positive edge
+)
+train_data, val_data, test_data = transform(data)
+```
+
+After splitting, each split contains:
+- `edge_index`: message-passing edges (train edges only — no data leakage)
+- `edge_label_index`: supervision edges `[2, num_supervision_edges]` — the edges to predict
+- `edge_label`: binary labels — 1 for positive (real) edges, 0 for negative (fake) edges
+
+For the training split with `add_negative_train_samples=False`, only positive edges are in `edge_label_index` and negatives are sampled during training. Val/test splits always include both positive and negative edges.
+
+## Encoder-Decoder Pattern
+
+The standard approach:
+1. **Encode** — use a GNN to produce node embeddings from the message-passing edges
+2. **Decode** — score candidate edges using the node embeddings
+
+```python
+import torch
+import torch.nn.functional as F
+from torch_geometric.nn import GCNConv
+
+class LinkEncoder(torch.nn.Module):
+ def __init__(self, in_channels, hidden_channels, out_channels):
+ super().__init__()
+ self.conv1 = GCNConv(in_channels, hidden_channels)
+ self.conv2 = GCNConv(hidden_channels, out_channels)
+
+ def forward(self, x, edge_index):
+ x = self.conv1(x, edge_index).relu()
+ x = self.conv2(x, edge_index)
+ return x
+
+def decode(z, edge_label_index):
+ """Dot-product decoder: score = z_src . z_dst for each edge."""
+ src, dst = edge_label_index
+ return (z[src] * z[dst]).sum(dim=1)
+```
+
+## Full-Batch Training Loop
+
+```python
+from torch_geometric.utils import negative_sampling
+
+model = LinkEncoder(data.num_features, 128, 64)
+optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
+
+def train(train_data):
+ model.train()
+ optimizer.zero_grad()
+
+ # Encode using message-passing edges only
+ z = model(train_data.x, train_data.edge_index)
+
+ # Sample negative edges for this batch
+ neg_edge_index = negative_sampling(
+ edge_index=train_data.edge_index,
+ num_nodes=train_data.num_nodes,
+ num_neg_samples=train_data.edge_label_index.size(1),
+ )
+
+ # Combine positive and negative supervision edges
+ edge_label_index = torch.cat([train_data.edge_label_index, neg_edge_index], dim=1)
+ edge_label = torch.cat([
+ torch.ones(train_data.edge_label_index.size(1)),
+ torch.zeros(neg_edge_index.size(1)),
+ ])
+
+ # Decode and compute loss
+ pred = decode(z, edge_label_index)
+ loss = F.binary_cross_entropy_with_logits(pred, edge_label)
+ loss.backward()
+ optimizer.step()
+ return loss.item()
+
+def test(data_split):
+ model.eval()
+ z = model(data_split.x, data_split.edge_index)
+ pred = decode(z, data_split.edge_label_index).sigmoid()
+ # AUC is the standard metric for link prediction
+ from sklearn.metrics import roc_auc_score
+ return roc_auc_score(data_split.edge_label.cpu(), pred.cpu())
+```
+
+## Graph Autoencoders (GAE / VGAE)
+
+PyG provides `GAE` and `VGAE` for unsupervised link prediction:
+
+```python
+from torch_geometric.nn import GAE, VGAE, GCNConv
+
+class Encoder(torch.nn.Module):
+ def __init__(self, in_channels, out_channels):
+ super().__init__()
+ self.conv1 = GCNConv(in_channels, 2 * out_channels)
+ self.conv2 = GCNConv(2 * out_channels, out_channels)
+ # For VGAE, also define conv_mu and conv_logstd
+
+ def forward(self, x, edge_index):
+ x = self.conv1(x, edge_index).relu()
+ return self.conv2(x, edge_index)
+
+# GAE wraps your encoder and provides train/test methods
+model = GAE(Encoder(data.num_features, 64))
+optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
+
+def train():
+ model.train()
+ optimizer.zero_grad()
+ z = model.encode(train_data.x, train_data.edge_index)
+ loss = model.recon_loss(z, train_data.edge_label_index)
+ # For VGAE, add KL divergence:
+ # loss = loss + (1 / data.num_nodes) * model.kl_loss()
+ loss.backward()
+ optimizer.step()
+ return loss.item()
+
+def test(data_split):
+ model.eval()
+ z = model.encode(data_split.x, data_split.edge_index)
+ return model.test(z, data_split.edge_label_index[0], # positive edges
+ data_split.edge_label_index[1]) # negative edges
+```
+
+For VGAE, the encoder must return `mu` and `logstd` instead of a single embedding. Use the VGAE-specific encoder pattern:
+
+```python
+class VariationalEncoder(torch.nn.Module):
+ def __init__(self, in_channels, out_channels):
+ super().__init__()
+ self.conv1 = GCNConv(in_channels, 2 * out_channels)
+ self.conv_mu = GCNConv(2 * out_channels, out_channels)
+ self.conv_logstd = GCNConv(2 * out_channels, out_channels)
+
+ def forward(self, x, edge_index):
+ x = self.conv1(x, edge_index).relu()
+ return self.conv_mu(x, edge_index), self.conv_logstd(x, edge_index)
+
+model = VGAE(VariationalEncoder(data.num_features, 64))
+```
+
+## Mini-Batch Link Prediction with LinkNeighborLoader
+
+For large graphs, use `LinkNeighborLoader` — it samples subgraphs around supervision edges:
+
+```python
+from torch_geometric.loader import LinkNeighborLoader
+
+train_loader = LinkNeighborLoader(
+ data=train_data,
+ num_neighbors=[20, 10], # Sample neighbors per hop
+ edge_label_index=train_data.edge_label_index,
+ edge_label=train_data.edge_label,
+ batch_size=128, # Number of supervision edges per batch
+ neg_sampling_ratio=1.0, # 1 negative per positive
+ shuffle=True,
+)
+
+for batch in train_loader:
+ # batch.edge_label_index: supervision edges (pos + neg)
+ # batch.edge_label: 1 for positive, 0 for negative
+ # batch.edge_index: message-passing edges (from neighbor sampling)
+ z = model(batch.x, batch.edge_index)
+ pred = decode(z, batch.edge_label_index)
+ loss = F.binary_cross_entropy_with_logits(pred, batch.edge_label)
+```
+
+## Heterogeneous Link Prediction
+
+For heterogeneous graphs (e.g., user-item recommendation):
+
+```python
+transform = T.RandomLinkSplit(
+ num_val=0.1,
+ num_test=0.1,
+ neg_sampling_ratio=1.0,
+ add_negative_train_samples=False,
+ edge_types=('user', 'rates', 'movie'), # Which edge type to predict
+ rev_edge_types=('movie', 'rev_rates', 'user'), # Its reverse
+)
+train_data, val_data, test_data = transform(data)
+
+# Supervision edges are in:
+# train_data['user', 'rates', 'movie'].edge_label_index
+# train_data['user', 'rates', 'movie'].edge_label
+```
+
+## Evaluation Metrics
+
+- **AUC-ROC**: Standard metric — area under the ROC curve
+- **Average Precision (AP)**: Area under the precision-recall curve
+- **Hits@K**: Fraction of positive edges ranked in top K (used in knowledge graphs)
+- **MRR**: Mean reciprocal rank of positive edges
+
+```python
+from sklearn.metrics import roc_auc_score, average_precision_score
+
+auc = roc_auc_score(edge_label.cpu(), pred.cpu())
+ap = average_precision_score(edge_label.cpu(), pred.cpu())
+```
+
+## Common Pitfalls
+
+1. **Data leakage**: Never include val/test edges in the message-passing graph during training. `RandomLinkSplit` handles this correctly — `edge_index` in train_data only contains training edges.
+2. **Negative sampling quality**: Using random negatives is standard but can be too easy. For harder negatives, sample from 2-hop neighbors.
+3. **Undirected graphs**: Set `is_undirected=True` in `RandomLinkSplit` — otherwise it will treat each direction independently and leak information.
+4. **Decoding**: Dot-product is simplest but not always best. Consider MLP decoders or DistMult for heterogeneous/knowledge graphs.
diff --git a/scientific-skills/torch-geometric/references/message_passing.md b/scientific-skills/torch-geometric/references/message_passing.md
new file mode 100644
index 00000000..b4d48805
--- /dev/null
+++ b/scientific-skills/torch-geometric/references/message_passing.md
@@ -0,0 +1,121 @@
+# Custom Message Passing Layers
+
+Full reference for implementing custom GNN layers via the `MessagePassing` base class.
+
+## MessagePassing API
+
+```python
+MessagePassing(aggr="add", flow="source_to_target", node_dim=-2)
+```
+
+- `aggr`: Aggregation scheme — `"add"`, `"mean"`, or `"max"`
+- `flow`: Message direction — `"source_to_target"` (default) or `"target_to_source"`
+- `node_dim`: Axis along which to propagate
+
+### Methods to override
+
+- `message(...)`: Constructs messages for each edge. Access source/target node features via `_j`/`_i` suffixes.
+- `aggregate(inputs, index)`: Aggregates messages (usually handled by `aggr` parameter).
+- `update(aggr_out, ...)`: Post-aggregation transform on each node.
+- `propagate(edge_index, size=None, **kwargs)`: Orchestrates the full pipeline. Call this from `forward()`.
+
+Any tensor passed to `propagate()` can be auto-indexed in `message()` by appending `_i` (target) or `_j` (source). E.g., passing `x=features` lets you use `x_i` and `x_j` in the message function.
+
+For bipartite graphs, pass `size=(N, M)` to `propagate()` and provide features as tuples: `x=(x_src, x_dst)`.
+
+## Example: GCN Layer from Scratch
+
+```python
+import torch
+from torch.nn import Linear, Parameter
+from torch_geometric.nn import MessagePassing
+from torch_geometric.utils import add_self_loops, degree
+
+class GCNConv(MessagePassing):
+ def __init__(self, in_channels, out_channels):
+ super().__init__(aggr='add')
+ self.lin = Linear(in_channels, out_channels, bias=False)
+ self.bias = Parameter(torch.empty(out_channels))
+ self.reset_parameters()
+
+ def reset_parameters(self):
+ self.lin.reset_parameters()
+ self.bias.data.zero_()
+
+ def forward(self, x, edge_index):
+ # 1. Add self-loops
+ edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0))
+ # 2. Linear transform
+ x = self.lin(x)
+ # 3. Compute normalization coefficients
+ row, col = edge_index
+ deg = degree(col, x.size(0), dtype=x.dtype)
+ deg_inv_sqrt = deg.pow(-0.5)
+ deg_inv_sqrt[deg_inv_sqrt == float('inf')] = 0
+ norm = deg_inv_sqrt[row] * deg_inv_sqrt[col]
+ # 4-5. Message passing
+ out = self.propagate(edge_index, x=x, norm=norm)
+ # 6. Add bias
+ return out + self.bias
+
+ def message(self, x_j, norm):
+ # x_j: source node features for each edge [num_edges, out_channels]
+ # norm: normalization coefficients [num_edges]
+ return norm.view(-1, 1) * x_j
+```
+
+## Example: EdgeConv Layer
+
+```python
+import torch
+from torch.nn import Sequential as Seq, Linear, ReLU
+from torch_geometric.nn import MessagePassing
+
+class EdgeConv(MessagePassing):
+ def __init__(self, in_channels, out_channels):
+ super().__init__(aggr='max')
+ self.mlp = Seq(
+ Linear(2 * in_channels, out_channels),
+ ReLU(),
+ Linear(out_channels, out_channels),
+ )
+
+ def forward(self, x, edge_index):
+ return self.propagate(edge_index, x=x)
+
+ def message(self, x_i, x_j):
+ # x_i: target node features [num_edges, in_channels]
+ # x_j: source node features [num_edges, in_channels]
+ return self.mlp(torch.cat([x_i, x_j - x_i], dim=1))
+```
+
+## Example: Dynamic EdgeConv (recomputes graph each layer)
+
+```python
+from torch_geometric.nn import knn_graph
+
+class DynamicEdgeConv(EdgeConv):
+ def __init__(self, in_channels, out_channels, k=6):
+ super().__init__(in_channels, out_channels)
+ self.k = k
+
+ def forward(self, x, batch=None):
+ edge_index = knn_graph(x, self.k, batch, loop=False, flow=self.flow)
+ return super().forward(x, edge_index)
+```
+
+## Utility Functions
+
+```python
+from torch_geometric.utils import (
+ add_self_loops, # Add self-loop edges
+ remove_self_loops, # Remove self-loop edges
+ degree, # Compute node degrees
+ softmax, # Sparse softmax over neighborhoods
+ to_dense_adj, # Convert edge_index to dense adjacency matrix
+ to_undirected, # Make edge_index undirected
+ contains_self_loops, # Check for self-loops
+ is_undirected, # Check if graph is undirected
+ scatter, # Scatter operations (sum, mean, max)
+)
+```
diff --git a/scientific-skills/torch-geometric/references/scaling.md b/scientific-skills/torch-geometric/references/scaling.md
new file mode 100644
index 00000000..dc7f6fb9
--- /dev/null
+++ b/scientific-skills/torch-geometric/references/scaling.md
@@ -0,0 +1,265 @@
+# Scaling GNNs — Full Reference
+
+Techniques for training GNNs on large graphs that don't fit in GPU memory, multi-GPU training, and performance optimization.
+
+## Table of Contents
+1. Neighbor Sampling (NeighborLoader)
+2. Other Sampling Strategies
+3. Multi-GPU / Distributed Training
+4. torch.compile Support
+5. Performance Tips
+
+---
+
+## 1. Neighbor Sampling (NeighborLoader)
+
+The primary approach for large single-graph training. Recursively samples a fixed number of neighbors per hop, bounding the computation graph.
+
+```python
+from torch_geometric.loader import NeighborLoader
+
+loader = NeighborLoader(
+ data,
+ num_neighbors=[15, 10], # Max neighbors per hop (hop 1: 15, hop 2: 10)
+ batch_size=1024, # Number of seed nodes per batch
+ input_nodes=data.train_mask, # Which nodes to sample from
+ shuffle=True,
+ num_workers=4, # Parallel data loading
+ replace=False, # Sample without replacement
+)
+```
+
+**Key parameters:**
+- `num_neighbors`: List of max neighbors per hop. Length should match GNN depth. Use `-1` to sample all neighbors for a hop.
+- `input_nodes`: Seed nodes — can be a mask, tensor of indices, or `('node_type', mask)` tuple for hetero graphs.
+- `subgraph_type`: `"directional"` (default), `"bidirectional"` (add reverse edges), or `"induced"` (full induced subgraph).
+- `disjoint`: If `True`, don't fuse neighborhoods across seed nodes (uses more memory but can be needed).
+
+**Training pattern:**
+```python
+model = GraphSAGE(in_channels, hidden_channels, out_channels, num_layers=2)
+
+for batch in loader:
+ batch = batch.to(device)
+ out = model(batch.x, batch.edge_index)
+ # CRITICAL: only first batch_size nodes are seed nodes
+ loss = F.cross_entropy(out[:batch.batch_size], batch.y[:batch.batch_size])
+```
+
+**Important details:**
+- Nodes are sorted: first `batch.batch_size` nodes are the seed nodes
+- `batch.n_id` maps local indices back to original node IDs
+- Sampling >2-3 hops is generally infeasible (exponential neighborhood growth)
+- Keep `len(num_neighbors) == num_gnn_layers` for efficiency
+
+### LinkNeighborLoader (for link prediction)
+
+Samples subgraphs around supervision edges:
+
+```python
+from torch_geometric.loader import LinkNeighborLoader
+
+loader = LinkNeighborLoader(
+ data,
+ num_neighbors=[20, 10],
+ edge_label_index=train_data.edge_label_index,
+ edge_label=train_data.edge_label,
+ batch_size=256,
+ neg_sampling_ratio=1.0,
+ shuffle=True,
+)
+```
+
+### HGTLoader (type-aware heterogeneous sampling)
+
+Samples a fixed number of nodes per type per hop, following HGT paper:
+
+```python
+from torch_geometric.loader import HGTLoader
+
+loader = HGTLoader(
+ data,
+ num_samples=[512] * 2, # Nodes per type per hop
+ batch_size=128,
+ input_nodes=('paper', data['paper'].train_mask),
+)
+```
+
+## 2. Other Sampling Strategies
+
+### ClusterLoader (ClusterGCN)
+
+Partitions the graph into clusters, trains on full subgraphs. Better for deeper GNNs since messages flow freely within clusters:
+
+```python
+from torch_geometric.loader import ClusterData, ClusterLoader
+
+cluster_data = ClusterData(data, num_parts=1500)
+loader = ClusterLoader(cluster_data, batch_size=20, shuffle=True)
+
+for batch in loader:
+ # batch is a full subgraph — no slicing needed
+ out = model(batch.x, batch.edge_index)
+ loss = F.cross_entropy(out[batch.train_mask], batch.y[batch.train_mask])
+```
+
+### GraphSAINTSampler
+
+Samples subgraphs via random walks, nodes, or edges with importance-based normalization:
+
+```python
+from torch_geometric.loader import GraphSAINTRandomWalkSampler
+
+loader = GraphSAINTRandomWalkSampler(
+ data, batch_size=6000, walk_length=2, num_steps=5,
+)
+```
+
+### ShaDowKHopSampler
+
+Extracts K-hop induced subgraphs around seed nodes — decouples depth from scope:
+
+```python
+from torch_geometric.loader import ShaDowKHopSampler
+
+loader = ShaDowKHopSampler(
+ data, depth=2, num_neighbors=5, batch_size=64,
+ input_nodes=data.train_mask,
+)
+```
+
+## 3. Multi-GPU / Distributed Training
+
+### DistributedDataParallel (DDP)
+
+Standard PyTorch DDP works with PyG. Each GPU gets a partition of the seed nodes:
+
+```python
+import torch.distributed as dist
+from torch.nn.parallel import DistributedDataParallel
+import torch.multiprocessing as mp
+
+def run(rank, world_size, dataset):
+ # Initialize process group
+ os.environ['MASTER_ADDR'] = 'localhost'
+ os.environ['MASTER_PORT'] = '12345'
+ dist.init_process_group('nccl', rank=rank, world_size=world_size)
+
+ data = dataset[0]
+
+ # Split training nodes across GPUs
+ train_idx = data.train_mask.nonzero().view(-1)
+ train_idx = train_idx.split(train_idx.size(0) // world_size)[rank]
+
+ loader = NeighborLoader(
+ data,
+ input_nodes=train_idx,
+ num_neighbors=[25, 10],
+ batch_size=1024,
+ num_workers=4,
+ shuffle=True,
+ )
+
+ # Wrap model in DDP
+ model = GraphSAGE(...).to(rank)
+ model = DistributedDataParallel(model, device_ids=[rank])
+ optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
+
+ for epoch in range(10):
+ model.train()
+ for batch in loader:
+ batch = batch.to(rank)
+ optimizer.zero_grad()
+ out = model(batch.x, batch.edge_index)[:batch.batch_size]
+ loss = F.cross_entropy(out, batch.y[:batch.batch_size])
+ loss.backward()
+ optimizer.step()
+
+ # Synchronize before evaluation
+ dist.barrier()
+
+ if rank == 0:
+ # Evaluate on rank 0 only
+ ...
+
+ dist.destroy_process_group()
+
+# Launch
+if __name__ == '__main__':
+ dataset = Reddit('./data/Reddit')
+ world_size = torch.cuda.device_count()
+ mp.spawn(run, args=(world_size, dataset), nprocs=world_size, join=True)
+```
+
+**Key points:**
+- Initialize dataset before `mp.spawn()` — data auto-moves to shared memory
+- Each rank creates its own NeighborLoader with a subset of seed nodes
+- Call `dist.barrier()` to synchronize before evaluation
+- Evaluate on rank 0 only for simplicity
+- Clean up with `dist.destroy_process_group()`
+
+### PyTorch Lightning Integration
+
+PyG provides Lightning wrappers for minimal boilerplate:
+
+```python
+from torch_geometric.data import LightningNodeData
+
+datamodule = LightningNodeData(
+ data,
+ input_train_nodes=data.train_mask,
+ input_val_nodes=data.val_mask,
+ input_test_nodes=data.test_mask,
+ loader='neighbor',
+ num_neighbors=[25, 10],
+ batch_size=1024,
+)
+
+# Use with any Lightning Trainer
+trainer = L.Trainer(devices=4, accelerator='gpu', strategy='ddp')
+trainer.fit(model, datamodule)
+```
+
+Also available: `LightningLinkData` for link prediction, `LightningDataset` for graph-level tasks.
+
+## 4. torch.compile Support
+
+PyG supports `torch.compile` for faster execution:
+
+```python
+model = GCN(...)
+model = torch.compile(model)
+
+# Works with standard training loops
+out = model(data.x, data.edge_index)
+```
+
+**What works:**
+- Most GNN layers (GCNConv, SAGEConv, GATConv, etc.)
+- Standard training/inference pipelines
+- Both CPU and CUDA backends
+
+**Limitations:**
+- Dynamic shapes (varying graph sizes per batch) may trigger recompilation
+- Some specialized layers or custom MessagePassing subclasses may not compile
+- Use `torch.compile(model, dynamic=True)` if batch graph sizes vary significantly
+
+## 5. Performance Tips
+
+- **num_workers**: Set `num_workers=4` (or more) in data loaders for CPU-side parallelism
+- **pin_memory**: Use `pin_memory=True` in loaders for faster CPU-to-GPU transfer
+- **Sparse tensors**: Use `SparseTensor` from `torch_sparse` instead of `edge_index` for faster message passing on some layers
+- **Profiling**: Use `torch_geometric.profile` to measure time and memory of individual layers
+- **Mixed precision**: Standard PyTorch AMP works with PyG:
+ ```python
+ from torch.amp import autocast, GradScaler
+ scaler = GradScaler()
+ with autocast('cuda'):
+ out = model(batch.x, batch.edge_index)
+ loss = F.cross_entropy(out[:batch.batch_size], batch.y[:batch.batch_size])
+ scaler.scale(loss).backward()
+ scaler.step(optimizer)
+ scaler.update()
+ ```
+- **Reduce sampling**: Fewer neighbors per hop = faster but noisier. Start with `[15, 10]` for 2-layer GNNs.
+- **Avoid unnecessary computation**: With NeighborLoader, only the first `batch_size` outputs matter — don't compute metrics on sampled-only nodes.
diff --git a/scientific-skills/torch-geometric/references/transforms_reference.md b/scientific-skills/torch-geometric/references/transforms_reference.md
deleted file mode 100644
index 5149e120..00000000
--- a/scientific-skills/torch-geometric/references/transforms_reference.md
+++ /dev/null
@@ -1,679 +0,0 @@
-# PyTorch Geometric Transforms Reference
-
-This document provides a comprehensive reference of all transforms available in `torch_geometric.transforms`.
-
-## Overview
-
-Transforms modify `Data` or `HeteroData` objects before or during training. Apply them via:
-
-```python
-# During dataset loading
-dataset = MyDataset(root='/tmp', transform=MyTransform())
-
-# Apply to individual data
-transform = MyTransform()
-data = transform(data)
-
-# Compose multiple transforms
-from torch_geometric.transforms import Compose
-transform = Compose([Transform1(), Transform2(), Transform3()])
-```
-
-## General Transforms
-
-### NormalizeFeatures
-**Purpose**: Row-normalizes node features to sum to 1
-**Use case**: Feature scaling, probability-like features
-```python
-from torch_geometric.transforms import NormalizeFeatures
-transform = NormalizeFeatures()
-```
-
-### ToDevice
-**Purpose**: Transfers data to specified device (CPU/GPU)
-**Use case**: GPU training, device management
-```python
-from torch_geometric.transforms import ToDevice
-transform = ToDevice('cuda')
-```
-
-### RandomNodeSplit
-**Purpose**: Creates train/val/test node masks
-**Use case**: Node classification splits
-**Parameters**: `split='train_rest'`, `num_splits`, `num_val`, `num_test`
-```python
-from torch_geometric.transforms import RandomNodeSplit
-transform = RandomNodeSplit(num_val=0.1, num_test=0.2)
-```
-
-### RandomLinkSplit
-**Purpose**: Creates train/val/test edge splits
-**Use case**: Link prediction
-**Parameters**: `num_val`, `num_test`, `is_undirected`, `split_labels`
-```python
-from torch_geometric.transforms import RandomLinkSplit
-transform = RandomLinkSplit(num_val=0.1, num_test=0.2)
-```
-
-### IndexToMask
-**Purpose**: Converts indices to boolean masks
-**Use case**: Data preprocessing
-```python
-from torch_geometric.transforms import IndexToMask
-transform = IndexToMask()
-```
-
-### MaskToIndex
-**Purpose**: Converts boolean masks to indices
-**Use case**: Data preprocessing
-```python
-from torch_geometric.transforms import MaskToIndex
-transform = MaskToIndex()
-```
-
-### FixedPoints
-**Purpose**: Samples a fixed number of points
-**Use case**: Point cloud subsampling
-**Parameters**: `num`, `replace`, `allow_duplicates`
-```python
-from torch_geometric.transforms import FixedPoints
-transform = FixedPoints(1024)
-```
-
-### ToDense
-**Purpose**: Converts to dense adjacency matrices
-**Use case**: Small graphs, dense operations
-```python
-from torch_geometric.transforms import ToDense
-transform = ToDense(num_nodes=100)
-```
-
-### ToSparseTensor
-**Purpose**: Converts edge_index to SparseTensor
-**Use case**: Efficient sparse operations
-**Parameters**: `remove_edge_index`, `fill_cache`
-```python
-from torch_geometric.transforms import ToSparseTensor
-transform = ToSparseTensor()
-```
-
-## Graph Structure Transforms
-
-### ToUndirected
-**Purpose**: Converts directed graph to undirected
-**Use case**: Undirected graph algorithms
-**Parameters**: `reduce='add'` (how to handle duplicate edges)
-```python
-from torch_geometric.transforms import ToUndirected
-transform = ToUndirected()
-```
-
-### AddSelfLoops
-**Purpose**: Adds self-loops to all nodes
-**Use case**: GCN-style convolutions
-**Parameters**: `fill_value` (edge attribute for self-loops)
-```python
-from torch_geometric.transforms import AddSelfLoops
-transform = AddSelfLoops()
-```
-
-### RemoveSelfLoops
-**Purpose**: Removes all self-loops
-**Use case**: Cleaning graph structure
-```python
-from torch_geometric.transforms import RemoveSelfLoops
-transform = RemoveSelfLoops()
-```
-
-### RemoveIsolatedNodes
-**Purpose**: Removes nodes without edges
-**Use case**: Graph cleaning
-```python
-from torch_geometric.transforms import RemoveIsolatedNodes
-transform = RemoveIsolatedNodes()
-```
-
-### RemoveDuplicatedEdges
-**Purpose**: Removes duplicate edges
-**Use case**: Graph cleaning
-```python
-from torch_geometric.transforms import RemoveDuplicatedEdges
-transform = RemoveDuplicatedEdges()
-```
-
-### LargestConnectedComponents
-**Purpose**: Keeps only the largest connected component
-**Use case**: Focus on main graph structure
-**Parameters**: `num_components` (how many components to keep)
-```python
-from torch_geometric.transforms import LargestConnectedComponents
-transform = LargestConnectedComponents(num_components=1)
-```
-
-### KNNGraph
-**Purpose**: Creates edges based on k-nearest neighbors
-**Use case**: Point clouds, spatial data
-**Parameters**: `k`, `loop`, `force_undirected`, `flow`
-```python
-from torch_geometric.transforms import KNNGraph
-transform = KNNGraph(k=6)
-```
-
-### RadiusGraph
-**Purpose**: Creates edges within a radius
-**Use case**: Point clouds, spatial data
-**Parameters**: `r`, `loop`, `max_num_neighbors`, `flow`
-```python
-from torch_geometric.transforms import RadiusGraph
-transform = RadiusGraph(r=0.1)
-```
-
-### Delaunay
-**Purpose**: Computes Delaunay triangulation
-**Use case**: 2D/3D spatial graphs
-```python
-from torch_geometric.transforms import Delaunay
-transform = Delaunay()
-```
-
-### FaceToEdge
-**Purpose**: Converts mesh faces to edges
-**Use case**: Mesh processing
-```python
-from torch_geometric.transforms import FaceToEdge
-transform = FaceToEdge()
-```
-
-### LineGraph
-**Purpose**: Converts graph to its line graph
-**Use case**: Edge-centric analysis
-**Parameters**: `force_directed`
-```python
-from torch_geometric.transforms import LineGraph
-transform = LineGraph()
-```
-
-### GDC
-**Purpose**: Graph Diffusion Convolution preprocessing
-**Use case**: Improved message passing
-**Parameters**: `self_loop_weight`, `normalization_in`, `normalization_out`, `diffusion_kwargs`
-```python
-from torch_geometric.transforms import GDC
-transform = GDC(self_loop_weight=1, normalization_in='sym',
- diffusion_kwargs=dict(method='ppr', alpha=0.15))
-```
-
-### SIGN
-**Purpose**: Scalable Inception Graph Neural Networks preprocessing
-**Use case**: Efficient multi-scale features
-**Parameters**: `K` (number of hops)
-```python
-from torch_geometric.transforms import SIGN
-transform = SIGN(K=3)
-```
-
-## Feature Transforms
-
-### OneHotDegree
-**Purpose**: One-hot encodes node degree
-**Use case**: Degree as feature
-**Parameters**: `max_degree`, `cat` (concatenate with existing features)
-```python
-from torch_geometric.transforms import OneHotDegree
-transform = OneHotDegree(max_degree=100)
-```
-
-### LocalDegreeProfile
-**Purpose**: Appends local degree profile
-**Use case**: Structural node features
-```python
-from torch_geometric.transforms import LocalDegreeProfile
-transform = LocalDegreeProfile()
-```
-
-### Constant
-**Purpose**: Adds constant features to nodes
-**Use case**: Featureless graphs
-**Parameters**: `value`, `cat`
-```python
-from torch_geometric.transforms import Constant
-transform = Constant(value=1.0)
-```
-
-### TargetIndegree
-**Purpose**: Saves in-degree as target
-**Use case**: Degree prediction
-**Parameters**: `norm`, `max_value`
-```python
-from torch_geometric.transforms import TargetIndegree
-transform = TargetIndegree(norm=False)
-```
-
-### AddRandomWalkPE
-**Purpose**: Adds random walk positional encoding
-**Use case**: Positional information
-**Parameters**: `walk_length`, `attr_name`
-```python
-from torch_geometric.transforms import AddRandomWalkPE
-transform = AddRandomWalkPE(walk_length=20)
-```
-
-### AddLaplacianEigenvectorPE
-**Purpose**: Adds Laplacian eigenvector positional encoding
-**Use case**: Spectral positional information
-**Parameters**: `k` (number of eigenvectors), `attr_name`
-```python
-from torch_geometric.transforms import AddLaplacianEigenvectorPE
-transform = AddLaplacianEigenvectorPE(k=10)
-```
-
-### AddMetaPaths
-**Purpose**: Adds meta-path induced edges
-**Use case**: Heterogeneous graphs
-**Parameters**: `metapaths`, `drop_orig_edges`, `drop_unconnected_nodes`
-```python
-from torch_geometric.transforms import AddMetaPaths
-metapaths = [[('author', 'paper'), ('paper', 'author')]] # Co-authorship
-transform = AddMetaPaths(metapaths)
-```
-
-### SVDFeatureReduction
-**Purpose**: Reduces feature dimensionality via SVD
-**Use case**: Dimensionality reduction
-**Parameters**: `out_channels`
-```python
-from torch_geometric.transforms import SVDFeatureReduction
-transform = SVDFeatureReduction(out_channels=64)
-```
-
-## Vision/Spatial Transforms
-
-### Center
-**Purpose**: Centers node positions
-**Use case**: Point cloud preprocessing
-```python
-from torch_geometric.transforms import Center
-transform = Center()
-```
-
-### NormalizeScale
-**Purpose**: Normalizes positions to unit sphere
-**Use case**: Point cloud normalization
-```python
-from torch_geometric.transforms import NormalizeScale
-transform = NormalizeScale()
-```
-
-### NormalizeRotation
-**Purpose**: Rotates to principal components
-**Use case**: Rotation-invariant learning
-**Parameters**: `max_points`
-```python
-from torch_geometric.transforms import NormalizeRotation
-transform = NormalizeRotation()
-```
-
-### Distance
-**Purpose**: Saves Euclidean distance as edge attribute
-**Use case**: Spatial graphs
-**Parameters**: `norm`, `max_value`, `cat`
-```python
-from torch_geometric.transforms import Distance
-transform = Distance(norm=False, cat=False)
-```
-
-### Cartesian
-**Purpose**: Saves relative Cartesian coordinates as edge attributes
-**Use case**: Spatial relationships
-**Parameters**: `norm`, `max_value`, `cat`
-```python
-from torch_geometric.transforms import Cartesian
-transform = Cartesian(norm=False)
-```
-
-### Polar
-**Purpose**: Saves polar coordinates as edge attributes
-**Use case**: 2D spatial graphs
-**Parameters**: `norm`, `max_value`, `cat`
-```python
-from torch_geometric.transforms import Polar
-transform = Polar(norm=False)
-```
-
-### Spherical
-**Purpose**: Saves spherical coordinates as edge attributes
-**Use case**: 3D spatial graphs
-**Parameters**: `norm`, `max_value`, `cat`
-```python
-from torch_geometric.transforms import Spherical
-transform = Spherical(norm=False)
-```
-
-### LocalCartesian
-**Purpose**: Saves coordinates in local coordinate system
-**Use case**: Local spatial features
-**Parameters**: `norm`, `cat`
-```python
-from torch_geometric.transforms import LocalCartesian
-transform = LocalCartesian()
-```
-
-### PointPairFeatures
-**Purpose**: Computes point pair features
-**Use case**: 3D registration, correspondence
-**Parameters**: `cat`
-```python
-from torch_geometric.transforms import PointPairFeatures
-transform = PointPairFeatures()
-```
-
-## Data Augmentation
-
-### RandomJitter
-**Purpose**: Randomly jitters node positions
-**Use case**: Point cloud augmentation
-**Parameters**: `translate`, `scale`
-```python
-from torch_geometric.transforms import RandomJitter
-transform = RandomJitter(0.01)
-```
-
-### RandomFlip
-**Purpose**: Randomly flips positions along axis
-**Use case**: Geometric augmentation
-**Parameters**: `axis`, `p` (probability)
-```python
-from torch_geometric.transforms import RandomFlip
-transform = RandomFlip(axis=0, p=0.5)
-```
-
-### RandomScale
-**Purpose**: Randomly scales positions
-**Use case**: Scale augmentation
-**Parameters**: `scales` (min, max)
-```python
-from torch_geometric.transforms import RandomScale
-transform = RandomScale((0.9, 1.1))
-```
-
-### RandomRotate
-**Purpose**: Randomly rotates positions
-**Use case**: Rotation augmentation
-**Parameters**: `degrees` (range), `axis` (rotation axis)
-```python
-from torch_geometric.transforms import RandomRotate
-transform = RandomRotate(degrees=15, axis=2)
-```
-
-### RandomShear
-**Purpose**: Randomly shears positions
-**Use case**: Geometric augmentation
-**Parameters**: `shear` (range)
-```python
-from torch_geometric.transforms import RandomShear
-transform = RandomShear(0.1)
-```
-
-### RandomTranslate
-**Purpose**: Randomly translates positions
-**Use case**: Translation augmentation
-**Parameters**: `translate` (range)
-```python
-from torch_geometric.transforms import RandomTranslate
-transform = RandomTranslate(0.1)
-```
-
-### LinearTransformation
-**Purpose**: Applies linear transformation matrix
-**Use case**: Custom geometric transforms
-**Parameters**: `matrix`
-```python
-from torch_geometric.transforms import LinearTransformation
-import torch
-matrix = torch.eye(3)
-transform = LinearTransformation(matrix)
-```
-
-## Mesh Processing
-
-### SamplePoints
-**Purpose**: Samples points uniformly from mesh
-**Use case**: Mesh to point cloud conversion
-**Parameters**: `num`, `remove_faces`, `include_normals`
-```python
-from torch_geometric.transforms import SamplePoints
-transform = SamplePoints(num=1024)
-```
-
-### GenerateMeshNormals
-**Purpose**: Generates face/vertex normals
-**Use case**: Mesh processing
-```python
-from torch_geometric.transforms import GenerateMeshNormals
-transform = GenerateMeshNormals()
-```
-
-### FaceToEdge
-**Purpose**: Converts mesh faces to edges
-**Use case**: Mesh to graph conversion
-**Parameters**: `remove_faces`
-```python
-from torch_geometric.transforms import FaceToEdge
-transform = FaceToEdge()
-```
-
-## Sampling and Splitting
-
-### GridSampling
-**Purpose**: Clusters points in voxel grid
-**Use case**: Point cloud downsampling
-**Parameters**: `size` (voxel size), `start`, `end`
-```python
-from torch_geometric.transforms import GridSampling
-transform = GridSampling(size=0.1)
-```
-
-### FixedPoints
-**Purpose**: Samples fixed number of points
-**Use case**: Uniform point cloud size
-**Parameters**: `num`, `replace`, `allow_duplicates`
-```python
-from torch_geometric.transforms import FixedPoints
-transform = FixedPoints(num=2048, replace=False)
-```
-
-### RandomScale
-**Purpose**: Randomly scales by sampling from range
-**Use case**: Scale augmentation (already listed above)
-
-### VirtualNode
-**Purpose**: Adds a virtual node connected to all nodes
-**Use case**: Global information propagation
-```python
-from torch_geometric.transforms import VirtualNode
-transform = VirtualNode()
-```
-
-## Specialized Transforms
-
-### ToSLIC
-**Purpose**: Converts images to superpixel graphs (SLIC algorithm)
-**Use case**: Image as graph
-**Parameters**: `num_segments`, `compactness`, `add_seg`, `add_img`
-```python
-from torch_geometric.transforms import ToSLIC
-transform = ToSLIC(num_segments=75)
-```
-
-### GCNNorm
-**Purpose**: Applies GCN-style normalization to edges
-**Use case**: Preprocessing for GCN
-**Parameters**: `add_self_loops`
-```python
-from torch_geometric.transforms import GCNNorm
-transform = GCNNorm(add_self_loops=True)
-```
-
-### LaplacianLambdaMax
-**Purpose**: Computes largest Laplacian eigenvalue
-**Use case**: ChebConv preprocessing
-**Parameters**: `normalization`, `is_undirected`
-```python
-from torch_geometric.transforms import LaplacianLambdaMax
-transform = LaplacianLambdaMax(normalization='sym')
-```
-
-### NormalizeRotation
-**Purpose**: Rotates mesh/point cloud to align with principal axes
-**Use case**: Canonical orientation
-**Parameters**: `max_points`
-```python
-from torch_geometric.transforms import NormalizeRotation
-transform = NormalizeRotation()
-```
-
-## Compose and Apply
-
-### Compose
-**Purpose**: Chains multiple transforms
-**Use case**: Complex preprocessing pipelines
-```python
-from torch_geometric.transforms import Compose
-transform = Compose([
- Center(),
- NormalizeScale(),
- KNNGraph(k=6),
- Distance(norm=False),
-])
-```
-
-### BaseTransform
-**Purpose**: Base class for custom transforms
-**Use case**: Implementing custom transforms
-```python
-from torch_geometric.transforms import BaseTransform
-
-class MyTransform(BaseTransform):
- def __init__(self, param):
- self.param = param
-
- def __call__(self, data):
- # Modify data
- data.x = data.x * self.param
- return data
-```
-
-## Common Transform Combinations
-
-### Node Classification Preprocessing
-```python
-transform = Compose([
- NormalizeFeatures(),
- RandomNodeSplit(num_val=0.1, num_test=0.2),
-])
-```
-
-### Point Cloud Processing
-```python
-transform = Compose([
- Center(),
- NormalizeScale(),
- RandomRotate(degrees=15, axis=2),
- RandomJitter(0.01),
- KNNGraph(k=6),
- Distance(norm=False),
-])
-```
-
-### Mesh to Graph
-```python
-transform = Compose([
- FaceToEdge(remove_faces=True),
- GenerateMeshNormals(),
- Distance(norm=True),
-])
-```
-
-### Graph Structure Enhancement
-```python
-transform = Compose([
- ToUndirected(),
- AddSelfLoops(),
- RemoveIsolatedNodes(),
- GCNNorm(),
-])
-```
-
-### Heterogeneous Graph Preprocessing
-```python
-transform = Compose([
- AddMetaPaths(metapaths=[
- [('author', 'paper'), ('paper', 'author')],
- [('author', 'paper'), ('paper', 'conference'), ('conference', 'paper'), ('paper', 'author')]
- ]),
- RandomNodeSplit(split='train_rest', num_val=0.1, num_test=0.2),
-])
-```
-
-### Link Prediction
-```python
-transform = Compose([
- NormalizeFeatures(),
- RandomLinkSplit(num_val=0.1, num_test=0.2, is_undirected=True),
-])
-```
-
-## Usage Tips
-
-1. **Order matters**: Apply structural transforms before feature transforms
-2. **Caching**: Some transforms (like GDC) are expensive—apply once
-3. **Augmentation**: Use Random* transforms during training only
-4. **Compose sparingly**: Too many transforms slow down data loading
-5. **Custom transforms**: Inherit from `BaseTransform` for custom logic
-6. **Pre-transforms**: Apply expensive transforms once during dataset processing:
- ```python
- dataset = MyDataset(root='/tmp', pre_transform=ExpensiveTransform())
- ```
-7. **Dynamic transforms**: Apply cheap transforms during training:
- ```python
- dataset = MyDataset(root='/tmp', transform=CheapTransform())
- ```
-
-## Performance Considerations
-
-**Expensive transforms** (apply as pre_transform):
-- GDC
-- SIGN
-- KNNGraph (for large point clouds)
-- AddLaplacianEigenvectorPE
-- SVDFeatureReduction
-
-**Cheap transforms** (apply as transform):
-- NormalizeFeatures
-- ToUndirected
-- AddSelfLoops
-- Random* augmentations
-- ToDevice
-
-**Example**:
-```python
-from torch_geometric.datasets import Planetoid
-from torch_geometric.transforms import Compose, GDC, NormalizeFeatures
-
-# Expensive preprocessing done once
-pre_transform = GDC(
- self_loop_weight=1,
- normalization_in='sym',
- diffusion_kwargs=dict(method='ppr', alpha=0.15)
-)
-
-# Cheap transform applied each time
-transform = NormalizeFeatures()
-
-dataset = Planetoid(
- root='/tmp/Cora',
- name='Cora',
- pre_transform=pre_transform,
- transform=transform
-)
-```
diff --git a/scientific-skills/torch-geometric/scripts/benchmark_model.py b/scientific-skills/torch-geometric/scripts/benchmark_model.py
deleted file mode 100644
index cddf5651..00000000
--- a/scientific-skills/torch-geometric/scripts/benchmark_model.py
+++ /dev/null
@@ -1,309 +0,0 @@
-#!/usr/bin/env python3
-"""
-Benchmark GNN models on standard datasets.
-
-This script provides a simple way to benchmark different GNN architectures
-on common datasets and compare their performance.
-
-Usage:
- python benchmark_model.py --models gcn gat --dataset Cora
- python benchmark_model.py --models gcn --dataset Cora --epochs 200 --runs 10
-"""
-
-import argparse
-import torch
-import torch.nn.functional as F
-from torch_geometric.nn import GCNConv, GATConv, SAGEConv, GINConv
-from torch_geometric.datasets import Planetoid, TUDataset
-from torch_geometric.loader import DataLoader
-from torch_geometric.nn import global_mean_pool
-import time
-import numpy as np
-
-
-class GCN(torch.nn.Module):
- def __init__(self, num_features, hidden_channels, num_classes, dropout=0.5):
- super().__init__()
- self.conv1 = GCNConv(num_features, hidden_channels)
- self.conv2 = GCNConv(hidden_channels, num_classes)
- self.dropout = dropout
-
- def forward(self, x, edge_index, batch=None):
- x = self.conv1(x, edge_index)
- x = F.relu(x)
- x = F.dropout(x, p=self.dropout, training=self.training)
- x = self.conv2(x, edge_index)
- if batch is not None:
- x = global_mean_pool(x, batch)
- return F.log_softmax(x, dim=1)
-
-
-class GAT(torch.nn.Module):
- def __init__(self, num_features, hidden_channels, num_classes, heads=8, dropout=0.6):
- super().__init__()
- self.conv1 = GATConv(num_features, hidden_channels, heads=heads, dropout=dropout)
- self.conv2 = GATConv(hidden_channels * heads, num_classes, heads=1,
- concat=False, dropout=dropout)
- self.dropout = dropout
-
- def forward(self, x, edge_index, batch=None):
- x = F.dropout(x, p=self.dropout, training=self.training)
- x = F.elu(self.conv1(x, edge_index))
- x = F.dropout(x, p=self.dropout, training=self.training)
- x = self.conv2(x, edge_index)
- if batch is not None:
- x = global_mean_pool(x, batch)
- return F.log_softmax(x, dim=1)
-
-
-class GraphSAGE(torch.nn.Module):
- def __init__(self, num_features, hidden_channels, num_classes, dropout=0.5):
- super().__init__()
- self.conv1 = SAGEConv(num_features, hidden_channels)
- self.conv2 = SAGEConv(hidden_channels, num_classes)
- self.dropout = dropout
-
- def forward(self, x, edge_index, batch=None):
- x = self.conv1(x, edge_index)
- x = F.relu(x)
- x = F.dropout(x, p=self.dropout, training=self.training)
- x = self.conv2(x, edge_index)
- if batch is not None:
- x = global_mean_pool(x, batch)
- return F.log_softmax(x, dim=1)
-
-
-MODELS = {
- 'gcn': GCN,
- 'gat': GAT,
- 'graphsage': GraphSAGE,
-}
-
-
-def train_node_classification(model, data, optimizer):
- """Train for node classification."""
- model.train()
- optimizer.zero_grad()
- out = model(data.x, data.edge_index)
- loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask])
- loss.backward()
- optimizer.step()
- return loss.item()
-
-
-def test_node_classification(model, data):
- """Test for node classification."""
- model.eval()
- out = model(data.x, data.edge_index)
- pred = out.argmax(dim=1)
-
- accs = []
- for mask in [data.train_mask, data.val_mask, data.test_mask]:
- correct = (pred[mask] == data.y[mask]).sum()
- accs.append(float(correct) / int(mask.sum()))
-
- return accs
-
-
-def train_graph_classification(model, loader, optimizer, device):
- """Train for graph classification."""
- model.train()
- total_loss = 0
-
- for data in loader:
- data = data.to(device)
- optimizer.zero_grad()
- out = model(data.x, data.edge_index, data.batch)
- loss = F.nll_loss(out, data.y)
- loss.backward()
- optimizer.step()
- total_loss += loss.item() * data.num_graphs
-
- return total_loss / len(loader.dataset)
-
-
-def test_graph_classification(model, loader, device):
- """Test for graph classification."""
- model.eval()
- correct = 0
-
- for data in loader:
- data = data.to(device)
- out = model(data.x, data.edge_index, data.batch)
- pred = out.argmax(dim=1)
- correct += (pred == data.y).sum().item()
-
- return correct / len(loader.dataset)
-
-
-def benchmark_node_classification(model_name, dataset_name, epochs, lr, weight_decay, device):
- """Benchmark a model on node classification."""
- # Load dataset
- dataset = Planetoid(root=f'/tmp/{dataset_name}', name=dataset_name)
- data = dataset[0].to(device)
-
- # Create model
- model_class = MODELS[model_name]
- model = model_class(
- num_features=dataset.num_features,
- hidden_channels=64,
- num_classes=dataset.num_classes
- ).to(device)
-
- optimizer = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=weight_decay)
-
- # Training
- start_time = time.time()
- best_val_acc = 0
- best_test_acc = 0
-
- for epoch in range(1, epochs + 1):
- loss = train_node_classification(model, data, optimizer)
- train_acc, val_acc, test_acc = test_node_classification(model, data)
-
- if val_acc > best_val_acc:
- best_val_acc = val_acc
- best_test_acc = test_acc
-
- train_time = time.time() - start_time
-
- return {
- 'train_acc': train_acc,
- 'val_acc': best_val_acc,
- 'test_acc': best_test_acc,
- 'train_time': train_time,
- }
-
-
-def benchmark_graph_classification(model_name, dataset_name, epochs, lr, device):
- """Benchmark a model on graph classification."""
- # Load dataset
- dataset = TUDataset(root=f'/tmp/{dataset_name}', name=dataset_name)
-
- # Split dataset
- dataset = dataset.shuffle()
- train_dataset = dataset[:int(len(dataset) * 0.8)]
- test_dataset = dataset[int(len(dataset) * 0.8):]
-
- train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
- test_loader = DataLoader(test_dataset, batch_size=32)
-
- # Create model
- model_class = MODELS[model_name]
- model = model_class(
- num_features=dataset.num_features,
- hidden_channels=64,
- num_classes=dataset.num_classes
- ).to(device)
-
- optimizer = torch.optim.Adam(model.parameters(), lr=lr)
-
- # Training
- start_time = time.time()
-
- for epoch in range(1, epochs + 1):
- loss = train_graph_classification(model, train_loader, optimizer, device)
-
- # Final evaluation
- train_acc = test_graph_classification(model, train_loader, device)
- test_acc = test_graph_classification(model, test_loader, device)
- train_time = time.time() - start_time
-
- return {
- 'train_acc': train_acc,
- 'test_acc': test_acc,
- 'train_time': train_time,
- }
-
-
-def run_benchmark(args):
- """Run benchmark experiments."""
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
- print(f"Using device: {device}")
-
- # Determine task type
- if args.dataset in ['Cora', 'CiteSeer', 'PubMed']:
- task = 'node_classification'
- else:
- task = 'graph_classification'
-
- print(f"\\nDataset: {args.dataset}")
- print(f"Task: {task}")
- print(f"Models: {', '.join(args.models)}")
- print(f"Epochs: {args.epochs}")
- print(f"Runs: {args.runs}")
- print("=" * 60)
-
- results = {model: [] for model in args.models}
-
- # Run experiments
- for run in range(args.runs):
- print(f"\\nRun {run + 1}/{args.runs}")
- print("-" * 60)
-
- for model_name in args.models:
- if model_name not in MODELS:
- print(f"Unknown model: {model_name}")
- continue
-
- print(f" Training {model_name.upper()}...", end=" ")
-
- try:
- if task == 'node_classification':
- result = benchmark_node_classification(
- model_name, args.dataset, args.epochs,
- args.lr, args.weight_decay, device
- )
- print(f"Test Acc: {result['test_acc']:.4f}, "
- f"Time: {result['train_time']:.2f}s")
- else:
- result = benchmark_graph_classification(
- model_name, args.dataset, args.epochs, args.lr, device
- )
- print(f"Test Acc: {result['test_acc']:.4f}, "
- f"Time: {result['train_time']:.2f}s")
-
- results[model_name].append(result)
- except Exception as e:
- print(f"Error: {e}")
-
- # Print summary
- print("\\n" + "=" * 60)
- print("BENCHMARK RESULTS")
- print("=" * 60)
-
- for model_name in args.models:
- if not results[model_name]:
- continue
-
- test_accs = [r['test_acc'] for r in results[model_name]]
- times = [r['train_time'] for r in results[model_name]]
-
- print(f"\\n{model_name.upper()}")
- print(f" Test Accuracy: {np.mean(test_accs):.4f} ± {np.std(test_accs):.4f}")
- print(f" Training Time: {np.mean(times):.2f} ± {np.std(times):.2f}s")
-
-
-def main():
- parser = argparse.ArgumentParser(description="Benchmark GNN models")
- parser.add_argument('--models', nargs='+', default=['gcn'],
- help='Model types to benchmark (gcn, gat, graphsage)')
- parser.add_argument('--dataset', type=str, default='Cora',
- help='Dataset name (Cora, CiteSeer, PubMed, ENZYMES, PROTEINS)')
- parser.add_argument('--epochs', type=int, default=200,
- help='Number of training epochs')
- parser.add_argument('--runs', type=int, default=5,
- help='Number of runs to average over')
- parser.add_argument('--lr', type=float, default=0.01,
- help='Learning rate')
- parser.add_argument('--weight-decay', type=float, default=5e-4,
- help='Weight decay for node classification')
-
- args = parser.parse_args()
- run_benchmark(args)
-
-
-if __name__ == '__main__':
- main()
diff --git a/scientific-skills/torch-geometric/scripts/create_gnn_template.py b/scientific-skills/torch-geometric/scripts/create_gnn_template.py
deleted file mode 100644
index 3882b4df..00000000
--- a/scientific-skills/torch-geometric/scripts/create_gnn_template.py
+++ /dev/null
@@ -1,529 +0,0 @@
-#!/usr/bin/env python3
-"""
-Generate boilerplate code for common GNN architectures in PyTorch Geometric.
-
-This script creates ready-to-use GNN model templates with training loops,
-evaluation metrics, and proper data handling.
-
-Usage:
- python create_gnn_template.py --model gcn --task node_classification --output my_model.py
- python create_gnn_template.py --model gat --task graph_classification --output graph_classifier.py
-"""
-
-import argparse
-from pathlib import Path
-
-
-TEMPLATES = {
- 'node_classification': {
- 'gcn': '''import torch
-import torch.nn.functional as F
-from torch_geometric.nn import GCNConv
-from torch_geometric.datasets import Planetoid
-
-
-class GCN(torch.nn.Module):
- """Graph Convolutional Network for node classification."""
-
- def __init__(self, num_features, hidden_channels, num_classes, num_layers=2, dropout=0.5):
- super().__init__()
- self.convs = torch.nn.ModuleList()
-
- # First layer
- self.convs.append(GCNConv(num_features, hidden_channels))
-
- # Hidden layers
- for _ in range(num_layers - 2):
- self.convs.append(GCNConv(hidden_channels, hidden_channels))
-
- # Output layer
- self.convs.append(GCNConv(hidden_channels, num_classes))
-
- self.dropout = dropout
-
- def forward(self, data):
- x, edge_index = data.x, data.edge_index
-
- # Apply conv layers with ReLU and dropout
- for conv in self.convs[:-1]:
- x = conv(x, edge_index)
- x = F.relu(x)
- x = F.dropout(x, p=self.dropout, training=self.training)
-
- # Final layer without activation
- x = self.convs[-1](x, edge_index)
- return F.log_softmax(x, dim=1)
-
-
-def train(model, data, optimizer):
- """Train the model for one epoch."""
- model.train()
- optimizer.zero_grad()
- out = model(data)
- loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask])
- loss.backward()
- optimizer.step()
- return loss.item()
-
-
-def test(model, data):
- """Evaluate the model."""
- model.eval()
- out = model(data)
- pred = out.argmax(dim=1)
-
- accs = []
- for mask in [data.train_mask, data.val_mask, data.test_mask]:
- correct = (pred[mask] == data.y[mask]).sum()
- accs.append(int(correct) / int(mask.sum()))
-
- return accs
-
-
-def main():
- # Load dataset
- dataset = Planetoid(root='/tmp/Cora', name='Cora')
- data = dataset[0]
-
- # Create model
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
- model = GCN(
- num_features=dataset.num_features,
- hidden_channels=64,
- num_classes=dataset.num_classes,
- num_layers=3,
- dropout=0.5
- ).to(device)
- data = data.to(device)
-
- # Setup optimizer
- optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)
-
- # Training loop
- print("Training GCN model...")
- best_val_acc = 0
- for epoch in range(1, 201):
- loss = train(model, data, optimizer)
- train_acc, val_acc, test_acc = test(model, data)
-
- if val_acc > best_val_acc:
- best_val_acc = val_acc
- best_test_acc = test_acc
-
- if epoch % 10 == 0:
- print(f'Epoch {epoch:03d}, Loss: {loss:.4f}, '
- f'Train: {train_acc:.4f}, Val: {val_acc:.4f}, Test: {test_acc:.4f}')
-
- print(f'\\nBest Test Accuracy: {best_test_acc:.4f}')
-
-
-if __name__ == '__main__':
- main()
-''',
-
- 'gat': '''import torch
-import torch.nn.functional as F
-from torch_geometric.nn import GATConv
-from torch_geometric.datasets import Planetoid
-
-
-class GAT(torch.nn.Module):
- """Graph Attention Network for node classification."""
-
- def __init__(self, num_features, hidden_channels, num_classes, heads=8, dropout=0.6):
- super().__init__()
-
- self.conv1 = GATConv(num_features, hidden_channels, heads=heads, dropout=dropout)
- self.conv2 = GATConv(hidden_channels * heads, num_classes, heads=1,
- concat=False, dropout=dropout)
-
- self.dropout = dropout
-
- def forward(self, data):
- x, edge_index = data.x, data.edge_index
-
- x = F.dropout(x, p=self.dropout, training=self.training)
- x = F.elu(self.conv1(x, edge_index))
- x = F.dropout(x, p=self.dropout, training=self.training)
- x = self.conv2(x, edge_index)
-
- return F.log_softmax(x, dim=1)
-
-
-def train(model, data, optimizer):
- """Train the model for one epoch."""
- model.train()
- optimizer.zero_grad()
- out = model(data)
- loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask])
- loss.backward()
- optimizer.step()
- return loss.item()
-
-
-def test(model, data):
- """Evaluate the model."""
- model.eval()
- out = model(data)
- pred = out.argmax(dim=1)
-
- accs = []
- for mask in [data.train_mask, data.val_mask, data.test_mask]:
- correct = (pred[mask] == data.y[mask]).sum()
- accs.append(int(correct) / int(mask.sum()))
-
- return accs
-
-
-def main():
- # Load dataset
- dataset = Planetoid(root='/tmp/Cora', name='Cora')
- data = dataset[0]
-
- # Create model
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
- model = GAT(
- num_features=dataset.num_features,
- hidden_channels=8,
- num_classes=dataset.num_classes,
- heads=8,
- dropout=0.6
- ).to(device)
- data = data.to(device)
-
- # Setup optimizer
- optimizer = torch.optim.Adam(model.parameters(), lr=0.005, weight_decay=5e-4)
-
- # Training loop
- print("Training GAT model...")
- best_val_acc = 0
- for epoch in range(1, 201):
- loss = train(model, data, optimizer)
- train_acc, val_acc, test_acc = test(model, data)
-
- if val_acc > best_val_acc:
- best_val_acc = val_acc
- best_test_acc = test_acc
-
- if epoch % 10 == 0:
- print(f'Epoch {epoch:03d}, Loss: {loss:.4f}, '
- f'Train: {train_acc:.4f}, Val: {val_acc:.4f}, Test: {test_acc:.4f}')
-
- print(f'\\nBest Test Accuracy: {best_test_acc:.4f}')
-
-
-if __name__ == '__main__':
- main()
-''',
-
- 'graphsage': '''import torch
-import torch.nn.functional as F
-from torch_geometric.nn import SAGEConv
-from torch_geometric.datasets import Planetoid
-
-
-class GraphSAGE(torch.nn.Module):
- """GraphSAGE for node classification."""
-
- def __init__(self, num_features, hidden_channels, num_classes, num_layers=2, dropout=0.5):
- super().__init__()
- self.convs = torch.nn.ModuleList()
-
- self.convs.append(SAGEConv(num_features, hidden_channels))
- for _ in range(num_layers - 2):
- self.convs.append(SAGEConv(hidden_channels, hidden_channels))
- self.convs.append(SAGEConv(hidden_channels, num_classes))
-
- self.dropout = dropout
-
- def forward(self, data):
- x, edge_index = data.x, data.edge_index
-
- for conv in self.convs[:-1]:
- x = conv(x, edge_index)
- x = F.relu(x)
- x = F.dropout(x, p=self.dropout, training=self.training)
-
- x = self.convs[-1](x, edge_index)
- return F.log_softmax(x, dim=1)
-
-
-def train(model, data, optimizer):
- model.train()
- optimizer.zero_grad()
- out = model(data)
- loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask])
- loss.backward()
- optimizer.step()
- return loss.item()
-
-
-def test(model, data):
- model.eval()
- out = model(data)
- pred = out.argmax(dim=1)
-
- accs = []
- for mask in [data.train_mask, data.val_mask, data.test_mask]:
- correct = (pred[mask] == data.y[mask]).sum()
- accs.append(int(correct) / int(mask.sum()))
-
- return accs
-
-
-def main():
- dataset = Planetoid(root='/tmp/Cora', name='Cora')
- data = dataset[0]
-
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
- model = GraphSAGE(
- num_features=dataset.num_features,
- hidden_channels=64,
- num_classes=dataset.num_classes,
- num_layers=2,
- dropout=0.5
- ).to(device)
- data = data.to(device)
-
- optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)
-
- print("Training GraphSAGE model...")
- best_val_acc = 0
- for epoch in range(1, 201):
- loss = train(model, data, optimizer)
- train_acc, val_acc, test_acc = test(model, data)
-
- if val_acc > best_val_acc:
- best_val_acc = val_acc
- best_test_acc = test_acc
-
- if epoch % 10 == 0:
- print(f'Epoch {epoch:03d}, Loss: {loss:.4f}, '
- f'Train: {train_acc:.4f}, Val: {val_acc:.4f}, Test: {test_acc:.4f}')
-
- print(f'\\nBest Test Accuracy: {best_test_acc:.4f}')
-
-
-if __name__ == '__main__':
- main()
-''',
- },
-
- 'graph_classification': {
- 'gin': '''import torch
-import torch.nn.functional as F
-from torch_geometric.nn import GINConv, global_add_pool
-from torch_geometric.datasets import TUDataset
-from torch_geometric.loader import DataLoader
-
-
-class GIN(torch.nn.Module):
- """Graph Isomorphism Network for graph classification."""
-
- def __init__(self, num_features, hidden_channels, num_classes, num_layers=3, dropout=0.5):
- super().__init__()
-
- self.convs = torch.nn.ModuleList()
- self.batch_norms = torch.nn.ModuleList()
-
- # Create MLP for first layer
- nn = torch.nn.Sequential(
- torch.nn.Linear(num_features, hidden_channels),
- torch.nn.ReLU(),
- torch.nn.Linear(hidden_channels, hidden_channels)
- )
- self.convs.append(GINConv(nn))
- self.batch_norms.append(torch.nn.BatchNorm1d(hidden_channels))
-
- # Hidden layers
- for _ in range(num_layers - 2):
- nn = torch.nn.Sequential(
- torch.nn.Linear(hidden_channels, hidden_channels),
- torch.nn.ReLU(),
- torch.nn.Linear(hidden_channels, hidden_channels)
- )
- self.convs.append(GINConv(nn))
- self.batch_norms.append(torch.nn.BatchNorm1d(hidden_channels))
-
- # Output MLP
- self.lin = torch.nn.Linear(hidden_channels, num_classes)
- self.dropout = dropout
-
- def forward(self, data):
- x, edge_index, batch = data.x, data.edge_index, data.batch
-
- for conv, batch_norm in zip(self.convs, self.batch_norms):
- x = conv(x, edge_index)
- x = batch_norm(x)
- x = F.relu(x)
- x = F.dropout(x, p=self.dropout, training=self.training)
-
- # Global pooling
- x = global_add_pool(x, batch)
-
- # Output layer
- x = self.lin(x)
- return F.log_softmax(x, dim=1)
-
-
-def train(model, loader, optimizer, device):
- """Train the model for one epoch."""
- model.train()
- total_loss = 0
-
- for data in loader:
- data = data.to(device)
- optimizer.zero_grad()
- out = model(data)
- loss = F.nll_loss(out, data.y)
- loss.backward()
- optimizer.step()
- total_loss += loss.item() * data.num_graphs
-
- return total_loss / len(loader.dataset)
-
-
-def test(model, loader, device):
- """Evaluate the model."""
- model.eval()
- correct = 0
-
- for data in loader:
- data = data.to(device)
- out = model(data)
- pred = out.argmax(dim=1)
- correct += (pred == data.y).sum().item()
-
- return correct / len(loader.dataset)
-
-
-def main():
- # Load dataset
- dataset = TUDataset(root='/tmp/ENZYMES', name='ENZYMES')
- print(f"Dataset: {dataset}")
- print(f"Number of graphs: {len(dataset)}")
- print(f"Number of features: {dataset.num_features}")
- print(f"Number of classes: {dataset.num_classes}")
-
- # Shuffle and split
- dataset = dataset.shuffle()
- train_dataset = dataset[:int(len(dataset) * 0.8)]
- test_dataset = dataset[int(len(dataset) * 0.8):]
-
- # Create data loaders
- train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
- test_loader = DataLoader(test_dataset, batch_size=32)
-
- # Create model
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
- model = GIN(
- num_features=dataset.num_features,
- hidden_channels=64,
- num_classes=dataset.num_classes,
- num_layers=3,
- dropout=0.5
- ).to(device)
-
- optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
-
- # Training loop
- print("\\nTraining GIN model...")
- for epoch in range(1, 101):
- loss = train(model, train_loader, optimizer, device)
- train_acc = test(model, train_loader, device)
- test_acc = test(model, test_loader, device)
-
- if epoch % 10 == 0:
- print(f'Epoch {epoch:03d}, Loss: {loss:.4f}, '
- f'Train Acc: {train_acc:.4f}, Test Acc: {test_acc:.4f}')
-
-
-if __name__ == '__main__':
- main()
-''',
- },
-}
-
-
-def generate_template(model_type: str, task: str, output_path: str):
- """Generate a GNN template file."""
- if task not in TEMPLATES:
- raise ValueError(f"Unknown task: {task}. Available: {list(TEMPLATES.keys())}")
-
- if model_type not in TEMPLATES[task]:
- raise ValueError(f"Model {model_type} not available for task {task}. "
- f"Available: {list(TEMPLATES[task].keys())}")
-
- template = TEMPLATES[task][model_type]
-
- # Write to file
- output_file = Path(output_path)
- output_file.parent.mkdir(parents=True, exist_ok=True)
-
- with open(output_file, 'w') as f:
- f.write(template)
-
- print(f"✓ Generated {model_type.upper()} template for {task}")
- print(f" Saved to: {output_path}")
- print(f"\\nTo run the template:")
- print(f" python {output_path}")
-
-
-def list_templates():
- """List all available templates."""
- print("Available GNN Templates")
- print("=" * 50)
- for task, models in TEMPLATES.items():
- print(f"\\n{task.upper()}")
- print("-" * 50)
- for model in models.keys():
- print(f" - {model}")
- print()
-
-
-def main():
- parser = argparse.ArgumentParser(
- description="Generate GNN model templates",
- formatter_class=argparse.RawDescriptionHelpFormatter,
- epilog="""
-Examples:
- python create_gnn_template.py --model gcn --task node_classification --output gcn_model.py
- python create_gnn_template.py --model gin --task graph_classification --output gin_model.py
- python create_gnn_template.py --list
- """
- )
-
- parser.add_argument('--model', type=str,
- help='Model type (gcn, gat, graphsage, gin)')
- parser.add_argument('--task', type=str,
- help='Task type (node_classification, graph_classification)')
- parser.add_argument('--output', type=str, default='gnn_model.py',
- help='Output file path (default: gnn_model.py)')
- parser.add_argument('--list', action='store_true',
- help='List all available templates')
-
- args = parser.parse_args()
-
- if args.list:
- list_templates()
- return
-
- if not args.model or not args.task:
- parser.print_help()
- print("\\n" + "=" * 50)
- list_templates()
- return
-
- try:
- generate_template(args.model, args.task, args.output)
- except ValueError as e:
- print(f"Error: {e}")
- print("\\nUse --list to see available templates")
-
-
-if __name__ == '__main__':
- main()
diff --git a/scientific-skills/torch-geometric/scripts/visualize_graph.py b/scientific-skills/torch-geometric/scripts/visualize_graph.py
deleted file mode 100644
index 58b87836..00000000
--- a/scientific-skills/torch-geometric/scripts/visualize_graph.py
+++ /dev/null
@@ -1,313 +0,0 @@
-#!/usr/bin/env python3
-"""
-Visualize PyTorch Geometric graph structures using networkx and matplotlib.
-
-This script provides utilities to visualize Data objects, including:
-- Graph structure (nodes and edges)
-- Node features (as colors)
-- Edge attributes (as edge colors/widths)
-- Community/cluster assignments
-
-Usage:
- python visualize_graph.py --dataset Cora --output graph.png
-
-Or import and use:
- from scripts.visualize_graph import visualize_data
- visualize_data(data, title="My Graph", show_labels=True)
-"""
-
-import argparse
-import matplotlib.pyplot as plt
-import networkx as nx
-import torch
-from typing import Optional, Union
-import numpy as np
-
-
-def visualize_data(
- data,
- title: str = "Graph Visualization",
- node_color_attr: Optional[str] = None,
- edge_color_attr: Optional[str] = None,
- show_labels: bool = False,
- node_size: int = 300,
- figsize: tuple = (12, 10),
- layout: str = "spring",
- output_path: Optional[str] = None,
- max_nodes: Optional[int] = None,
-):
- """
- Visualize a PyTorch Geometric Data object.
-
- Args:
- data: PyTorch Geometric Data object
- title: Plot title
- node_color_attr: Data attribute to use for node colors (e.g., 'y', 'train_mask')
- edge_color_attr: Data attribute to use for edge colors
- show_labels: Whether to show node labels
- node_size: Size of nodes in visualization
- figsize: Figure size (width, height)
- layout: Graph layout algorithm ('spring', 'circular', 'kamada_kawai', 'spectral')
- output_path: Path to save figure (if None, displays interactively)
- max_nodes: Maximum number of nodes to visualize (samples if exceeded)
- """
- # Sample nodes if graph is too large
- if max_nodes and data.num_nodes > max_nodes:
- print(f"Graph has {data.num_nodes} nodes. Sampling {max_nodes} nodes for visualization.")
- node_indices = torch.randperm(data.num_nodes)[:max_nodes]
- data = data.subgraph(node_indices)
-
- # Convert to networkx graph
- G = nx.Graph() if is_undirected(data.edge_index) else nx.DiGraph()
-
- # Add nodes
- G.add_nodes_from(range(data.num_nodes))
-
- # Add edges
- edge_index = data.edge_index.cpu().numpy()
- edges = list(zip(edge_index[0], edge_index[1]))
- G.add_edges_from(edges)
-
- # Setup figure
- fig, ax = plt.subplots(figsize=figsize)
-
- # Choose layout
- if layout == "spring":
- pos = nx.spring_layout(G, k=0.5, iterations=50)
- elif layout == "circular":
- pos = nx.circular_layout(G)
- elif layout == "kamada_kawai":
- pos = nx.kamada_kawai_layout(G)
- elif layout == "spectral":
- pos = nx.spectral_layout(G)
- else:
- raise ValueError(f"Unknown layout: {layout}")
-
- # Determine node colors
- if node_color_attr and hasattr(data, node_color_attr):
- node_colors = getattr(data, node_color_attr).cpu().numpy()
- if node_colors.dtype == bool:
- node_colors = node_colors.astype(int)
- if len(node_colors.shape) > 1:
- # Multi-dimensional features - use first dimension
- node_colors = node_colors[:, 0]
- else:
- node_colors = 'skyblue'
-
- # Determine edge colors
- if edge_color_attr and hasattr(data, edge_color_attr):
- edge_colors = getattr(data, edge_color_attr).cpu().numpy()
- if len(edge_colors.shape) > 1:
- edge_colors = edge_colors[:, 0]
- else:
- edge_colors = 'gray'
-
- # Draw graph
- nx.draw_networkx_nodes(
- G, pos,
- node_color=node_colors,
- node_size=node_size,
- cmap=plt.cm.viridis,
- ax=ax
- )
-
- nx.draw_networkx_edges(
- G, pos,
- edge_color=edge_colors,
- alpha=0.3,
- arrows=isinstance(G, nx.DiGraph),
- arrowsize=10,
- ax=ax
- )
-
- if show_labels:
- nx.draw_networkx_labels(G, pos, font_size=8, ax=ax)
-
- ax.set_title(title, fontsize=16, fontweight='bold')
- ax.axis('off')
-
- # Add colorbar if using numeric node colors
- if node_color_attr and isinstance(node_colors, np.ndarray):
- sm = plt.cm.ScalarMappable(
- cmap=plt.cm.viridis,
- norm=plt.Normalize(vmin=node_colors.min(), vmax=node_colors.max())
- )
- sm.set_array([])
- cbar = plt.colorbar(sm, ax=ax, fraction=0.046, pad=0.04)
- cbar.set_label(node_color_attr, rotation=270, labelpad=20)
-
- plt.tight_layout()
-
- if output_path:
- plt.savefig(output_path, dpi=300, bbox_inches='tight')
- print(f"Figure saved to {output_path}")
- else:
- plt.show()
-
- plt.close()
-
-
-def is_undirected(edge_index):
- """Check if graph is undirected."""
- row, col = edge_index
- num_edges = edge_index.size(1)
-
- # Create a set of edges and reverse edges
- edges = set(zip(row.tolist(), col.tolist()))
- reverse_edges = set(zip(col.tolist(), row.tolist()))
-
- # Check if all edges have their reverse
- return edges == reverse_edges
-
-
-def plot_degree_distribution(data, output_path: Optional[str] = None):
- """Plot the degree distribution of the graph."""
- from torch_geometric.utils import degree
-
- row, col = data.edge_index
- deg = degree(col, data.num_nodes).cpu().numpy()
-
- fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 5))
-
- # Histogram
- ax1.hist(deg, bins=50, edgecolor='black', alpha=0.7)
- ax1.set_xlabel('Degree', fontsize=12)
- ax1.set_ylabel('Frequency', fontsize=12)
- ax1.set_title('Degree Distribution', fontsize=14, fontweight='bold')
- ax1.grid(alpha=0.3)
-
- # Log-log plot
- unique_degrees, counts = np.unique(deg, return_counts=True)
- ax2.loglog(unique_degrees, counts, 'o-', alpha=0.7)
- ax2.set_xlabel('Degree (log scale)', fontsize=12)
- ax2.set_ylabel('Frequency (log scale)', fontsize=12)
- ax2.set_title('Degree Distribution (Log-Log)', fontsize=14, fontweight='bold')
- ax2.grid(alpha=0.3)
-
- plt.tight_layout()
-
- if output_path:
- plt.savefig(output_path, dpi=300, bbox_inches='tight')
- print(f"Degree distribution saved to {output_path}")
- else:
- plt.show()
-
- plt.close()
-
-
-def plot_graph_statistics(data, output_path: Optional[str] = None):
- """Plot various graph statistics."""
- from torch_geometric.utils import degree, contains_self_loops, is_undirected as check_undirected
-
- # Compute statistics
- row, col = data.edge_index
- deg = degree(col, data.num_nodes).cpu().numpy()
-
- stats = {
- 'Nodes': data.num_nodes,
- 'Edges': data.num_edges,
- 'Avg Degree': deg.mean(),
- 'Max Degree': deg.max(),
- 'Self-loops': contains_self_loops(data.edge_index),
- 'Undirected': check_undirected(data.edge_index),
- }
-
- if hasattr(data, 'num_node_features'):
- stats['Node Features'] = data.num_node_features
- if hasattr(data, 'num_edge_features') and data.edge_attr is not None:
- stats['Edge Features'] = data.num_edge_features
- if hasattr(data, 'y'):
- if data.y.dim() == 1:
- stats['Classes'] = int(data.y.max().item()) + 1
-
- # Create text plot
- fig, ax = plt.subplots(figsize=(8, 6))
- ax.axis('off')
-
- text = "Graph Statistics\n" + "=" * 40 + "\n\n"
- for key, value in stats.items():
- text += f"{key:20s}: {value}\n"
-
- ax.text(0.1, 0.5, text, fontsize=14, family='monospace',
- verticalalignment='center', transform=ax.transAxes)
-
- plt.tight_layout()
-
- if output_path:
- plt.savefig(output_path, dpi=300, bbox_inches='tight')
- print(f"Statistics saved to {output_path}")
- else:
- plt.show()
-
- plt.close()
-
- # Print to console as well
- print("\n" + text)
-
-
-def main():
- parser = argparse.ArgumentParser(description="Visualize PyTorch Geometric graphs")
- parser.add_argument('--dataset', type=str, default='Cora',
- help='Dataset name (e.g., Cora, CiteSeer, ENZYMES)')
- parser.add_argument('--output', type=str, default=None,
- help='Output file path for visualization')
- parser.add_argument('--node-color', type=str, default='y',
- help='Attribute to use for node colors')
- parser.add_argument('--layout', type=str, default='spring',
- choices=['spring', 'circular', 'kamada_kawai', 'spectral'],
- help='Graph layout algorithm')
- parser.add_argument('--show-labels', action='store_true',
- help='Show node labels')
- parser.add_argument('--max-nodes', type=int, default=500,
- help='Maximum nodes to visualize')
- parser.add_argument('--stats', action='store_true',
- help='Show graph statistics')
- parser.add_argument('--degree', action='store_true',
- help='Show degree distribution')
-
- args = parser.parse_args()
-
- # Load dataset
- print(f"Loading dataset: {args.dataset}")
-
- try:
- # Try Planetoid datasets
- from torch_geometric.datasets import Planetoid
- dataset = Planetoid(root=f'/tmp/{args.dataset}', name=args.dataset)
- data = dataset[0]
- except:
- try:
- # Try TUDataset
- from torch_geometric.datasets import TUDataset
- dataset = TUDataset(root=f'/tmp/{args.dataset}', name=args.dataset)
- data = dataset[0]
- except Exception as e:
- print(f"Error loading dataset: {e}")
- print("Supported datasets: Cora, CiteSeer, PubMed, ENZYMES, PROTEINS, etc.")
- return
-
- print(f"Loaded {args.dataset}: {data.num_nodes} nodes, {data.num_edges} edges")
-
- # Generate visualizations
- if args.stats:
- stats_output = args.output.replace('.png', '_stats.png') if args.output else None
- plot_graph_statistics(data, stats_output)
-
- if args.degree:
- degree_output = args.output.replace('.png', '_degree.png') if args.output else None
- plot_degree_distribution(data, degree_output)
-
- # Main visualization
- visualize_data(
- data,
- title=f"{args.dataset} Graph",
- node_color_attr=args.node_color,
- show_labels=args.show_labels,
- layout=args.layout,
- output_path=args.output,
- max_nodes=args.max_nodes
- )
-
-
-if __name__ == '__main__':
- main()