File size: 8,201 Bytes
8949695 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | """
Model architectures for TopoHyper and baselines.
Implements:
- TopoHyperNet: Full hybrid architecture
- GCNNet, GATNet: Standard GNN baselines
- HGNNNet: Hypergraph-only baseline
- SimplicialNet: Simplicial-only baseline
- SimpleHybrid: Naive concatenation hybrid
- get_model(): Factory function
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
from .layers import (TopoHyperConv, TopoHyperPool, SimplicialConv,
HypergraphConv, GCNLayer, GATLayer)
class TopoHyperNet(nn.Module):
"""
Full TopoHyper architecture.
Architecture:
1. Input projection
2. N x TopoHyperConv layers (3-phase: simplicial + hypergraph + fusion)
3. TopoHyperPool (mean + max + attention readout)
4. Classification head
Args:
in_dim: Input feature dimension
hidden_dim: Hidden layer dimension
num_classes: Number of output classes
num_layers: Number of TopoHyperConv layers
dropout: Dropout rate
use_bridge: Whether to use bridge matrix in fusion
use_attention: Whether to use attention-gated fusion
"""
def __init__(self, in_dim, hidden_dim, num_classes, num_layers=2,
dropout=0.3, use_bridge=True, use_attention=True):
super().__init__()
self.input_proj = nn.Linear(in_dim, hidden_dim)
self.convs = nn.ModuleList()
for i in range(num_layers):
self.convs.append(TopoHyperConv(
hidden_dim, hidden_dim,
use_bridge=use_bridge,
use_attention=use_attention
))
self.pool = TopoHyperPool(hidden_dim)
self.classifier = nn.Sequential(
nn.Linear(hidden_dim * 3, hidden_dim),
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(hidden_dim, num_classes)
)
self.dropout = dropout
def forward(self, x, sc, hg, edge_index=None):
x = F.relu(self.input_proj(x))
x = F.dropout(x, p=self.dropout, training=self.training)
for conv in self.convs:
x = conv(x, sc, hg)
x = F.dropout(x, p=self.dropout, training=self.training)
g = self.pool(x)
return self.classifier(g)
class GCNNet(nn.Module):
"""GCN baseline."""
def __init__(self, in_dim, hidden_dim, num_classes, num_layers=2, dropout=0.3):
super().__init__()
self.layers = nn.ModuleList()
self.layers.append(GCNLayer(in_dim, hidden_dim))
for _ in range(num_layers - 1):
self.layers.append(GCNLayer(hidden_dim, hidden_dim))
self.classifier = nn.Sequential(
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(hidden_dim, num_classes)
)
self.dropout = dropout
def forward(self, x, sc=None, hg=None, edge_index=None):
for layer in self.layers:
x = F.relu(layer(x, edge_index))
x = F.dropout(x, p=self.dropout, training=self.training)
g = x.mean(dim=0)
return self.classifier(g)
class GATNet(nn.Module):
"""GAT baseline."""
def __init__(self, in_dim, hidden_dim, num_classes, num_layers=2, dropout=0.3):
super().__init__()
self.layers = nn.ModuleList()
self.layers.append(GATLayer(in_dim, hidden_dim))
for _ in range(num_layers - 1):
self.layers.append(GATLayer(hidden_dim, hidden_dim))
self.classifier = nn.Sequential(
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(hidden_dim, num_classes)
)
self.dropout = dropout
def forward(self, x, sc=None, hg=None, edge_index=None):
for layer in self.layers:
x = F.relu(layer(x, edge_index))
x = F.dropout(x, p=self.dropout, training=self.training)
g = x.mean(dim=0)
return self.classifier(g)
class HGNNNet(nn.Module):
"""Hypergraph-only baseline."""
def __init__(self, in_dim, hidden_dim, num_classes, num_layers=2, dropout=0.3):
super().__init__()
self.layers = nn.ModuleList()
self.layers.append(HypergraphConv(in_dim, hidden_dim))
for _ in range(num_layers - 1):
self.layers.append(HypergraphConv(hidden_dim, hidden_dim))
self.classifier = nn.Sequential(
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(hidden_dim, num_classes)
)
self.dropout = dropout
def forward(self, x, sc=None, hg=None, edge_index=None):
for layer in self.layers:
x = F.relu(layer(x, hg))
x = F.dropout(x, p=self.dropout, training=self.training)
g = x.mean(dim=0)
return self.classifier(g)
class SimplicialNet(nn.Module):
"""Simplicial-only baseline."""
def __init__(self, in_dim, hidden_dim, num_classes, num_layers=2, dropout=0.3):
super().__init__()
self.layers = nn.ModuleList()
self.layers.append(SimplicialConv(in_dim, hidden_dim))
for _ in range(num_layers - 1):
self.layers.append(SimplicialConv(hidden_dim, hidden_dim))
self.classifier = nn.Sequential(
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(hidden_dim, num_classes)
)
self.dropout = dropout
def forward(self, x, sc=None, hg=None, edge_index=None):
for layer in self.layers:
x = F.relu(layer(x, sc))
x = F.dropout(x, p=self.dropout, training=self.training)
g = x.mean(dim=0)
return self.classifier(g)
class SimpleHybrid(nn.Module):
"""
Naive hybrid baseline: separate simplicial + hypergraph branches
concatenated (no bridge, no attention).
"""
def __init__(self, in_dim, hidden_dim, num_classes, num_layers=2, dropout=0.3):
super().__init__()
half_dim = hidden_dim // 2
self.sc_layers = nn.ModuleList()
self.sc_layers.append(SimplicialConv(in_dim, half_dim))
for _ in range(num_layers - 1):
self.sc_layers.append(SimplicialConv(half_dim, half_dim))
self.hg_layers = nn.ModuleList()
self.hg_layers.append(HypergraphConv(in_dim, half_dim))
for _ in range(num_layers - 1):
self.hg_layers.append(HypergraphConv(half_dim, half_dim))
self.classifier = nn.Sequential(
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(hidden_dim, num_classes)
)
self.dropout = dropout
def forward(self, x, sc=None, hg=None, edge_index=None):
h_sc = x
for layer in self.sc_layers:
h_sc = F.relu(layer(h_sc, sc))
h_sc = F.dropout(h_sc, p=self.dropout, training=self.training)
h_hg = x
for layer in self.hg_layers:
h_hg = F.relu(layer(h_hg, hg))
h_hg = F.dropout(h_hg, p=self.dropout, training=self.training)
h = torch.cat([h_sc, h_hg], dim=-1)
g = h.mean(dim=0)
return self.classifier(g)
def get_model(name, in_dim, hidden_dim, num_classes, **kwargs):
"""
Factory function to create models by name.
Args:
name: One of 'topohyper', 'gcn', 'gat', 'hgnn', 'simplicial', 'simple_hybrid'
in_dim: Input feature dimension
hidden_dim: Hidden dimension
num_classes: Number of classes
**kwargs: Additional model arguments (use_bridge, use_attention, etc.)
"""
models = {
'topohyper': TopoHyperNet,
'gcn': GCNNet,
'gat': GATNet,
'hgnn': HGNNNet,
'simplicial': SimplicialNet,
'simple_hybrid': SimpleHybrid,
}
if name not in models:
raise ValueError(f"Unknown model: {name}. Choose from {list(models.keys())}")
return models[name](in_dim, hidden_dim, num_classes, **kwargs)
|