What HypergraphConv does
HypergraphConv performs two-phase message passing through hyperedges:
- Node to hyperedge: Aggregate features from all nodes in each hyperedge to form a hyperedge representation
- Hyperedge to node: Aggregate features from all hyperedges containing each node to update node representations
This two-phase process lets information flow through group interactions: all co-authors of a paper share information simultaneously, not just through pairwise author-author edges.
The math (simplified)
# Phase 1: Node -> Hyperedge
h_e = (1/|e|) · Σ_{v in e} W · h_v # average node features in hyperedge e
# Phase 2: Hyperedge -> Node
h_v' = (1/|E(v)|) · Σ_{e: v in e} h_e # average hyperedge features for node v
Where:
e = a hyperedge (set of nodes)
|e| = number of nodes in hyperedge e
E(v) = set of hyperedges containing node v
W = shared weight matrixThis is equivalent to GCN on the clique expansion, but more principled: it preserves the hyperedge structure instead of losing it to pairwise reduction.
PyG implementation
import torch
import torch.nn.functional as F
from torch_geometric.nn import HypergraphConv
class HyperGNN(torch.nn.Module):
def __init__(self, in_channels, hidden, out_channels):
super().__init__()
self.conv1 = HypergraphConv(in_channels, hidden)
self.conv2 = HypergraphConv(hidden, out_channels)
def forward(self, x, hyperedge_index):
x = F.relu(self.conv1(x, hyperedge_index))
x = self.conv2(x, hyperedge_index)
return x
# hyperedge_index shape: [2, num_entries]
# Row 0: node indices, Row 1: hyperedge indices
# Example: 3 hyperedges connecting groups of nodes
hyperedge_index = torch.tensor([
[0, 1, 2, 1, 3, 4, 2, 4, 5], # node indices
[0, 0, 0, 1, 1, 1, 2, 2, 2], # hyperedge indices
])
# Hyperedge 0: {0, 1, 2}, Hyperedge 1: {1, 3, 4}, Hyperedge 2: {2, 4, 5}
model = HyperGNN(in_channels=64, hidden=32, out_channels=num_classes)hyperedge_index maps each node to its hyperedges. Hyperedge 0 connects nodes {0, 1, 2} simultaneously - a group interaction, not three pairwise edges.
When to use HypergraphConv
- Co-authorship networks. A paper with 5 authors is one hyperedge, not 10 pairwise edges. The group structure matters for research community analysis.
- Drug combination effects. Multiple drugs interact simultaneously. The group effect differs from the sum of pairwise effects.
- Shopping basket analysis. Items purchased together form a hyperedge. The basket-level pattern captures complementary product relationships.
- Visual object segmentation. Superpixels that belong to the same semantic region form a hyperedge capturing the region-level context.
When not to use HypergraphConv
- When interactions are inherently pairwise. Edges in social networks (A follows B), citation networks (A cites B), and molecular bonds are naturally pairwise. Hypergraph structure adds complexity without benefit.
- When clique expansion works. If converting hyperedges to pairwise edges (connecting all pairs within each group) achieves your target accuracy, the simpler approach wins.