NN¶
hyperbench.nn
¶
HyperedgeAggregator
¶
Pool node embeddings into hyperedge embeddings using the incidence structure.
Each node-hyperedge incidence selects one node embedding row, then reduces those rows per hyperedge with the requested scatter aggregation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_index
|
Tensor
|
Hyperedge incidence in COO format of size |
required |
node_embeddings
|
Tensor
|
Node embedding matrix of size |
required |
num_hyperedges
|
int | None
|
Optional explicit hyperedge count.
When provided, the pooled output preserves empty hyperedges that do not appear in |
None
|
Source code in hyperbench/nn/aggregator.py
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 | |
pool(aggregation)
¶
Aggregate node embeddings for each hyperedge.
hyperedge_index is the COO encoding of the nonzero entries of H,
so hyperedge_index[0, k] = v and hyperedge_index[1, k] = e means H[v, e] = 1 for incidence k.
Let H be the binary incidence matrix of shape (num_nodes, num_hyperedges)
and let X be the node embedding matrix of shape (num_nodes, num_channels).
This method pools node features into hyperedge features using the incidence pattern in H:
- aggregation="sum" computes the equivalent of the standard sparse matrix product H^T X.
- aggregation="mean" computes D_e^{-1} H^T X, where D_e[e, e] = sum_v H[v, e] is the hyperedge cardinality matrix.
- aggregation in {"max", "min", "mul"} uses the same sparsity pattern as H^T X,
but replaces the summation over incident nodes with a channel-wise max, min, or product reduction.
- aggregation="maxmin" computes the channel-wise range max - min for each hyperedge.
Examples:
>>> hyperedge_index = [[0, 1, 2, 2, 3],
... [0, 0, 0, 1, 1]]
>>> node_embeddings = [[1, 10], [2, 20], [3, 30], [4, 40]]
>>> HyperedgeAggregator(hyperedge_index, node_embeddings).pool("mean")
... [[2, 20], [3.5, 35]]
>>> HyperedgeAggregator(hyperedge_index, node_embeddings).pool("sum")
... [[6, 60], [7, 70]]
>>> HyperedgeAggregator(hyperedge_index, node_embeddings).pool("max")
... [[3, 30], [4, 40]]
>>> HyperedgeAggregator(hyperedge_index, node_embeddings).pool("maxmin")
... [[2, 20], [1, 10]]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aggregation
|
Literal['maxmin', 'max', 'min', 'mean', 'mul', 'sum']
|
Reduction applied across the nodes belonging to each hyperedge. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
A hyperedge embedding matrix of shape |
Source code in hyperbench/nn/aggregator.py
NodeAggregator
¶
Pool hyperedge embeddings into node embeddings using the incidence structure.
Each node-hyperedge incidence selects one hyperedge embedding row, then reduces those rows per node with the requested scatter aggregation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_index
|
Tensor
|
Hyperedge incidence in COO format of size |
required |
hyperedge_embeddings
|
Tensor
|
Hyperedge embedding matrix of size |
required |
num_nodes
|
int | None
|
Optional explicit node count. When provided, the pooled output preserves isolated nodes that do not appear in |
None
|
Source code in hyperbench/nn/aggregator.py
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 | |
pool(aggregation)
¶
Aggregate hyperedge embeddings for each node.
hyperedge_index is the COO encoding of the nonzero entries of H,
so hyperedge_index[0, k] = v and hyperedge_index[1, k] = e means H[v, e] = 1 for incidence k.
Let H be the incidence matrix of shape (num_nodes, num_hyperedges)
and let E be the hyperedge embedding matrix of shape (num_hyperedges, num_channels).
This method pools hyperedge features into node features using the incidence pattern in H:
- aggregation="sum" computes the equivalent of the standard sparse matrix product H E.
- aggregation="mean" computes D_v^{-1} H E, where D_v[v, v] = sum_e H[v, e] is the node degree matrix.
- aggregation in {"max", "min", "mul"} uses the same sparsity pattern as H E,
but replaces the summation over incident hyperedges with a channel-wise max, min, or product reduction.
Examples:
>>> hyperedge_index = [[0, 1, 1, 2],
... [0, 0, 1, 1]]
>>> hyperedge_embeddings = [[10, 100], [20, 200]]
>>> NodeAggregator(hyperedge_index, hyperedge_embeddings).pool("mean")
... [[10, 100], [15, 150], [20, 200]]
>>> NodeAggregator(hyperedge_index, hyperedge_embeddings).pool("sum")
... [[10, 100], [30, 300], [20, 200]]
>>> NodeAggregator(hyperedge_index, hyperedge_embeddings).pool("max")
... [[10, 100], [20, 200], [20, 200]]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aggregation
|
Literal['maxmin', 'max', 'min', 'mean', 'mul', 'sum']
|
Reduction applied across the hyperedges incident to each node. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
A node embedding matrix of shape |
Source code in hyperbench/nn/aggregator.py
HGNNConv
¶
Bases: Module
The HGNNConv layer proposed in Hypergraph Neural Networks <https://arxiv.org/pdf/1809.09401>_ paper (AAAI 2019).
Reference implementation: source <https://deephypergraph.readthedocs.io/en/latest/_modules/dhg/nn/convs/hypergraphs/hgnn_conv.html#HGNNConv>_.
Each layer performs: X' = sigma(L_HGNN X Theta) where L_HGNN = D_n^{-1/2} H D_e^{-1} H^T D_n^{-1/2}
is the hypergraph Laplacian computed from the incidence matrix H. This smooths node features through
the hypergraph structure (nodes -> hyperedges -> nodes) without reducing to a pairwise graph.
Unlike HyperGCNConv, which uses a GCN Laplacian on a graph reduced from the hypergraph,
HGNNConv operates entirely in hypergraph space and preserves all higher-order relationships.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
The number of input channels. |
required |
out_channels
|
int
|
The number of output channels. |
required |
bias
|
bool
|
If set to |
True
|
use_batch_normalization
|
bool
|
If set to |
False
|
drop_rate
|
float
|
If set to a positive number, the layer will use dropout. Defaults to |
0.5
|
is_last
|
bool
|
If set to |
False
|
Source code in hyperbench/nn/conv.py
forward(x, hyperedge_index)
¶
Apply one HGNN convolution layer: project features, smooth via hypergraph Laplacian, then apply activation, batch norm, and dropout (unless this is the last layer).
The full per-layer formula is
X' = sigma( D_n^{-1/2} H D_e^{-1} H^T D_n^{-1/2} (X Theta) )
where the Laplacian L = D_n^{-1/2} H D_e^{-1} H^T D_n^{-1/2} is computed from
the hyperedge_index and can be passed in precomputed as hgnn_laplacian_matrix
for efficiency when the hypergraph structure does not change across forward passes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input node feature matrix of size |
required |
hyperedge_index
|
Tensor
|
Hyperedge incidence in COO format of size |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The output node feature matrix of size |
Source code in hyperbench/nn/conv.py
HGNNPConv
¶
Bases: Module
The HGNNPConv layer proposed in HGNN+: General Hypergraph Neural Networks <https://ieeexplore.ieee.org/document/9795251>_ paper (IEEE T-PAMI 2022).
Reference implementation: source <https://deephypergraph.readthedocs.io/en/latest/_modules/dhg/nn/convs/hypergraphs/hgnnp_conv.html#HGNNPConv>_.
Each layer performs: X' = sigma(M_HGNN+ X Theta) where
M_HGNN+ = D_v^{-1} H D_e^{-1} H^T is the HGNN+ smoothing matrix.
Unlike HGNNConv, which uses symmetric D_v^{-1/2} normalization for a
spectral Laplacian, HGNNPConv uses plain inverse degrees and performs
two-stage mean aggregation: nodes -> hyperedges -> nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
The number of input channels. |
required |
out_channels
|
int
|
The number of output channels. |
required |
bias
|
bool
|
If set to |
True
|
use_batch_normalization
|
bool
|
If set to |
False
|
drop_rate
|
float
|
If set to a positive number, the layer will use dropout. Defaults to |
0.5
|
is_last
|
bool
|
If set to |
False
|
Source code in hyperbench/nn/conv.py
forward(x, hyperedge_index)
¶
Apply one HGNN+ convolution layer using row-stochastic hypergraph smoothing.
The full per-layer formula is
X' = sigma( D_v^{-1} H D_e^{-1} H^T (X Theta) )
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input node feature matrix of size |
required |
hyperedge_index
|
Tensor
|
Hyperedge incidence in COO format of size |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The output node feature matrix of size |
Source code in hyperbench/nn/conv.py
HNHNConv
¶
Bases: Module
The HNHNConv layer proposed in HNHN: Hypergraph Networks with Hyperedge Neurons <https://arxiv.org/abs/2006.12278>_ paper.
Reference implementation: source <https://deephypergraph.readthedocs.io/en/latest/_modules/dhg/nn/convs/hypergraphs/hnhn_conv.html#HNHNConv>_.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
The number of input channels. |
required |
out_channels
|
int
|
The number of output channels. |
required |
bias
|
bool
|
If set to |
True
|
use_batch_normalization
|
bool
|
If set to |
False
|
drop_rate
|
float
|
If set to a positive number, the layer will use dropout. Defaults to |
0.5
|
is_last
|
bool
|
If set to |
False
|
Source code in hyperbench/nn/conv.py
forward(x, hyperedge_index)
¶
Apply one HNHN convolution layer using two learned projections around node-to-hyperedge and hyperedge-to-node mean aggregation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input node feature matrix of size |
required |
hyperedge_index
|
Tensor
|
Hyperedge incidence in COO format of size |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The output node feature matrix of size |
Source code in hyperbench/nn/conv.py
HyperGCNConv
¶
Bases: Module
The HyperGCNConv layer proposed in HyperGCN: A New Method of Training Graph Convolutional Networks on Hypergraphs <https://dl.acm.org/doi/10.5555/3454287.3454422>_ paper (NeurIPS 2019).
Reference implementation: source <https://deephypergraph.readthedocs.io/en/latest/_modules/dhg/nn/convs/hypergraphs/hypergcn_conv.html#HyperGCNConv>_.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
The number of input channels. |
required |
out_channels
|
int
|
The number of output channels. |
required |
bias
|
bool
|
If set to |
True
|
use_batch_normalization
|
bool
|
If set to |
False
|
drop_rate
|
float
|
If set to a positive number, the layer will use dropout. Defaults to |
0.5
|
use_mediator
|
bool
|
Whether to use mediator to transform the hyperedges to edges in the graph. Defaults to |
False
|
is_last
|
bool
|
If set to |
False
|
Source code in hyperbench/nn/conv.py
forward(x, hyperedge_index, gcn_laplacian_matrix=None)
¶
The forward function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input node feature matrix. Size |
required |
hyperedge_index
|
Tensor
|
Hyperedge indices representing the hypergraph structure. Size |
required |
gcn_laplacian_matrix
|
Tensor | None
|
Optional precomputed normalized GCN Laplacian matrix. Size |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
The output node feature matrix. Size |
Source code in hyperbench/nn/conv.py
NodeEnricher
¶
HyperedgeEnricher
¶
FillValueHyperedgeAttrsEnricher
¶
Bases: HyperedgeAttrsEnricher
Generates simple hyperedge attributes by filling them with a constant value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
str | None
|
Directory for saving/loading cached features. If |
None
|
fill_value
|
float
|
The constant value to fill the hyperedge attributes with. Defaults to |
1.0
|
Source code in hyperbench/nn/enricher.py
enrich(hyperedge_index)
¶
Generate hyperedge attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_index
|
Tensor
|
Hyperedge index tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of shape |
Source code in hyperbench/nn/enricher.py
ABHyperedgeWeightsEnricher
¶
Bases: HyperedgeWeightsEnricher
Generates hyperedge weights based on the number of nodes in each hyperedge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
str | None
|
Directory for saving/loading cached features. If |
None
|
alpha
|
float
|
Scaling factor for the random component added to weights. Must be between 0.0 and 1.0. |
1.0
|
beta
|
float | None
|
If provided, the random component is alpha * beta. If None, no random component is added. |
None
|
Source code in hyperbench/nn/enricher.py
enrich(hyperedge_index)
¶
Compute edge weights as the number of nodes in each hyperedge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_index
|
Tensor
|
Hyperedge index tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of shape |
Source code in hyperbench/nn/enricher.py
LaplacianPositionalEncodingEnricher
¶
Bases: NodeEnricher
Enrich node features with Laplacian Positional Encodings computed from the symmetric normalized Laplacian of the clique expansion of the hypergraph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_features
|
int
|
Number of positional encoding features to generate for each node. |
required |
num_nodes
|
int
|
Total number of nodes in the graph. If not provided, it will be inferred from the hyperedge_index. This is only needed if the hyperedge_index does not include all nodes (e.g., some isolated nodes are missing). Another instance is when the setting is transductive and the hyperedge index contains some hyperedges that do not contain all the nodes in the node space. |
0
|
cache_dir
|
str | None
|
Optional directory to cache computed features. If |
None
|
Source code in hyperbench/nn/enricher.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 | |
enrich(hyperedge_index)
¶
Compute Laplacian Positional Encoding: the k smallest non-trivial eigenvectors of the symmetric normalized Laplacian L = I - D^{-½} A D^{-½}.
The first eigenvector (constant, eigenvalue ~0) is skipped. The next num_features eigenvectors are returned as positional features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_index
|
Tensor
|
Hyperedge index tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of shape |
Source code in hyperbench/nn/enricher.py
Node2VecEnricher
¶
Bases: NodeEnricher
Enrich node features using Node2Vec embeddings computed from the clique expansion of the hypergraph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_features
|
int
|
Dimensionality of the node embeddings to generate. |
required |
walk_length
|
int
|
Length of each random walk. |
20
|
context_size
|
int
|
Window size for the skip-gram model (number of neighbors in the walk considered as context).
For example, if |
10
|
num_walks_per_node
|
int
|
Number of random walks to start at each node. |
10
|
p
|
float
|
Return hyperparameter for Node2Vec. Default is |
1.0
|
q
|
float
|
In-out hyperparameter for Node2Vec. Default is |
1.0
|
num_negative_samples
|
int
|
Number of negative samples to use for training the skip-gram model.
If set to |
1
|
num_nodes
|
int
|
Total number of nodes in the graph. If not provided, it will be inferred from the hyperedge_index. This is only needed if the hyperedge_index does not include all nodes (e.g., some isolated nodes are missing). |
0
|
graph_reduction_strategy
|
Literal['clique_expansion']
|
Strategy for reducing the hyperedge graph. Defaults to |
'clique_expansion'
|
num_epochs
|
int
|
Number of epochs used to optimize Node2Vec embeddings. Defaults to |
5
|
learning_rate
|
float
|
Learning rate for embedding optimization. Defaults to |
0.01
|
batch_size
|
int
|
Batch size used by the random-walk loader. Defaults to |
128
|
sparse
|
bool
|
Whether Node2Vec embeddings should use sparse gradients. |
True
|
cache_dir
|
str | None
|
Optional directory to cache computed embeddings. If |
None
|
verbose
|
bool
|
Whether to print verbose output during training. Defaults to |
False
|
Source code in hyperbench/nn/enricher.py
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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |
enrich(hyperedge_index)
¶
Compute Node2Vec embeddings from the clique expansion of the hypergraph.
The hypergraph is converted to a regular graph via clique expansion, where each hyperedge of size k
contributes a k x k block of edges between its member nodes.
The resulting edge_index is then used to train a Node2Vec model using random walks and the skip-gram objective.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_index
|
Tensor
|
Hyperedge index tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of shape |
Source code in hyperbench/nn/enricher.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |
VilLainHyperedgeAttrsEnricher
¶
Bases: _VilLainTrainer, HyperedgeAttrsEnricher
Enrich hyperedge attributes with VilLain embeddings learned from hypergraph topology.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_features
|
int
|
Dimensionality of the hyperedge embeddings to generate. |
required |
num_nodes
|
int
|
Total number of nodes, including isolated nodes that do not appear in |
0
|
num_hyperedges
|
int
|
Total number of hyperedges, including empty hyperedges that do not appear in |
0
|
labels_per_subspace
|
int
|
Number of virtual labels per subspace. Defaults to |
2
|
training_steps
|
int
|
Propagation steps used for VilLain self-supervised loss. Defaults to |
4
|
generation_steps
|
int
|
Propagation steps averaged for final embeddings. Defaults to |
100
|
tau
|
float
|
Gumbel-Softmax temperature. Defaults to |
1.0
|
eps
|
float
|
Numerical stability constant. Defaults to |
1e-10
|
num_epochs
|
int
|
Number of epochs used to optimize VilLain embeddings. Defaults to |
5
|
learning_rate
|
float
|
Learning rate for embedding optimization. Defaults to |
0.01
|
weight_decay
|
float
|
Weight decay for the optimizer. Defaults to |
0.0
|
cache_dir
|
str | None
|
Optional directory to cache computed features. If |
None
|
verbose
|
bool
|
Whether to print verbose output during training. Defaults to |
False
|
Source code in hyperbench/nn/enricher.py
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 | |
enrich(hyperedge_index)
¶
Train VilLain on the hypergraph and return hyperedge embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_index
|
Tensor
|
Hyperedge index tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of shape |
Source code in hyperbench/nn/enricher.py
VilLainEnricher
¶
Bases: _VilLainTrainer, NodeEnricher
Enrich node features with VilLain embeddings learned from hypergraph topology.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_features
|
int
|
Dimensionality of the node embeddings to generate. |
required |
num_nodes
|
int
|
Total number of nodes, including isolated nodes that do not appear in |
0
|
num_hyperedges
|
int
|
Total number of hyperedges, including empty hyperedges that do not appear in |
0
|
labels_per_subspace
|
int
|
Number of virtual labels per subspace. Defaults to |
2
|
training_steps
|
int
|
Propagation steps used for VilLain self-supervised loss. Defaults to |
4
|
generation_steps
|
int
|
Propagation steps averaged for final embeddings. Defaults to |
100
|
tau
|
float
|
Gumbel-Softmax temperature. Defaults to |
1.0
|
eps
|
float
|
Numerical stability constant. Defaults to |
1e-10
|
num_epochs
|
int
|
Number of epochs used to optimize VilLain embeddings. Defaults to |
5
|
learning_rate
|
float
|
Learning rate for embedding optimization. Defaults to |
0.01
|
weight_decay
|
float
|
Weight decay for the optimizer. Defaults to |
0.0
|
cache_dir
|
str | None
|
Optional directory to cache computed features. If |
None
|
verbose
|
bool
|
Whether to print verbose output during training. Defaults to |
False
|
Source code in hyperbench/nn/enricher.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 | |
enrich(hyperedge_index)
¶
Train VilLain on the hypergraph and return node embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_index
|
Tensor
|
Hyperedge index tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of shape |
Source code in hyperbench/nn/enricher.py
NHPRankingLoss
¶
Bases: Module
Ranking loss that pushes positive hyperedges above sampled negatives.
Examples:
>>> logits = [2.0, 1.0, -1.0]
>>> labels = [1.0, 1.0, 0.0]
>>> loss = NHPRankingLoss()
>>> loss(logits, labels)
>>> loss.ndim
... 0
Source code in hyperbench/nn/loss.py
forward(logits, labels)
¶
Compute the ranking loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits
|
Tensor
|
Logit scores for each candidate hyperedge, of shape |
required |
labels
|
Tensor
|
Binary labels indicating positive (1) and negative (0) hyperedges, of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar loss value. |
Source code in hyperbench/nn/loss.py
VilLainLoss
¶
VilLain self-supervised loss formulas.
This class is intentionally stateless with respect to propagation. The VilLain model owns message passing and accumulation over steps and this class owns the per-step formulas for local and global loss,
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_subspaces
|
int
|
Number of virtual-label subspaces in each embedding. |
required |
labels_per_subspace
|
int
|
Number of virtual labels in each subspace. |
required |
eps
|
float
|
Numerical stability constant used in logarithms and cosine similarity. |
1e-12
|
Source code in hyperbench/nn/loss.py
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 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
local_loss(node_embeddings, hyperedge_embeddings)
¶
Compute the local entropy loss for one propagation step.
Local loss is minimized to encourage propagated node and hyperedge distributions to become confident within each virtual-label subspace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_embeddings
|
Tensor
|
Propagated node states of shape |
required |
hyperedge_embeddings
|
Tensor
|
Propagated hyperedge states with the same channel dimension as |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar tensor containing node plus hyperedge entropy losses. |
Source code in hyperbench/nn/loss.py
global_loss(node_embeddings, hyperedge_embeddings)
¶
Compute global anti-collapse losses for one propagation step.
Global loss combines negative global entropy, which encourages balanced label usage with a distinctiveness term that separates label columns inside each subspace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_embeddings
|
Tensor
|
Propagated node states of shape |
required |
hyperedge_embeddings
|
Tensor
|
Propagated hyperedge states with the same channel dimension as |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar tensor containing node plus hyperedge global losses. |
Source code in hyperbench/nn/loss.py
total_loss(local_loss, global_loss)
¶
Combine accumulated local and global VilLain losses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_loss
|
Tensor
|
Accumulated local entropy loss. |
required |
global_loss
|
Tensor
|
Accumulated balance plus distinctiveness loss. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar tensor to minimize. |
Source code in hyperbench/nn/loss.py
entropy_loss(x)
¶
Compute mean entropy within each virtual-label subspace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Flattened virtual-label probabilities of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar entropy loss. |
Source code in hyperbench/nn/loss.py
balance_loss(x)
¶
Compute negative entropy of global virtual-label usage.
This term is minimized, so the negative sign makes optimization maximize entropy of average label usage and reduces collapse to one virtual label.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Flattened virtual-label probabilities of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar balance loss. |
Source code in hyperbench/nn/loss.py
distinctiveness_loss(x)
¶
Penalize similar virtual-label columns inside each subspace.
For every subspace, this compares all label columns across items with cosine similarity and applies a diagonal classification objective. The diagonal target encourages each label column to be most similar to itself and less similar to other labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Flattened virtual-label probabilities of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar distinctiveness loss. |
Source code in hyperbench/nn/loss.py
VilLainLossParts
¶
Bases: TypedDict
Named VilLain self-supervised loss parts returned by VilLain.loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_loss
|
Sum of node and hyperedge local entropy losses over all training propagation steps. |
required | |
global_loss
|
Sum of balance and distinctiveness losses over all training propagation steps. |
required |
Source code in hyperbench/nn/loss.py
CommonNeighborsScorer
¶
Bases: NeighborScorer
Source code in hyperbench/nn/scorer.py
score(candidate_nodes, candidate_to_neighbors)
¶
Compute the CN score for a single candidate hyperedge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidate_nodes
|
list[int]
|
List of node IDs forming the candidate hyperedge.
If less than 2 nodes are provided, the function returns a default score of |
required |
candidate_to_neighbors
|
dict[int, Neighborhood]
|
Mapping from node IDs to their set of neighbors. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The aggregated common neighbors score. |
Source code in hyperbench/nn/scorer.py
score_batch(hyperedge_index, node_to_neighbors=None)
¶
Score all hyperedges in a hyperedge index tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_index
|
Tensor
|
Tensor of shape |
required |
node_to_neighbors
|
dict[int, Neighborhood] | None
|
Optional precomputed node to neighborhood mapping. If None, it will be computed from |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
A 1-D tensor of shape |