Berlin Tech Meetup: The Future of Relational Foundation Models, Systems, and Real-World Applications

Register now:
PyG/Layer8 min read

HypergraphConv: Beyond Pairwise Edges

Standard graphs connect pairs of nodes. Hypergraphs connect groups. When the natural unit of interaction involves multiple entities simultaneously (co-authors, drug combinations, shopping baskets), HypergraphConv captures the group structure that pairwise reductions lose.

PyTorch Geometric

TL;DR

  • 1HypergraphConv operates on hyperedges: edges that connect any number of nodes simultaneously. Captures group interactions that pairwise graphs cannot represent.
  • 2Two-step message passing: (1) aggregate node features into hyperedge representations, (2) aggregate hyperedge representations back to nodes.
  • 3Use when interactions are inherently group-based: co-authorship, drug combinations, shopping baskets, group meetings.
  • 4Represented via hyperedge_index in PyG: a sparse incidence matrix mapping nodes to hyperedges.

Original Paper

Hypergraph Neural Networks

Feng et al. (2019). AAAI 2019

Read paper →

What HypergraphConv does

HypergraphConv performs two-phase message passing through hyperedges:

  1. Node to hyperedge: Aggregate features from all nodes in each hyperedge to form a hyperedge representation
  2. 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)

HypergraphConv formula
# 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 matrix

This 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

hypergraph_model.py
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.

Frequently asked questions

What is HypergraphConv in PyTorch Geometric?

HypergraphConv implements hypergraph convolution from Feng et al. (2019). Unlike standard graphs where edges connect exactly two nodes, hypergraphs have hyperedges that can connect any number of nodes simultaneously. HypergraphConv propagates information through these higher-order connections.

What is a hypergraph?

A hypergraph is a generalization of a graph where each edge (called a hyperedge) can connect two or more nodes. Example: a co-authorship hyperedge connects all authors of a paper simultaneously, not just pairs. A shopping cart hyperedge connects all items purchased together.

How does HypergraphConv differ from standard GCNConv?

GCNConv operates on pairwise edges. HypergraphConv operates on hyperedges via a two-step message passing: (1) aggregate node features into each hyperedge representation, (2) aggregate hyperedge representations back to each node. This captures group interactions that pairwise edges cannot.

When should I use a hypergraph vs a standard graph?

Use a hypergraph when the natural unit of interaction involves more than two entities simultaneously: co-authorship (all authors of a paper), drug combinations (multiple drugs interact), shopping baskets (items bought together), group meetings (all attendees). Reducing these to pairwise edges loses the group structure.

How is the hypergraph represented in PyG?

In PyG, a hypergraph is represented using a hyperedge_index tensor of shape [2, num_entries]: the first row contains node indices, the second row contains hyperedge indices. This is essentially an incidence matrix in COO format. Each hyperedge can connect any number of nodes.

Learn more about graph ML

PyTorch Geometric is the open-source foundation for graph neural networks. Explore more layers, concepts, and production patterns.