NN¶
hypertorch.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.
Attributes:
| Name | Type | Description |
|---|---|---|
hyperedge_index_wrapper |
HyperedgeIndex
|
Wrapper around the hyperedge incidence tensor. |
node_embeddings |
Tensor
|
Node embedding matrix of size |
num_hyperedges |
int | None
|
Optional explicit hyperedge count.
When provided, the pooled output preserves empty hyperedges that
do not appear in |
Source code in hypertorch/nn/aggregator.py
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 | |
__init__(hyperedge_index, node_embeddings, num_hyperedges=None)
¶
Initialize the hyperedge aggregator.
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. Defaults to |
None
|
Source code in hypertorch/nn/aggregator.py
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.
Aggregations
aggregation="sum"computes the equivalent of the standard sparse matrix productH^T X.aggregation="mean"computesD_e^{-1} H^T X, whereD_e[e, e] = sum_v H[v, e]is the hyperedge cardinality matrix.aggregation in {"max", "min", "mul"}uses the same sparsity pattern asH^T X, but replaces the summation over incident nodes with a channel-wisemax,min, or product reduction.aggregation="maxmin"computes the channel-wise rangemax - minfor 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:
| Name | Type | Description |
|---|---|---|
hyperedge_embeddings |
Tensor
|
A hyperedge embedding matrix of
shape |
Source code in hypertorch/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.
Attributes:
| Name | Type | Description |
|---|---|---|
hyperedge_index_wrapper |
HyperedgeIndex
|
Wrapper around the hyperedge incidence tensor. |
hyperedge_embeddings |
Tensor
|
Hyperedge embedding matrix of size |
num_nodes |
int | None
|
Optional explicit node count. When provided, the pooled output preserves
isolated nodes that do not appear in |
Source code in hypertorch/nn/aggregator.py
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 | |
__init__(hyperedge_index, hyperedge_embeddings, num_nodes=None)
¶
Initialize the node aggregator.
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. Defaults to |
None
|
Source code in hypertorch/nn/aggregator.py
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.
Aggregations
aggregation="sum"computes the equivalent of the standard sparse matrix productH E.aggregation="mean"computesD_v^{-1} H E, whereD_v[v, v] = sum_e H[v, e]is the node degree matrix.aggregation in {"max", "min", "mul"}uses the same sparsity pattern asH E, but replaces the summation over incident hyperedges with a channel-wisemax,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:
| Name | Type | Description |
|---|---|---|
node_embeddings |
Tensor
|
A node embedding matrix of shape |
Source code in hypertorch/nn/aggregator.py
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 | |
HGNNConv
¶
Bases: Module
References
- The HGNNConv layer proposed in Hypergraph Neural Networks paper (AAAI 2019).
- Reference implementation: Code.
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.
Attributes:
| Name | Type | Description |
|---|---|---|
is_last |
bool
|
Whether to skip the final activation and dropout.
If |
batch_norm_1d |
BatchNorm1d | None
|
Optional batch normalization layer. |
activation_fn |
ReLU
|
Activation function applied to hidden outputs. |
dropout |
Dropout
|
Dropout layer applied to hidden outputs. Defaults to |
theta |
Linear
|
Learnable feature projection. |
Source code in hypertorch/nn/conv.py
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 | |
__init__(in_channels, out_channels, bias=True, use_batch_normalization=False, drop_rate=0.5, is_last=False)
¶
Initialize the HGNN convolution layer.
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 hypertorch/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:
| Name | Type | Description |
|---|---|---|
x |
Tensor
|
The output node feature matrix of size |
Source code in hypertorch/nn/conv.py
HGNNPConv
¶
Bases: Module
References
- The HGNNPConv layer proposed in HGNN+: General Hypergraph Neural Networks paper (IEEE T-PAMI 2022).
- Reference implementation: Code.
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.
Attributes:
| Name | Type | Description |
|---|---|---|
is_last |
bool
|
Whether to skip the final activation and dropout.
If |
batch_norm_1d |
BatchNorm1d | None
|
Optional batch normalization layer. |
activation_fn |
ReLU
|
Activation function applied to hidden outputs. |
dropout |
Dropout
|
Dropout layer applied to hidden outputs. Defaults to |
theta |
Linear
|
Learnable feature projection. |
Source code in hypertorch/nn/conv.py
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 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 | |
__init__(in_channels, out_channels, bias=True, use_batch_normalization=False, drop_rate=0.5, is_last=False)
¶
Initialize the HGNN+ convolution layer.
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 hypertorch/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:
| Name | Type | Description |
|---|---|---|
x |
Tensor
|
The output node feature matrix of size |
Source code in hypertorch/nn/conv.py
HNHNConv
¶
Bases: Module
References
- The HNHNConv layer proposed in HNHN: Hypergraph Networks with Hyperedge Neurons paper.
- Reference implementation: Code.
Attributes:
| Name | Type | Description |
|---|---|---|
is_last |
bool
|
Whether to skip the final activation and dropout.
If |
batch_norm_1d |
BatchNorm1d | None
|
Optional batch normalization layer. |
activation_fn |
ReLU
|
Activation function applied to hidden outputs. |
dropout |
Dropout
|
Dropout layer applied to hidden outputs. Defaults to |
theta_v2e |
Linear
|
Learnable node-to-hyperedge projection. |
theta_e2v |
Linear
|
Learnable hyperedge-to-node projection. |
Source code in hypertorch/nn/conv.py
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 | |
__init__(in_channels, out_channels, bias=True, use_batch_normalization=False, drop_rate=0.5, is_last=False)
¶
Initialize the HNHN convolution layer.
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 hypertorch/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:
| Name | Type | Description |
|---|---|---|
x |
Tensor
|
The output node feature matrix of size |
Source code in hypertorch/nn/conv.py
HyperGCNConv
¶
Bases: Module
References
- The HyperGCNConv layer proposed in HyperGCN: A New Method of Training Graph Convolutional Networks on Hypergraphs paper (NeurIPS 2019).
- Reference implementation: source.
Attributes:
| Name | Type | Description |
|---|---|---|
is_last |
bool
|
Whether to skip the final activation and dropout.
If |
use_mediator |
bool
|
Whether to use mediator to transform the hyperedges to edges in the graph.
Defaults to |
batch_norm_1d |
BatchNorm1d | None
|
Optional batch normalization layer. |
activation_fn |
ReLU
|
Activation function applied to hidden outputs. |
dropout |
Dropout
|
Dropout layer applied to hidden outputs. Defaults to |
theta |
Linear
|
Learnable feature projection. |
seed |
int | None
|
Optional random seed for reducing hyperedges to graph edges.
Defaults to |
Source code in hypertorch/nn/conv.py
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 | |
__init__(in_channels, out_channels, bias=True, use_batch_normalization=False, drop_rate=0.5, use_mediator=False, is_last=False, seed=None)
¶
Initialize the HyperGCN convolution layer.
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
|
seed
|
int | None
|
Optional random seed for the random reduction of hyperedges to edges.
Defaults to |
None
|
Source code in hypertorch/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:
| Name | Type | Description |
|---|---|---|
x |
Tensor
|
The output node feature matrix. Size |
Source code in hypertorch/nn/conv.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 hypertorch/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:
| Name | Type | Description |
|---|---|---|
loss |
Tensor
|
Scalar loss value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in hypertorch/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,
Attributes:
| Name | Type | Description |
|---|---|---|
num_subspaces |
int
|
Number of virtual-label subspaces in each embedding. |
labels_per_subspace |
int
|
Number of virtual labels in each subspace. |
eps |
float
|
Numerical stability constant used in logarithms and cosine similarity. |
Source code in hypertorch/nn/loss.py
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 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 | |
__init__(num_subspaces, labels_per_subspace, eps=1e-12)
¶
Initialize the VilLain loss helper.
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. Defaults to |
1e-12
|
Source code in hypertorch/nn/loss.py
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:
| Name | Type | Description |
|---|---|---|
loss |
Tensor
|
Scalar tensor containing node plus hyperedge entropy losses. |
Source code in hypertorch/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:
| Name | Type | Description |
|---|---|---|
loss |
Tensor
|
Scalar tensor containing node plus hyperedge global losses. |
Source code in hypertorch/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:
| Name | Type | Description |
|---|---|---|
loss |
Tensor
|
Scalar tensor to minimize. |
Source code in hypertorch/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:
| Name | Type | Description |
|---|---|---|
loss |
Tensor
|
Scalar entropy loss. |
Source code in hypertorch/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:
| Name | Type | Description |
|---|---|---|
loss |
Tensor
|
Scalar balance loss. |
Source code in hypertorch/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:
| Name | Type | Description |
|---|---|---|
loss |
Tensor
|
Scalar distinctiveness loss. |
Source code in hypertorch/nn/loss.py
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 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 | |
VilLainLossParts
¶
Bases: TypedDict
Named VilLain self-supervised loss parts returned by VilLain.loss.
Attributes:
| Name | Type | Description |
|---|---|---|
local_loss |
Tensor
|
Sum of node and hyperedge local entropy losses over all training propagation steps. |
global_loss |
Tensor
|
Sum of balance and distinctiveness losses over all training propagation steps. |
Source code in hypertorch/nn/loss.py
CommonNeighborsNodeScorer
¶
Bases: NeighborScorer
Scorer for computing the Common Neighbors (CN) score for nodes.
Attributes:
| Name | Type | Description |
|---|---|---|
num_classes |
int
|
Number of node classes to score. |
class_to_node_ids |
dict[int, list[int]]
|
Mapping from class IDs to labeled reference node IDs. |
aggregation |
Literal['mean', 'min', 'sum']
|
Method used to aggregate pairwise node-reference scores per class. |
exclude_self_reference |
bool
|
Whether a node should ignore itself when it also appears among labeled reference nodes. |
Source code in hypertorch/nn/scorer.py
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 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 | |
__init__(num_classes, class_to_node_ids=None, aggregation='sum', exclude_self_reference=True)
¶
Initialize the common-neighbors node scorer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_classes
|
int
|
Number of node classes to score. |
required |
class_to_node_ids
|
dict[int, list[int]] | None
|
Mapping from class IDs to labeled reference node IDs. Defaults to an empty mapping for each class. |
None
|
aggregation
|
Literal['mean', 'min', 'sum']
|
Method used to aggregate pairwise node-reference scores
per class. Defaults to |
'sum'
|
exclude_self_reference
|
bool
|
Whether a node should ignore itself when it also
appears among labeled reference nodes. Defaults to |
True
|
Source code in hypertorch/nn/scorer.py
score(candidate_nodes, candidate_to_neighbors)
¶
Score one node against reference nodes from a single class.
The first item in candidate_nodes is the node being classified.
Remaining items are labeled reference nodes for one class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidate_nodes
|
list[int]
|
Node IDs containing one target node followed by reference nodes for one class. |
required |
candidate_to_neighbors
|
dict[int, Neighborhood]
|
Mapping from node IDs to their neighborhoods. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
score |
float
|
Aggregated node-class score. |
Source code in hypertorch/nn/scorer.py
score_batch(candidate_nodes, hyperedge_index=None, node_to_neighbors=None)
¶
Score nodes against labeled reference nodes grouped by class. A reference node is a training node labeled with a class ID, and the score of a node for each class is computed by aggregating the pairwise common-neighbor counts between the node being scored and each reference node for a class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidate_nodes
|
Tensor
|
Tensor containing node IDs to score of shape |
required |
hyperedge_index
|
Tensor | None
|
Tensor of shape |
None
|
node_to_neighbors
|
dict[int, Neighborhood] | None
|
Mapping from node IDs to their training-world neighborhoods. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
scores |
Tensor
|
Raw node-class scores of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in hypertorch/nn/scorer.py
__score_node_class(node_id, reference_node_ids, node_to_neighbors)
¶
Score one node against labeled reference nodes for a single class. The score is computed by aggregating the pairwise common-neighbor counts between the node being scored and each reference node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
int
|
Node ID to classify. |
required |
reference_node_ids
|
list[int]
|
Labeled reference node IDs for one class. |
required |
node_to_neighbors
|
dict[int, Neighborhood]
|
Mapping from node IDs to their neighborhoods. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
score |
float
|
Aggregated node-class score. |
Source code in hypertorch/nn/scorer.py
__score_pair(node_id, reference_node_id, node_to_neighbors)
¶
Compute the pairwise common-neighbor count for two nodes. The count is the number of nodes that are neighbors of both the node being scored and the reference node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
int
|
Node ID to classify. |
required |
reference_node_id
|
int
|
Labeled reference node ID for one class. |
required |
node_to_neighbors
|
dict[int, Neighborhood]
|
Mapping from node IDs to their neighborhoods. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
score |
int
|
Pairwise common-neighbor count. |
Source code in hypertorch/nn/scorer.py
CommonNeighborsHyperedgeScorer
¶
Bases: NeighborScorer
Scorer for computing the Common Neighbors (CN) score for hyperedges.
Attributes:
| Name | Type | Description |
|---|---|---|
aggregation |
Literal['mean', 'min', 'sum']
|
Method to aggregate node embeddings per hyperedge. Can be one of
|
Source code in hypertorch/nn/scorer.py
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 | |
__init__(aggregation)
¶
Initialize the common-neighbors scorer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aggregation
|
Literal['mean', 'min', 'sum']
|
Method used to aggregate pairwise common-neighbor counts. |
required |
Source code in hypertorch/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:
| Name | Type | Description |
|---|---|---|
score |
float
|
The aggregated common neighbors score. |
Source code in hypertorch/nn/scorer.py
score_batch(candidate_nodes, hyperedge_index=None, node_to_neighbors=None)
¶
Score a batch of hyperedges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidate_nodes
|
Tensor
|
Tensor containing gloal node IDs so that nodes in the batch
can be scored by mapping them to the ID space of the training hypergraph.
The tensor shape is |
required |
hyperedge_index
|
Tensor | None
|
Tensor containing the hyperedge index. Defaults to |
None
|
node_to_neighbors
|
dict[int, Neighborhood] | None
|
Optional precomputed node to neighborhood mapping.
Defaults to |
None
|
node_to_neighbors
|
dict[int, Neighborhood] | None
|
Optional precomputed node to neighborhood mapping.
If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
scores |
Tensor
|
A 1-D tensor of shape |
Source code in hypertorch/nn/scorer.py
NeighborScorer
¶
Bases: ABC
Abstract base class for neighbor scorers.
Source code in hypertorch/nn/scorer.py
score(candidate_nodes, candidate_to_neighbors)
abstractmethod
¶
Score a single candidate hyperedge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidate_nodes
|
list[int]
|
Node IDs in the candidate hyperedge. |
required |
candidate_to_neighbors
|
dict[int, Neighborhood]
|
Mapping from node IDs to their neighborhoods. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
score |
float
|
Candidate score. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the method is not implemented by a subclass. |
Source code in hypertorch/nn/scorer.py
score_batch(candidate_nodes, hyperedge_index=None, node_to_neighbors=None)
abstractmethod
¶
Score a batch of hyperedges or nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidate_nodes
|
Tensor
|
Tensor containing node IDs to score of shape |
required |
hyperedge_index
|
Tensor | None
|
Optional tensor containing the hyperedge index. Defaults to |
None
|
node_to_neighbors
|
dict[int, Neighborhood] | None
|
Optional precomputed node-neighborhood mapping. Defaults to |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
scores |
Tensor
|
Score tensor with per-hyperedge or per-node scores. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the method is not implemented by a subclass. |