Data¶
hypertorch.data
¶
EnrichmentMode = Literal['concatenate', 'replace']
module-attribute
¶
Mode used to combine generated features with existing features.
HyperedgeAttrsEnricher = HyperedgeEnricher
module-attribute
¶
Type alias for enrichers that generate hyperedge attributes.
HyperedgeWeightsEnricher = HyperedgeEnricher
module-attribute
¶
Type alias for enrichers that generate hyperedge weights.
NegativeSamplingSchedule = Literal['first_epoch', 'every_n_epochs', 'every_epoch']
module-attribute
¶
Schedule controlling when negative samples are generated during training.
SamplingStrategy = SamplingStrategyEnum | SamplingStrategyLiteral
module-attribute
¶
Type for supported sampling strategies, either as an enum or a string literal.
SamplingStrategyLiteral = Literal['node', 'hyperedge']
module-attribute
¶
Literal type for supported sampling strategies.
Dataset
¶
Bases: Dataset
A dataset class for loading and processing hypergraph data.
Attributes:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
The hypergraph data stored by the dataset. |
sampling_strategy |
SamplingStrategy
|
The strategy used for sampling sub-hypergraphs. |
Source code in hypertorch/data/dataset.py
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 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 384 385 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 464 465 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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 | |
__init__(hdata=None, sampling_strategy=SamplingStrategyEnum.HYPEREDGE, task=TaskEnum.HYPERLINK_PREDICTION)
¶
Initialize the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdata
|
HData | None
|
The processed hypergraph data in HData format. |
None
|
sampling_strategy
|
SamplingStrategy
|
The strategy used for sampling sub-hypergraphs
(e.g., by node IDs or hyperedge IDs).
If not provided, defaults to |
HYPEREDGE
|
task
|
Task
|
Learning task used when the HData in input is not provided.
Defaults to |
HYPERLINK_PREDICTION
|
Source code in hypertorch/data/dataset.py
__len__()
¶
Return the number of sampleable items in the dataset.
Returns:
| Name | Type | Description |
|---|---|---|
length |
int
|
Number of sampleable nodes or hyperedges, depending on the sampling strategy. |
__getitem__(index)
¶
Sample a sub-hypergraph based on the sampling strategy and return it as HData.
If
- Sampling by node IDs, the sub-hypergraph will contain all hyperedges incident to the sampled nodes and all nodes incident to those hyperedges.
- Sampling by hyperedge IDs, the sub-hypergraph will contain all nodes incident to the sampled hyperedges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int | list[int]
|
An integer or a list of integers representing node or hyperedge IDs to sample, depending on the sampling strategy. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
An HData instance containing the sampled sub-hypergraph. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provided index is invalid (e.g., empty list or list length exceeds number of nodes/hyperedges). |
IndexError
|
If any node/hyperedge ID is out of bounds. |
Source code in hypertorch/data/dataset.py
from_hdata(hdata, sampling_strategy=SamplingStrategyEnum.HYPEREDGE, task=TaskEnum.HYPERLINK_PREDICTION)
classmethod
¶
Create a Dataset instance from an HData object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdata
|
HData
|
|
required |
sampling_strategy
|
SamplingStrategy
|
The sampling strategy to use for the dataset. If not provided,
defaults to |
HYPEREDGE
|
task
|
Task
|
Learning task used when the HData. If not provided,
defaults to |
HYPERLINK_PREDICTION
|
Returns:
| Name | Type | Description |
|---|---|---|
dataset |
Dataset
|
The |
Source code in hypertorch/data/dataset.py
from_url(url, sampling_strategy=SamplingStrategyEnum.HYPEREDGE, task=TaskEnum.HYPERLINK_PREDICTION, save_on_disk=False)
classmethod
¶
Create a Dataset instance by loading a hypergraph from a URL pointing to a .json or
.json.zst file in HIF format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL to the .json or .json.zst file containing the HIF hypergraph data.
sampling_strategy: The sampling strategy to use for the dataset. If not provided,
defaults to |
required |
task
|
Task
|
Learning task used when the HData. If not provided,
defaults to |
HYPERLINK_PREDICTION
|
save_on_disk
|
bool
|
Whether to save the downloaded file on disk. Defaults to |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dataset |
Dataset
|
The |
Source code in hypertorch/data/dataset.py
from_path(filepath, sampling_strategy=SamplingStrategyEnum.HYPEREDGE, task=TaskEnum.HYPERLINK_PREDICTION)
classmethod
¶
Create a Dataset instance by loading a hypergraph from a local file path pointing to a
.json or .json.zst file in HIF format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
The local file path to the .json or .json.zst file containing the HIF hypergraph data. |
required |
sampling_strategy
|
SamplingStrategyEnum
|
The sampling strategy to use for the dataset. If not provided,
defaults to |
HYPEREDGE
|
task
|
TaskEnum
|
Learning task used when the HData. If not provided,
defaults to |
HYPERLINK_PREDICTION
|
Returns:
| Name | Type | Description |
|---|---|---|
dataset |
Dataset
|
The |
Source code in hypertorch/data/dataset.py
enrich_node_features(enricher, enrichment_mode=None)
¶
Enrich node features using the provided node feature enricher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enricher
|
NodeEnricher
|
An instance of NodeEnricher to generate structural node features from hypergraph topology. |
required |
enrichment_mode
|
EnrichmentMode | None
|
How to combine generated features with existing |
None
|
Source code in hypertorch/data/dataset.py
enrich_node_features_from(dataset_with_features, node_space_setting='transductive', fill_value=None)
¶
Enrich node features from another dataset by copying features by global_node_ids.
Examples:
In a transductive setting, the full node space is preserved across datasets:
In inductive setting, missing node features can be filled with 0.0:
>>> test_dataset.enrich_node_features_from(
... train_dataset,
... node_space_setting="inductive",
... fill_value=0.0, # torch.tensor(0.0) also works and will be broadcast to the
... appropriate shape
... )
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_with_features
|
Dataset
|
Source dataset providing node features. |
required |
node_space_setting
|
NodeSpaceSetting
|
The setting for the node space, determining how nodes are handled.
|
'transductive'
|
fill_value
|
NodeSpaceFiller | None
|
Scalar or vector used to fill missing node features when
|
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the source dataset's node features cannot be aligned with the target dataset's nodes. |
Source code in hypertorch/data/dataset.py
enrich_hyperedge_attr(enricher, enrichment_mode=None)
¶
Enrich hyperedge attributes using the provided hyperedge feature enricher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enricher
|
HyperedgeEnricher
|
An instance of HyperedgeEnricher to generate structural hyperedge attributes from hypergraph topology. |
required |
enrichment_mode
|
EnrichmentMode | None
|
How to combine generated attributes with existing
|
None
|
Source code in hypertorch/data/dataset.py
enrich_hyperedge_weights(enricher, enrichment_mode=None)
¶
Enrich hyperedge weights using the provided hyperedge weight enricher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enricher
|
HyperedgeEnricher
|
An instance of HyperedgeEnricher to generate structural hyperedge weights from hypergraph topology. |
required |
enrichment_mode
|
EnrichmentMode | None
|
How to combine generated weights with existing
|
None
|
Source code in hypertorch/data/dataset.py
update_from_hdata(hdata)
¶
Create a Dataset instance from an HData object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdata
|
HData
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
dataset |
Dataset
|
The |
Source code in hypertorch/data/dataset.py
add_negative_samples(negative_sampler, seed=None)
¶
Create a new Dataset with sampled negative hyperedges added.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
negative_sampler
|
NegativeSampler
|
Sampler used to generate negative hyperedges from
this dataset's |
required |
seed
|
int | None
|
Optional random seed used for both negative sampling and the final shuffle.
Defaults to |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dataset |
Dataset
|
A new Dataset instance with positives and sampled negatives. |
Source code in hypertorch/data/dataset.py
remove_hyperedges_with_fewer_than_k_nodes(k, preserve_global_node_ids=False)
¶
Remove hyperedges that have fewer than k incident nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
The minimum number of nodes a hyperedge must have to be retained. |
required |
preserve_global_node_ids
|
bool
|
Whether to preserve the global node IDs
after removing hyperedges. Defaults to |
False
|
Source code in hypertorch/data/dataset.py
split(ratios=None, shuffle=False, seed=None, node_space_setting='transductive', sparse_split_hyperedges=False, cover_all_nodes_in_train_split=False, train_split_idx=0, splitter=None)
¶
Split the dataset based on task into partitions: - For node classification, splits are based on nodes and their incident hyperedges. - For hyperlink prediction, splits are based on hyperedges and their incident nodes.
Boundaries are computed using cumulative floor to prevent early splits from
over-consuming edges. The last split absorbs any rounding remainder.
In the transductive setting, node-classification splits keep the full node space on
the train split. Hyperlink-prediction splits keep the full hypergraph as context by
default and mark supervised hyperedges with target_hyperedge_mask.
Splits that would end with zero hyperedges are rejected.
Use split_with_ratios to also get the final ratios after splitting.
Examples:
Transductive split:
>>> train, test = dataset.split([0.8, 0.2])
>>> train.hdata.num_nodes == dataset.hdata.num_nodes
>>> int(train.hdata.target_hyperedge_mask.sum().item()) == len(train)
Inductive split:
>>> train, test = dataset.split(
... [0.8, 0.2],
... node_space_setting="inductive",
... )
>>> train.hdata.num_nodes <= dataset.hdata.num_nodes
>>> test.hdata.num_nodes <= dataset.hdata.num_nodes
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ratios
|
list[float] | None
|
List of floats summing to |
None
|
shuffle
|
bool | None
|
Whether to shuffle hyperedges before splitting.
Defaults to |
False
|
seed
|
int | None
|
Optional random seed for reproducibility. Ignored if shuffle is set to |
None
|
node_space_setting
|
NodeSpaceSetting
|
Whether to preserve the full node space in the splits.
|
'transductive'
|
sparse_split_hyperedges
|
bool
|
Whether hyperlink-prediction splits should use the
sparse split behavior. Defaults to |
False
|
cover_all_nodes_in_train_split
|
bool
|
Whether a transductive sparse hyperedge split should move hyperedges from later splits until every node is incident to one of its selected hyperedges. Ratios are approximate when this coverage requires moving hyperedges into the first split. |
False
|
train_split_idx
|
int
|
The index of the split to treat as the train split. Defaults to |
0
|
splitter
|
Splitter[Dataset, Any] | None
|
Optional dataset splitter. When provided, it owns split
construction and final-ratio reporting. Defaults to |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
datasets |
list[Dataset]
|
List of Dataset objects, one per split, each with contiguous IDs. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If ratios do not sum to |
Source code in hypertorch/data/dataset.py
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 384 385 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 | |
split_with_ratios(ratios, shuffle=False, seed=None, node_space_setting='transductive', cover_all_nodes_in_train_split=False, train_split_idx=0, sparse_split_hyperedges=False)
¶
Split the dataset based on task and return the final hyperedge ratios:
- For node classification, splits are based on nodes and their incident hyperedges.
For more details, look at the NodeDatasetSplitter class.
- For hyperlink prediction, splits are based on hyperedges and their incident nodes.
For more details, look at the HyperedgeDatasetSplitter class.
Boundaries are computed using cumulative floor to prevent early splits from
over-consuming edges. The last split absorbs any rounding remainder.
In the transductive setting, node-classification splits keep the full node space on
the train split. Hyperlink-prediction splits keep the full hypergraph as context by
default and mark supervised hyperedges with target_hyperedge_mask.
Splits that would end with zero hyperedges are rejected.
Final ratios are computed from split hyperedge counts after ratio boundaries and any requested transductive rebalancing have been applied.
To provide a custom splitting implementation, use the splitter
argument of the split method instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ratios
|
list[float]
|
List of floats summing to |
required |
shuffle
|
bool | None
|
Whether to shuffle hyperedges before splitting. Defaults to
|
False
|
node_space_setting
|
NodeSpaceSetting
|
Whether to preserve the full node space in the
splits. |
'transductive'
|
sparse_split_hyperedges
|
bool
|
Whether hyperlink-prediction splits should use the legacy
sparse materialization behavior. Defaults to |
False
|
cover_all_nodes_in_train_split
|
bool
|
Whether a transductive sparse hyperedge split should move hyperedges from later splits until every node is incident to one of its selected hyperedges. |
False
|
train_split_idx
|
int
|
The index of the split to treat as the train split. Defaults to |
0
|
seed
|
int | None
|
Optional random seed for reproducibility. Ignored if |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
datasets |
list[Dataset]
|
List of Dataset instances, one per split, each with contiguous IDs. |
final_ratios |
list[float]
|
List of floats representing the actual ratios of target hyperedges in each split after splitting and any requested rebalancing. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If ratios do not sum to |
Source code in hypertorch/data/dataset.py
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 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 | |
to(device)
¶
Move the dataset's HData to the specified device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device
|
The target device (e.g., |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dataset |
Dataset
|
The Dataset instance moved to the specified device. |
Source code in hypertorch/data/dataset.py
transform_node_attrs(attrs, attr_keys=None)
¶
Transform HIF node attributes into a numeric tensor. Overload this in case processing node attributes requires custom logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs
|
dict[str, Any]
|
Attributes to transform. |
required |
attr_keys
|
list[str] | None
|
Optional attribute key order used for consistent output shape.
Defaults to |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
attrs |
Tensor
|
Tensor containing numeric attribute values. |
Source code in hypertorch/data/dataset.py
transform_hyperedge_attrs(attrs, attr_keys=None)
¶
Transform hyperedge attributes into a numeric tensor. Overload this in case processing hyperedge attributes requires custom logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs
|
dict[str, Any]
|
Attributes to transform. |
required |
attr_keys
|
list[str] | None
|
Optional attribute key order used for consistent output shape.
Defaults to |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
attrs |
Tensor
|
Tensor containing numeric attribute values. |
Source code in hypertorch/data/dataset.py
stats()
¶
Compute statistics for the dataset.
This method currently delegates to the underlying HData's stats method.
Fields
shape_x: The shape of the node feature matrixx.shape_hyperedge_attr: The shape of the hyperedge attribute matrix, orNoneif hyperedge attributes are not present.num_nodes: The number of nodes in the hypergraph.num_hyperedges: The number of hyperedges in the hypergraph.avg_degree_node_raw: The average degree of nodes, calculated as the mean number of hyperedges each node belongs to.avg_degree_node: The floored node average degree.avg_degree_hyperedge_raw: The average size of hyperedges, calculated as the mean number of nodes each hyperedge contains.avg_degree_hyperedge: The floored hyperedge average size.node_degree_max: The maximum degree of any node in the hypergraph.hyperedge_degree_max: The maximum size of any hyperedge in the hypergraph.node_degree_median: The median degree of nodes in the hypergraph.hyperedge_degree_median: The median size of hyperedges in the hypergraph.distribution_node_degree: A list where the value at indexirepresents the count of nodes with degreei.distribution_hyperedge_size: A list where the value at indexirepresents the count of hyperedges with sizei.distribution_node_degree_hist: A dictionary where the keys are node degrees and the values are the count of nodes with that degree.distribution_hyperedge_size_hist: A dictionary where the keys are hyperedge sizes and the values are the count of hyperedges with that size.
Returns:
| Name | Type | Description |
|---|---|---|
stats |
dict[str, Any]
|
A dictionary containing various statistics about the hypergraph. |
Source code in hypertorch/data/dataset.py
HIFLoader
¶
A utility class to load hypergraphs from HIF format.
Source code in hypertorch/data/hif.py
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 384 385 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 464 465 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 | |
load_from_url(url, task=TaskEnum.HYPERLINK_PREDICTION, save_on_disk=False)
classmethod
¶
Load a hypergraph from a given URL pointing to a .json or .json.zst file in HIF format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL to the .json or .json.zst file containing the HIF hypergraph data. |
required |
task
|
Task
|
The learning task for the loaded hypergraph. |
HYPERLINK_PREDICTION
|
save_on_disk
|
bool
|
Whether to save the downloaded file on disk. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
The loaded hypergraph object. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the URL cannot be downloaded, has an unsupported file format, or has an unexpected filename format. |
Source code in hypertorch/data/hif.py
load_from_path(filepath, task=TaskEnum.HYPERLINK_PREDICTION)
classmethod
¶
Load a hypergraph from a local file path pointing to a .json or .json.zst file in HIF format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
The local file path to the .json or .json.zst file containing the HIF hypergraph data. |
required |
task
|
Task
|
The learning task for the loaded hypergraph. |
HYPERLINK_PREDICTION
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
The loaded hypergraph object. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in hypertorch/data/hif.py
load_by_name(dataset_name, hf_sha=None, task=TaskEnum.HYPERLINK_PREDICTION, save_on_disk=False)
classmethod
¶
Load a supported dataset by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
str
|
Name of the dataset to load. |
required |
task
|
Task
|
Task type for the dataset. Defaults to "hyperlink-prediction". |
HYPERLINK_PREDICTION
|
hf_sha
|
str | None
|
Optional pinned Hugging Face revision used as a fallback source. |
None
|
save_on_disk
|
bool
|
Whether to cache the downloaded compressed dataset file.
Defaults to |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
Loaded hypergraph data. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the dataset cannot be downloaded or parsed. |
Source code in hypertorch/data/hif.py
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 464 465 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 | |
__process_hif_data(hif_data, dataset_name=None, task=TaskEnum.HYPERLINK_PREDICTION)
classmethod
¶
Validate and process parsed HIF data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hif_data
|
dict[str, Any]
|
Parsed HIF JSON data. |
required |
dataset_name
|
str | None
|
Optional dataset name used in validation errors. |
None
|
task
|
Task
|
Task type for the dataset. Defaults to "hyperlink-prediction". |
HYPERLINK_PREDICTION
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
Processed hypergraph data. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the data is not HIF-compliant. |
Source code in hypertorch/data/hif.py
HIFProcessor
¶
A utility class to process HIF hypergraph data into HData format.
Source code in hypertorch/data/hif.py
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 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 | |
transform_attrs(attrs, attr_keys=None)
staticmethod
¶
Extract and encode numeric attributes to tensor.
Non-numeric attributes are discarded. Missing attributes are filled with 0.0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs
|
dict[str, Any]
|
Dictionary of attributes |
required |
attr_keys
|
list[str] | None
|
Optional list of attribute keys to encode. If provided,
ensures consistent ordering and fill missing with |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
attrs |
Tensor
|
Tensor of numeric attribute values |
Source code in hypertorch/data/hif.py
process_hypergraph(hypergraph, task=TaskEnum.HYPERLINK_PREDICTION)
classmethod
¶
Process the loaded hypergraph into HData format, mapping HIF structure to tensors.
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
The processed hypergraph data. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If HIF node IDs are not unique or an incidence references an undeclared node ID. |
Source code in hypertorch/data/hif.py
__collect_attr_keys(attr_keys)
classmethod
¶
Collect unique numeric attribute keys from a list of attribute dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attr_keys
|
list[dict[str, Any]]
|
List of attribute dictionaries. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
attr_keys |
list[str]
|
List of unique numeric attribute keys. |
Source code in hypertorch/data/hif.py
__process_hyperedge_attr(hypergraph, hyperedge_id_to_idx, num_hyperedges)
classmethod
¶
Build the hyperedge attribute matrix from HIF hyperedge attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hypergraph
|
HIFHypergraph
|
HIF hypergraph to process. |
required |
hyperedge_id_to_idx
|
dict[Any, int]
|
Mapping from HIF hyperedge IDs to contiguous indices. |
required |
num_hyperedges
|
int
|
Number of hyperedges in the processed data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
hyperedge_attr |
Tensor | None
|
Hyperedge attribute tensor, or |
Source code in hypertorch/data/hif.py
__process_x(hypergraph, num_nodes)
classmethod
¶
Build the node feature matrix from HIF node attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hypergraph
|
HIFHypergraph
|
HIF hypergraph to process. |
required |
num_nodes
|
int
|
Number of nodes in the processed data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
x |
Tensor
|
Node feature matrix. |
Source code in hypertorch/data/hif.py
__process_hyperedge_weights(hypergraph, hyperedge_id_to_idx, num_hyperedges)
classmethod
¶
Build hyperedge weights from HIF hyperedge attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hypergraph
|
HIFHypergraph
|
HIF hypergraph to process. |
required |
hyperedge_id_to_idx
|
dict[Any, int]
|
Mapping from HIF hyperedge IDs to contiguous indices. |
required |
num_hyperedges
|
int
|
Number of hyperedges in the processed data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
hyperedge_weights |
Tensor | None
|
Hyperedge weight tensor, or |
Source code in hypertorch/data/hif.py
AlgebraDataset
¶
Bases: _PreloadedDataset
Preloaded algebra dataset.
Source code in hypertorch/data/supported_datasets.py
AmazonDataset
¶
Bases: _PreloadedDataset
Preloaded Amazon reviews dataset.
Source code in hypertorch/data/supported_datasets.py
ContactHighSchoolDataset
¶
Bases: _PreloadedDataset
Preloaded high-school contact network dataset.
Source code in hypertorch/data/supported_datasets.py
ContactPrimarySchoolDataset
¶
Bases: _PreloadedDataset
Preloaded primary-school contact network dataset.
Source code in hypertorch/data/supported_datasets.py
CoraDataset
¶
Bases: _PreloadedDataset
Preloaded Cora citation dataset.
Source code in hypertorch/data/supported_datasets.py
CourseraDataset
¶
Bases: _PreloadedDataset
Preloaded Coursera dataset.
Source code in hypertorch/data/supported_datasets.py
DBLPDataset
¶
Bases: _PreloadedDataset
Preloaded DBLP co-authorship dataset.
Source code in hypertorch/data/supported_datasets.py
EmailEnronDataset
¶
Bases: _PreloadedDataset
Preloaded Enron email dataset.
Source code in hypertorch/data/supported_datasets.py
EmailW3CDataset
¶
Bases: _PreloadedDataset
Preloaded W3C email dataset.
Source code in hypertorch/data/supported_datasets.py
GeometryDataset
¶
Bases: _PreloadedDataset
Preloaded geometry dataset.
Source code in hypertorch/data/supported_datasets.py
GOTDataset
¶
Bases: _PreloadedDataset
Preloaded Game of Thrones dataset.
Source code in hypertorch/data/supported_datasets.py
IMDBDataset
¶
Bases: _PreloadedDataset
Preloaded IMDB dataset.
Source code in hypertorch/data/supported_datasets.py
MusicBluesReviewsDataset
¶
Bases: _PreloadedDataset
Preloaded music blues reviews dataset.
Source code in hypertorch/data/supported_datasets.py
NBADataset
¶
Bases: _PreloadedDataset
Preloaded NBA dataset.
Source code in hypertorch/data/supported_datasets.py
NDCClassesDataset
¶
Bases: _PreloadedDataset
Preloaded NDC classes dataset.
Source code in hypertorch/data/supported_datasets.py
NDCSubstancesDataset
¶
Bases: _PreloadedDataset
Preloaded NDC substances dataset.
Source code in hypertorch/data/supported_datasets.py
PatentDataset
¶
Bases: _PreloadedDataset
Preloaded patent dataset.
Source code in hypertorch/data/supported_datasets.py
PubmedDataset
¶
Bases: _PreloadedDataset
Preloaded PubMed dataset.
Source code in hypertorch/data/supported_datasets.py
RestaurantReviewsDataset
¶
Bases: _PreloadedDataset
Preloaded restaurant reviews dataset.
Source code in hypertorch/data/supported_datasets.py
ThreadsAskUbuntuDataset
¶
Bases: _PreloadedDataset
Preloaded Ask Ubuntu thread dataset.
Source code in hypertorch/data/supported_datasets.py
ThreadsMathsxDataset
¶
Bases: _PreloadedDataset
Preloaded Math StackExchange thread dataset.
Source code in hypertorch/data/supported_datasets.py
TwitterDataset
¶
Bases: _PreloadedDataset
Preloaded Twitter dataset.
Source code in hypertorch/data/supported_datasets.py
VegasBarsReviewsDataset
¶
Bases: _PreloadedDataset
Preloaded Vegas bars reviews dataset.
Source code in hypertorch/data/supported_datasets.py
DataLoader
¶
Bases: DataLoader
DataLoader combines a dataset and a sampler, and provides an iterable
over the given dataset. It extends torch.utils.data.DataLoader.
Source code in hypertorch/data/loader.py
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 | |
__init__(dataset, batch_size=1, shuffle=False, sample_full_hypergraph=False, drop_last=False, num_workers=0, persistent_workers=False, collate_fn=None, generator=None, **kwargs)
¶
Initialize the data loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
Dataset that provides sampled |
required |
batch_size
|
int
|
Number of samples per batch. Ignored when
|
1
|
shuffle
|
bool | None
|
Whether to reshuffle sample indices at every epoch. |
False
|
sample_full_hypergraph
|
bool
|
Whether each collated batch should ignore the sampled
mini-batch subgraphs and return a clone of the dataset's full |
False
|
drop_last
|
bool
|
Whether to drop the final incomplete batch when the dataset size
is not divisible by the batch size. If |
False
|
num_workers
|
int
|
Optional number of subprocesses to use for data loading.
For instance, |
0
|
persistent_workers
|
bool
|
Whether worker processes should stay alive after a dataset has
been consumed once. If |
False
|
collate_fn
|
Callable[[list[HData]], HData] | None
|
Optional custom collate function. When |
None
|
generator
|
Generator | None
|
Optional random generator used by the underlying Torch data loader. |
None
|
kwargs
|
Any
|
sampler: Defines the strategy to draw samples from the dataset.
Can be any |
{}
|
Source code in hypertorch/data/loader.py
collate(batch)
¶
Collates a list of HData objects into a single batched HData object.
This function combines multiple separate samples into a single batched representation suitable for mini-batch training.
Handles
- Concatenating node features from all samples.
- Concatenating and offsetting hyperedges from all samples.
- Concatenating hyperedge attributes from all samples, if present.
- Concatenating hyperedge weights from all samples, if present.
Examples:
Given batch = [HData_0, HData_1]:
For node features:
>>> HData_0.x.shape # (3, 64) — 3 nodes with 64 features
>>> HData_1.x.shape # (2, 64) — 2 nodes with 64 features
>>> x.shape # (5, 64) — all 5 nodes concatenated
For hyperedge index:
HData_0(3 nodes, 2 hyperedges):
>>> hyperedge_index = [[0, 1, 1, 2], # Nodes 0, 1, 1, 2
... [0, 0, 1, 1]] # HE 0 contains {0,1}, HE 1 contains {1,2}
HData_1(2 nodes, 1 hyperedge):
Batched result:
>>> hyperedge_index = [[0, 1, 1, 2, 3, 4], # Node indices: original then offset by 3
... [0, 0, 1, 1, 2, 2]] # Hyperedge IDs: original then offset by 2
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
list[HData]
|
List of |
required |
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
A single |
Source code in hypertorch/data/loader.py
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 | |
__collate_y_and_target_masks_for_task(batch, hyperedge_index_wrapper)
¶
Collates the labels (y) and target masks for a batch of HData instances based on the task type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
list[HData]
|
List of HData instances containing the data to collate. |
required |
hyperedge_index_wrapper
|
HyperedgeIndex
|
A HyperedgeIndex wrapping the collated hyperedge index. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
collated_y |
Tensor
|
A tensor containing the collated labels for the batch. |
collated_target_node_mask |
Tensor | None
|
A tensor containing the collated target node mask
for the batch, or |
collated_target_hyperedge_mask |
Tensor | None
|
A tensor containing the collated target hyperedge
mask for the batch, or |
Source code in hypertorch/data/loader.py
ABHyperedgeWeightsEnricher
¶
Bases: HyperedgeWeightsEnricher
Generates hyperedge weights based on the number of nodes in each hyperedge.
Attributes:
| Name | Type | Description |
|---|---|---|
alpha |
float
|
Scaling factor for the random component added to weights.
Must be between |
beta |
float | None
|
If provided, the random component is alpha * beta.
If |
Source code in hypertorch/data/enricher.py
__init__(cache_dir=None, alpha=1.0, beta=None)
¶
Initialize the hyperedge weight enricher.
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. |
1.0
|
beta
|
float | None
|
If provided, the random component is |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in hypertorch/data/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:
| Name | Type | Description |
|---|---|---|
hyperedge_weight |
Tensor
|
Tensor of shape |
Source code in hypertorch/data/enricher.py
FillValueHyperedgeAttrsEnricher
¶
Bases: HyperedgeAttrsEnricher
Generates simple hyperedge attributes by filling them with a constant value.
Attributes:
| Name | Type | Description |
|---|---|---|
fill_value |
float
|
The constant value to fill the hyperedge attributes with. Defaults to |
Source code in hypertorch/data/enricher.py
__init__(cache_dir=None, fill_value=1.0)
¶
Initialize the fill-value hyperedge attribute enricher.
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. |
1.0
|
Source code in hypertorch/data/enricher.py
enrich(hyperedge_index)
¶
Generate hyperedge attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_index
|
Tensor
|
Hyperedge index tensor of shape |
required |
Returns:
| Name | Type | Description |
|---|---|---|
hyperedge_attr |
Tensor
|
Tensor of shape |
Source code in hypertorch/data/enricher.py
HyperedgeEnricher
¶
LaplacianPositionalEncodingEnricher
¶
Bases: NodeEnricher
Enrich node features with Laplacian Positional Encodings computed from the symmetric normalized Laplacian of the clique expansion of the hypergraph.
Attributes:
| Name | Type | Description |
|---|---|---|
num_features |
int
|
Number of positional encoding features to generate for each node. |
num_nodes |
int
|
Total number of nodes in the graph. If not provided, it will be inferred
from |
Source code in hypertorch/data/enricher.py
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 | |
__init__(num_features, num_nodes=0, cache_dir=None)
¶
Initialize the Laplacian positional encoding enricher.
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 is inferred from |
0
|
cache_dir
|
str | None
|
Optional directory to cache computed features.
If |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in hypertorch/data/enricher.py
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:
| Name | Type | Description |
|---|---|---|
node_features |
Tensor
|
Tensor of shape |
Source code in hypertorch/data/enricher.py
Node2VecEnricher
¶
Bases: NodeEnricher
Enrich node features using Node2Vec embeddings computed from the clique expansion of the hypergraph.
Attributes:
| Name | Type | Description |
|---|---|---|
embedding_dim |
int
|
Dimensionality of the node embeddings to generate. |
walk_length |
int
|
Length of each random walk. |
context_size |
int
|
Window size for the skip-gram model
(number of neighbors in the walk considered as context).
For example, if |
num_walks_per_node |
int
|
Number of random walks to start at each node. |
p |
float
|
Return hyperparameter for Node2Vec. Default is |
q |
float
|
In-out hyperparameter for Node2Vec. Default is |
num_negative_samples |
int
|
Number of negative samples used for skip-gram training.
If set to |
num_nodes |
int
|
Total number of nodes to preserve. If not provided, it will be inferred from
|
graph_reduction_strategy |
GraphReductionStrategy
|
Strategy for reducing the hyperedge graph.
Defaults to |
num_epochs |
int
|
Number of epochs used to optimize Node2Vec embeddings. Defaults to |
learning_rate |
float
|
Learning rate for embedding optimization. Defaults to |
batch_size |
int
|
Batch size used by the random-walk loader. Defaults to |
sparse |
bool
|
Whether Node2Vec embeddings should use sparse gradients. |
verbose |
bool
|
Whether to print verbose output during training. Defaults to |
Source code in hypertorch/data/enricher.py
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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 | |
__init__(num_features, walk_length=20, context_size=10, num_walks_per_node=10, p=1.0, q=1.0, num_negative_samples=1, num_nodes=0, graph_reduction_strategy=GraphReductionStrategyEnum.CLIQUE_EXPANSION, num_epochs=5, learning_rate=0.01, batch_size=128, sparse=True, cache_dir=None, verbose=False)
¶
Initialize the Node2Vec enricher.
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. 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. |
1.0
|
q
|
float
|
In-out hyperparameter for Node2Vec. |
1.0
|
num_negative_samples
|
int
|
Number of negative samples to use for skip-gram training. |
1
|
num_nodes
|
int
|
Total number of nodes in the graph. If not provided, it is inferred from
|
0
|
graph_reduction_strategy
|
GraphReductionStrategy
|
Strategy for reducing the hypergraph. |
CLIQUE_EXPANSION
|
num_epochs
|
int
|
Number of epochs used to optimize Node2Vec embeddings. |
5
|
learning_rate
|
float
|
Learning rate for embedding optimization. |
0.01
|
batch_size
|
int
|
Batch size used by the random-walk loader. |
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. |
False
|
Source code in hypertorch/data/enricher.py
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:
| Name | Type | Description |
|---|---|---|
x |
Tensor
|
Tensor of shape |
Source code in hypertorch/data/enricher.py
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 | |
__validate()
¶
Validate Node2Vec enrichment configuration.
Raises:
| Type | Description |
|---|---|
ValueError
|
If any configuration value is outside its supported range. |
Source code in hypertorch/data/enricher.py
NodeEnricher
¶
VilLainEnricher
¶
Bases: _VilLainTrainer, NodeEnricher
Enrich node features with VilLain embeddings learned from hypergraph topology.
Source code in hypertorch/data/enricher.py
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 | |
__init__(num_features, num_nodes=0, num_hyperedges=0, labels_per_subspace=2, training_steps=4, generation_steps=100, tau=1.0, eps=1e-10, num_epochs=5, learning_rate=0.01, weight_decay=0.0, cache_dir=None, verbose=False)
¶
Initialize the VilLain node feature enricher.
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 missing
from |
0
|
num_hyperedges
|
int
|
Total number of hyperedges, including empty hyperedges missing
from |
0
|
labels_per_subspace
|
int
|
Number of virtual labels per subspace. |
2
|
training_steps
|
int
|
Propagation steps used for VilLain self-supervised loss. |
4
|
generation_steps
|
int
|
Propagation steps averaged for final embeddings. |
100
|
tau
|
float
|
Gumbel-Softmax temperature. |
1.0
|
eps
|
float
|
Numerical stability constant. |
1e-10
|
num_epochs
|
int
|
Number of epochs used to optimize VilLain embeddings. |
5
|
learning_rate
|
float
|
Learning rate for embedding optimization. |
0.01
|
weight_decay
|
float
|
Weight decay for the optimizer. |
0.0
|
cache_dir
|
str | None
|
Optional directory to cache computed features. If |
None
|
verbose
|
bool
|
Whether to print verbose output during training. |
False
|
Source code in hypertorch/data/enricher.py
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:
| Name | Type | Description |
|---|---|---|
node_embeddings |
Tensor
|
Tensor of shape |
Source code in hypertorch/data/enricher.py
VilLainHyperedgeAttrsEnricher
¶
Bases: _VilLainTrainer, HyperedgeAttrsEnricher
Enrich hyperedge attributes with VilLain embeddings learned from hypergraph topology.
Source code in hypertorch/data/enricher.py
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 384 385 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 | |
__init__(num_features, num_nodes=0, num_hyperedges=0, labels_per_subspace=2, training_steps=4, generation_steps=100, tau=1.0, eps=1e-10, num_epochs=5, learning_rate=0.01, weight_decay=0.0, cache_dir=None, verbose=False)
¶
Initialize the VilLain hyperedge attribute enricher.
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 missing from
|
0
|
num_hyperedges
|
int
|
Total number of hyperedges, including empty hyperedges missing
from |
0
|
labels_per_subspace
|
int
|
Number of virtual labels per subspace. |
2
|
training_steps
|
int
|
Propagation steps used for VilLain self-supervised loss. |
4
|
generation_steps
|
int
|
Propagation steps averaged for final embeddings. |
100
|
tau
|
float
|
Gumbel-Softmax temperature. |
1.0
|
eps
|
float
|
Numerical stability constant. |
1e-10
|
num_epochs
|
int
|
Number of epochs used to optimize VilLain embeddings. |
5
|
learning_rate
|
float
|
Learning rate for embedding optimization. |
0.01
|
weight_decay
|
float
|
Weight decay for the optimizer. |
0.0
|
cache_dir
|
str | None
|
Optional directory to cache computed features. If |
None
|
verbose
|
bool
|
Whether to print verbose output during training. |
False
|
Source code in hypertorch/data/enricher.py
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:
| Name | Type | Description |
|---|---|---|
hyperedge_embeddings |
Tensor
|
Tensor of shape |
Source code in hypertorch/data/enricher.py
CliqueNegativeSampler
¶
Bases: SameNodeSpaceNegativeSampler
Sample negative hyperedges that are cliques in the underlying graph.
The underlying graph is obtained through clique expansion: two nodes are adjacent when
they co-occur in at least one positive hyperedge. A sampled negative hyperedge contains
num_nodes_per_sample nodes where every pair is adjacent, and the node set must not
already exist as a positive hyperedge.
Attributes:
| Name | Type | Description |
|---|---|---|
num_negative_samples |
int
|
Number of negative hyperedges to generate. |
num_nodes_per_sample |
int
|
Number of nodes per negative hyperedge. Must be at least 2. |
max_candidates |
int | None
|
Optional upper bound for full-size clique candidates enumerated
during search. If |
Source code in hypertorch/data/negative_sampler.py
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 | |
__init__(num_negative_samples, num_nodes_per_sample, hyperedge_attr_enricher=None, hyperedge_weights_enricher=None, return_0based_negatives=False, max_candidates=None)
¶
Initialize the clique negative sampler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_negative_samples
|
int
|
Number of negative hyperedges to generate. |
required |
num_nodes_per_sample
|
int
|
Number of nodes per negative hyperedge. Must be at least 2. |
required |
hyperedge_attr_enricher
|
HyperedgeAttrsEnricher | None
|
Optional enricher to generate attributes for sampled negatives. |
None
|
hyperedge_weights_enricher
|
HyperedgeWeightsEnricher | None
|
Optional enricher to generate weights for sampled negatives. |
None
|
return_0based_negatives
|
bool
|
If |
False
|
max_candidates
|
int | None
|
Optional upper bound for full-size clique candidates enumerated
during search. If |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If numeric arguments are invalid. |
Source code in hypertorch/data/negative_sampler.py
sample(hdata, seed=None)
¶
Generate clique-based negative hyperedges from the input hypergraph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdata
|
HData
|
Input hypergraph data. |
required |
seed
|
int | None
|
Optional random seed for reproducible candidate selection. Defaults to |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
A new |
Raises:
| Type | Description |
|---|---|
ValueError
|
If too few nodes or valid clique negatives are available. |
Source code in hypertorch/data/negative_sampler.py
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 | |
__expand_clique_candidates(prefix, candidates, adjacency_list, positive_hyperedge_signatures, valid_candidates, enumerated_candidates)
¶
Recursively enumerate clique candidates from a clique-expanded adjacency list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
tuple[int, ...]
|
Current partial clique, represented as sorted node IDs. |
required |
candidates
|
list[int]
|
Node IDs that may extend |
required |
adjacency_list
|
list[set[int]]
|
Clique-expanded graph adjacency list. |
required |
positive_hyperedge_signatures
|
set[tuple[int, ...]]
|
Positive hyperedge node signatures that must not be returned as negatives. |
required |
valid_candidates
|
list[tuple[int, ...]]
|
Output list mutated in place with valid negative clique candidates. |
required |
enumerated_candidates
|
int
|
Number of full-size clique candidates visited so far. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
visited |
int
|
Updated number of full-size clique candidates visited. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in hypertorch/data/negative_sampler.py
__find_valid_clique_candidates(adjacency_list, positive_hyperedge_signatures)
¶
Find valid clique negative candidates in the clique-expanded graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adjacency_list
|
list[set[int]]
|
Clique-expanded graph adjacency list. |
required |
positive_hyperedge_signatures
|
set[tuple[int, ...]]
|
Positive hyperedge node signatures with the requested sample size. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
candidates |
list[tuple[int, ...]]
|
Clique node signatures that are not positive hyperedges. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer valid clique negatives exist than requested, or if
|
Source code in hypertorch/data/negative_sampler.py
__sample_loop(hdata, clique_candidates, seed=None)
¶
Sample from valid clique candidates and build negative hyperedge tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdata
|
HData
|
Input hypergraph data used for feature, attribute, and ID context. |
required |
clique_candidates
|
list[tuple[int, ...]]
|
Valid clique negative candidates to sample from. |
required |
seed
|
int | None
|
Optional seed for reproducible candidate shuffling and random attributes. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
sampled_hyperedge_indexes |
list[Tensor]
|
sampled hyperedge index tensors |
sampled_hyperedge_attrs |
list[Tensor]
|
sampled hyperedge attribute tensors. |
sampled_negative_node_ids |
set[int]
|
sampled negative node IDs. |
new_hyperedge_id_offset |
int
|
first negative hyperedge ID. |
Source code in hypertorch/data/negative_sampler.py
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 | |
GeneratedNodesNegativeSampler
¶
Bases: NegativeSampler, ABC
Base class for negative samplers that generate new nodes instead of sampling from existing ones.
Attributes:
| Name | Type | Description |
|---|---|---|
node_feature_enricher |
NodeEnricher
|
Enricher used to generate features for new nodes. |
hyperedge_attr_enricher |
HyperedgeAttrsEnricher | None
|
Optional enricher used to generate attributes for sampled hyperedges. |
hyperedge_weights_enricher |
HyperedgeWeightsEnricher | None
|
Optional enricher used to generate weights for sampled hyperedges. |
Source code in hypertorch/data/negative_sampler.py
__init__(node_feature_enricher, hyperedge_attr_enricher=None, hyperedge_weights_enricher=None, return_0based_negatives=False)
¶
Initialize the generated-node negative sampler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_feature_enricher
|
NodeEnricher
|
Enricher used to generate features for new nodes. |
required |
hyperedge_attr_enricher
|
HyperedgeAttrsEnricher | None
|
Optional enricher used to generate attributes for sampled hyperedges. |
None
|
hyperedge_weights_enricher
|
HyperedgeWeightsEnricher | None
|
Optional enricher used to generate weights for sampled hyperedges. |
None
|
return_0based_negatives
|
bool
|
If |
False
|
Source code in hypertorch/data/negative_sampler.py
NegativeSampler
¶
Bases: ABC
Abstract base class for negative samplers.
Attributes:
| Name | Type | Description |
|---|---|---|
return_0based_negatives |
bool
|
|
Source code in hypertorch/data/negative_sampler.py
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 247 248 249 250 251 252 253 254 255 256 257 | |
__init__(return_0based_negatives=False)
¶
Initialize the negative sampler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
return_0based_negatives
|
bool
|
If |
False
|
Source code in hypertorch/data/negative_sampler.py
sample(hdata, seed=None)
abstractmethod
¶
Abstract method for negative sampling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdata
|
HData
|
The input data object containing graph or hypergraph information. |
required |
seed
|
int | None
|
Optional random seed for reproducible negative sampling. Defaults to |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
The negative samples as a new |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the method is not implemented in a subclass. |
Source code in hypertorch/data/negative_sampler.py
RandomNegativeSampler
¶
Bases: SameNodeSpaceNegativeSampler
A random negative sampler. Negatives generated with return_0based_negatives = False
aren't usable standalone as they have global node and hyperedge IDs. They must be concatenated
with the original HData object that is provided as input to the sample method, as it
contains the global node and hyperedge IDs and features that can be indexed with
the negative samples' IDs.
Attributes:
| Name | Type | Description |
|---|---|---|
num_negative_samples |
int
|
Number of negative hyperedges to generate. |
num_nodes_per_sample |
int
|
Number of nodes per negative hyperedge. |
max_retry |
int
|
Maximum number of rejected sampling attempts allowed per requested negative hyperedge before failing. |
Source code in hypertorch/data/negative_sampler.py
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 384 385 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 464 465 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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 | |
__init__(num_negative_samples, num_nodes_per_sample, hyperedge_attr_enricher=None, hyperedge_weights_enricher=None, return_0based_negatives=False, max_retry=100)
¶
Initialize the random negative sampler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_negative_samples
|
int
|
Number of negative hyperedges to generate. |
required |
num_nodes_per_sample
|
int
|
Number of nodes per negative hyperedge. |
required |
hyperedge_attr_enricher
|
HyperedgeAttrsEnricher | None
|
An optional |
None
|
hyperedge_weights_enricher
|
HyperedgeWeightsEnricher | None
|
An optional |
None
|
return_0based_negatives
|
bool
|
If |
False
|
max_retry
|
int
|
Maximum number of rejected sampling attempts allowed per requested
negative hyperedge before failing. If |
100
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any numeric argument is not positive. |
Source code in hypertorch/data/negative_sampler.py
sample(hdata, seed=None)
¶
Generate negative hyperedges by randomly sampling unique node IDs.
Node IDs are sampled from the same node space as the input data, and the new negative
hyperedge IDs start from the original number of hyperedges in the input data to
avoid ID conflicts. The resulting negative samples are returned as a new HData object
with remapped 0-based node and hyperedge IDs, if self.return_0based_negatives == True.
Otherwise, the negative samples retain their original global node and hyperedge IDs
from the input data.
Examples:
With self.return_0based_negatives = True:
>>> num_negative_samples = 2
>>> num_nodes_per_sample = 3
>>> negative_hyperedge_index = [[0, 0, 1, 2, 3, 4],
... [0, 1, 1, 0, 1, 0]]
The negative hyperedge 0 connects nodes 0, 2, 3. The second negative hyperedge 1 connects nodes 0, 1, 4.
>>> negative_x = data.x[[0, 1, 2, 3, 4]]
>>> negative_hyperedge_attr = random_attributes_for_2_negative_hyperedges
With self.return_0based_negatives = False:
>>> num_negative_samples = 2
>>> num_nodes_per_sample = 3
>>> negative_hyperedge_index = [[100, 120, 300, 450, 500, 501],
... [3, 3, 3, 4, 4, 4]]
Since node IDs are not remapped, the original feature matrix can be used directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdata
|
HData
|
The input data object containing node and hyperedge information. |
required |
seed
|
int | None
|
Optional random seed for reproducible negative sampling. Defaults to |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
A new |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in hypertorch/data/negative_sampler.py
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 464 465 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 | |
__sample_loop(hdata, positive_hyperedges_signatures, seed=None)
¶
Sample unique negative hyperedges until the requested count or retry limit is reached.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdata
|
HData
|
The input hypergraph data used as the node and hyperedge ID source. |
required |
positive_hyperedges_signatures
|
set[tuple[int, ...]]
|
Existing positive hyperedge signatures that must not be sampled as negatives. |
required |
seed
|
int | None
|
Optional random seed for reproducible sampling. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
sampled_hyperedge_indexes |
list[Tensor]
|
sampled hyperedge index tensors |
sampled_hyperedge_attrs |
list[Tensor]
|
sampled hyperedge attribute tensors. |
sampled_negative_node_ids |
set[int]
|
sampled negative node IDs. |
new_hyperedge_id_offset |
int
|
first negative hyperedge ID. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the sampler cannot produce the requested number of unique negative hyperedges within the number of maximum allowed attempts. |
Source code in hypertorch/data/negative_sampler.py
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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | |
__validate_enough_negative_hyperedges(hdata, positive_hyperedges_signatures)
¶
Validate that enough unique negative hyperedges exist for the requested sample count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdata
|
HData
|
The input hypergraph data that defines the number of available nodes. |
required |
positive_hyperedges_signatures
|
set[tuple[int, ...]]
|
Positive hyperedge signatures with the same size as the requested negative hyperedges. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the requested number of negatives exceeds the number of possible unique non-positive hyperedges. |
Source code in hypertorch/data/negative_sampler.py
SameNodeSpaceNegativeSampler
¶
Bases: NegativeSampler, ABC
Base class for negative samplers that sample only from existing nodes.
Attributes:
| Name | Type | Description |
|---|---|---|
hyperedge_attr_enricher |
HyperedgeAttrsEnricher | None
|
Optional enricher used to generate attributes for sampled hyperedges. |
hyperedge_weights_enricher |
HyperedgeWeightsEnricher | None
|
Optional enricher used to generate weights for sampled hyperedges. |
Source code in hypertorch/data/negative_sampler.py
__init__(hyperedge_attr_enricher=None, hyperedge_weights_enricher=None, return_0based_negatives=False)
¶
Initialize the same-node-space negative sampler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_attr_enricher
|
HyperedgeAttrsEnricher | None
|
Optional enricher used to generate attributes for sampled hyperedges. |
None
|
hyperedge_weights_enricher
|
HyperedgeWeightsEnricher | None
|
Optional enricher used to generate weights for sampled hyperedges. |
None
|
return_0based_negatives
|
bool
|
If |
False
|
Source code in hypertorch/data/negative_sampler.py
NegativeSamplingScheduler
¶
Manages when to perform negative sampling during training based on a specified schedule.
This class allows for flexible scheduling of negative sampling, enabling it to be performed at different frequencies (e.g., every epoch, every N epochs, or only at the first epoch). The scheduler maintains a cache of the most recently sampled negatives, which can be reused across epochs if the schedule does not require resampling. This helps to optimize training by avoiding unnecessary sampling when the schedule dictates that negatives should only be generated at certain intervals.
Attributes:
| Name | Type | Description |
|---|---|---|
negative_sampler |
NegativeSampler
|
An instance of a |
negative_sampling_schedule |
NegativeSamplingSchedule
|
Literal string specifying the schedule for sampling negatives. |
negative_sampling_every_n |
int
|
An integer specifying the interval for sampling negatives
when the schedule is set to |
Source code in hypertorch/data/negative_sampling_scheduler.py
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 | |
config
property
¶
Returns the configuration of the negative sampling scheduler as a dictionary.
__init__(negative_sampler, negative_sampling_schedule='every_epoch', negative_sampling_every_n=1)
¶
Initialize the scheduler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
negative_sampler
|
NegativeSampler
|
Sampler used to create negative samples. |
required |
negative_sampling_schedule
|
NegativeSamplingSchedule
|
Schedule controlling when negatives are sampled. |
'every_epoch'
|
negative_sampling_every_n
|
int
|
Epoch interval used for |
1
|
Source code in hypertorch/data/negative_sampling_scheduler.py
should_sample(epoch)
¶
Whether to resample negatives for the current epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epoch
|
int
|
The current epoch number, used to determine if sampling should occur based on the schedule. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
should_sample |
bool
|
True if negatives should be resampled for the current epoch, False otherwise. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in hypertorch/data/negative_sampling_scheduler.py
sample(batch, epoch)
¶
Sample fresh negatives if the schedule requires it, otherwise return cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
HData
|
The current batch of data for which to sample negatives. |
required |
epoch
|
int
|
The current epoch number, used to determine if sampling should occur based on the schedule. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
negatives |
HData
|
A batch of negative samples, either freshly sampled or from cache. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the schedule asks to reuse cached negatives before any negatives have been sampled. |
Source code in hypertorch/data/negative_sampling_scheduler.py
BaseSampler
¶
Bases: ABC
Abstract base class for sampling from an HData instance.
Source code in hypertorch/data/sampler.py
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 | |
sample(index, hdata)
abstractmethod
¶
Sample a sub-hypergraph and return HData with global IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int | list[int]
|
An integer or list of integers specifying which items to sample. |
required |
hdata
|
HData
|
The original HData to sample from. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
A new HData instance containing only the sampled items and their associated data. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the method is not implemented by a subclass. |
Source code in hypertorch/data/sampler.py
len(hdata)
abstractmethod
¶
Return the number of sampleable items (nodes or hyperedges).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdata
|
HData
|
The HData to query for the number of sampleable items. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the method is not implemented by a subclass. |
Source code in hypertorch/data/sampler.py
HyperedgeSampler
¶
Bases: BaseSampler
Sampler that selects hyperedges and their incident nodes.
Source code in hypertorch/data/sampler.py
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 | |
sample(index, hdata)
¶
Sample hyperedges by their IDs and return the sub-hypergraph containing only those hyperedges and their incident nodes.
Examples:
>>> hyperedge_index = [[0, 0, 1, 2, 3, 4],
... [0, 0, 0, 1, 2, 2]]
>>> hdata = HData.from_hyperedge_index(hyperedge_index)
>>> strategy = HyperedgeSampler()
>>> sampled_hdata = strategy.sample([0, 2], hdata)
>>> sampled_hdata.hyperedge_index
>>> tensor([[0, 0, 1, 3, 4],
... [0, 0, 0, 2, 2]])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int | list[int]
|
An integer or a list of integers representing hyperedge IDs to sample. |
required |
hdata
|
HData
|
The original HData to sample from. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
An HData instance containing only the sampled hyperedges and their incident nodes. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provided index is invalid (e.g., empty list or list length exceeds number of hyperedges). |
IndexError
|
If any hyperedge ID is out of bounds. |
Source code in hypertorch/data/sampler.py
len(hdata)
¶
Return the number of sampleable hyperedges in the given HData.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdata
|
HData
|
The HData to query for the number of sampleable hyperedges. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
num_hyperedges |
int
|
The number of sampleable hyperedges in the HData. |
Source code in hypertorch/data/sampler.py
__sample_task_specific_fields(sampled_hdata, sampled_hyperedge_index, sampled_hyperedge_ids)
¶
Sample task-specific fields for the given sampled HData.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sampled_hdata
|
HData
|
The sampled HData to process. |
required |
sampled_hyperedge_index
|
Tensor
|
A tensor containing the hyperedge index of the sampled hyperedges. |
required |
sampled_hyperedge_ids
|
Tensor
|
A tensor containing the IDs of the sampled hyperedges. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
sampled_hdata |
HData
|
The HData with task-specific fields sampled. |
Source code in hypertorch/data/sampler.py
NodeSampler
¶
Bases: BaseSampler
Sampler that selects nodes and their incident hyperedges.
Source code in hypertorch/data/sampler.py
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 384 385 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 | |
sample(index, hdata)
¶
Sample nodes by their IDs and return the sub-hypergraph containing only those nodes and their incident hyperedges.
Examples:
>>> hyperedge_index = [[0, 0, 1, 2, 3, 4],
... [0, 0, 0, 1, 2, 2]]
>>> hdata = HData.from_hyperedge_index(hyperedge_index)
>>> strategy = NodeSampler()
>>> sampled_hdata = strategy.sample([0, 3], hdata)
>>> sampled_hdata.hyperedge_index
>>> tensor([[0, 0, 1, 3, 4],
... [0, 0, 0, 2, 2]])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int | list[int]
|
An integer or a list of integers representing node IDs to sample. |
required |
hdata
|
HData
|
The original HData to sample from. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
An HData instance containing only the sampled nodes and their incident hyperedges. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provided index is invalid (e.g., empty list or list length exceeds number of nodes). |
IndexError
|
If any node ID is out of bounds. |
Source code in hypertorch/data/sampler.py
len(hdata)
¶
Return the number of nodes in the given HData.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdata
|
HData
|
The HData to query for the number of nodes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
num_nodes |
int
|
The number of nodes in the HData. |
Source code in hypertorch/data/sampler.py
__sample_hdata(hdata, ids_to_sample)
¶
Sample nodes from the given HData and return a new HData instance containing only the sampled nodes and their incident hyperedges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdata
|
HData
|
The HData to sample from. |
required |
ids_to_sample
|
list[int]
|
A list of node IDs to sample. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
sampled_hdata |
HData
|
An HData instance containing only the sampled nodes and their incident hyperedges. |
sampled_hyperedge_index |
Tensor
|
A tensor containing the hyperedge index of the sampled hyperedges. |
sampled_node_ids |
Tensor
|
A tensor containing the IDs of the sampled nodes. |
Source code in hypertorch/data/sampler.py
__sample_task_specific_fields(sampled_hdata, sampled_hyperedge_index, sampled_node_ids)
¶
Sample task-specific fields for the given sampled HData. For examples, if the task is node-related, it will create a target node mask for the sampled nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sampled_hdata
|
HData
|
The sampled HData to process. |
required |
sampled_hyperedge_index
|
Tensor
|
A tensor containing the hyperedge index of the sampled hyperedges. |
required |
sampled_node_ids
|
Tensor
|
A tensor containing the IDs of the sampled nodes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
sampled_hdata |
HData
|
The HData with task-specific fields sampled. |
Source code in hypertorch/data/sampler.py
SamplingStrategyEnum
¶
HyperedgeDatasetSplitter
¶
Bases: Splitter['Dataset', tuple[list['Dataset'], list[float]]]
Split a dataset into dense transductive hyperedge partitions.
In the transductive setting, every split keeps the full hypergraph as model context and
identifies its supervised hyperedges with HData.target_hyperedge_mask. In the inductive
setting, splits are materialized as local sub-hypergraphs.
Attributes:
| Name | Type | Description |
|---|---|---|
node_space_setting |
NodeSpaceSetting
|
Whether to preserve full or local node spaces. |
shuffle |
bool | None
|
Whether to shuffle hyperedges before splitting. |
seed |
int | None
|
Random seed used when shuffling. |
Source code in hypertorch/data/splitter.py
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 | |
__init__(node_space_setting='transductive', shuffle=False, seed=None)
¶
Initialize the dataset splitter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_space_setting
|
NodeSpaceSetting
|
Whether to preserve full or local node spaces. |
'transductive'
|
shuffle
|
bool | None
|
Whether to shuffle hyperedges before splitting. |
False
|
seed
|
int | None
|
Optional random seed for reproducibility. |
None
|
Source code in hypertorch/data/splitter.py
split(to_split, **kwargs)
¶
Split a dataset and return materialized split datasets plus final ratios.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_split
|
Dataset
|
The |
required |
kwargs
|
Any
|
ratios: Desired split ratios, used for initial split construction and
as a reference during rebalancing. Expected as a keyword argument.
List of floats summing to |
{}
|
Returns: split_datasets: The list of split datasets. final_ratios: The list of final target-hyperedge-count ratios.
Raises:
| Type | Description |
|---|---|
ValueError
|
If ratios do not sum to |
Source code in hypertorch/data/splitter.py
HyperedgeHDataSplitter
¶
Bases: Splitter['HData', 'HData']
Materialize an HData split from explicit hyperedge IDs.
Attributes:
| Name | Type | Description |
|---|---|---|
node_space_setting |
NodeSpaceSetting
|
Whether to preserve the full node space in the split. |
Source code in hypertorch/data/splitter.py
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 384 385 386 387 388 389 390 391 392 393 394 | |
__init__(node_space_setting='transductive')
¶
Initialize the HData splitter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_space_setting
|
NodeSpaceSetting
|
Whether to preserve the full node space in the split. |
'transductive'
|
Source code in hypertorch/data/splitter.py
split(to_split, **kwargs)
¶
Build an HData for a single split from the given hyperedge IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_split
|
HData
|
The original HData containing the full hypergraph. |
required |
kwargs
|
Any
|
split_hyperedge_ids: The hyperedge IDs that should be included in the split, expected as a keyword argument. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
The splitted instance with remapped node and hyperedge IDs. |
Source code in hypertorch/data/splitter.py
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 384 385 386 387 388 389 390 391 392 393 394 | |
HyperedgeIDSplitter
¶
Bases: Splitter['Tensor', tuple[list['Tensor'], list[float]]]
Splitter for hyperedge-ID based dataset partitioning.
Attributes:
| Name | Type | Description |
|---|---|---|
hyperedge_index |
Tensor
|
Hypergraph incidence index whose node coverage drives the split logic. |
num_nodes |
int
|
Number of nodes in the source hypergraph. |
num_hyperedges |
int
|
Number of hyperedges in the source hypergraph. |
device |
device
|
Device of the source hyperedge index. |
Source code in hypertorch/data/splitter.py
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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 | |
__init__(hyperedge_index, num_nodes, num_hyperedges)
¶
Initialize the hyperedge ID splitter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_index
|
Tensor
|
Hypergraph incidence index whose node coverage drives split logic. |
required |
num_nodes
|
int
|
Number of nodes in the source hypergraph. |
required |
num_hyperedges
|
int
|
Number of hyperedges in the source hypergraph. |
required |
Source code in hypertorch/data/splitter.py
ensure_split_covers_all_nodes(hyperedge_ids_by_split, split_idx=0)
¶
Rebalance a split until its hyperedges cover every node in the hypergraph.
Hyperedges are moved from the other splits into the target split, always choosing the donor hyperedge that covers the largest number of currently missing nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_ids_by_split
|
list[Tensor]
|
Hyperedge IDs assigned to each split. |
required |
split_idx
|
int
|
Index of the split that must cover the full node space. Defaults to |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
hyperedge_ids_by_split |
list[Tensor]
|
The updated hyperedge IDs for each split. |
ratios |
list[float]
|
The final ratios of hyperedges in each split after rebalancing. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If one or more nodes do not appear in any hyperedge of the source hypergraph. |
Source code in hypertorch/data/splitter.py
validate_splits_have_hyperedges(hyperedge_ids_by_split)
¶
Validate that every split retains at least one hyperedge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_ids_by_split
|
list[Tensor]
|
Hyperedge IDs assigned to each split. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any split is empty after splitting or rebalancing. |
Source code in hypertorch/data/splitter.py
get_hyperedge_ids_permutation(shuffle, seed)
¶
Return hyperedge IDs in deterministic or shuffled order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shuffle
|
bool | None
|
Whether to randomly permute the hyperedge IDs. |
required |
seed
|
int | None
|
Optional random seed used when |
required |
Returns:
| Name | Type | Description |
|---|---|---|
hyperedge_ids_permutation |
Tensor
|
Ordered or shuffled hyperedge IDs on the HData device. |
Source code in hypertorch/data/splitter.py
get_sampleable_hyperedge_ids_permutation(sampleable_hyperedge_ids, shuffle, seed)
¶
Return sampleable hyperedge IDs in deterministic or shuffled order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sampleable_hyperedge_ids
|
Tensor
|
Hyperedge IDs that can be sampled from the dataset. |
required |
shuffle
|
bool | None
|
Whether to randomly permute the hyperedge IDs. |
required |
seed
|
int | None
|
Optional random seed used when |
required |
Returns:
| Name | Type | Description |
|---|---|---|
hyperedge_ids_permutation |
Tensor
|
Ordered or shuffled hyperedge IDs on the HData device. |
Source code in hypertorch/data/splitter.py
get_split_ratios(hyperedge_ids_by_split)
¶
Compute realized split ratios from hyperedge counts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_ids_by_split
|
list[Tensor]
|
Hyperedge IDs assigned to each split. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ratios |
list[float]
|
Ratios derived from the number of hyperedges in each split. |
Source code in hypertorch/data/splitter.py
split(to_split, **kwargs)
¶
Split hyperedge IDs by cumulative ratio boundaries.
Early splits use cumulative floor boundaries to avoid over-consuming hyperedges. The final split receives any remaining hyperedges caused by rounding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_split
|
Tensor
|
Hyperedge IDs to partition. |
required |
kwargs
|
Any
|
ratios: Desired split ratios, used for initial split construction and as a reference during rebalancing. Expected as a keyword argument. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
hyperedge_ids_by_split |
list[Tensor]
|
The updated hyperedge IDs for each split. |
ratios |
list[float]
|
The final ratios of hyperedges in each split after rebalancing. |
Source code in hypertorch/data/splitter.py
__missing_node_ids(hyperedge_ids, required_node_ids)
¶
Return the node IDs not covered by the given hyperedges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_ids
|
Tensor
|
Hyperedge IDs whose node coverage should be inspected. |
required |
required_node_ids
|
Tensor
|
Node IDs that must be covered. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
missing_node_ids |
Tensor
|
Required node IDs that are still uncovered. |
Source code in hypertorch/data/splitter.py
__nodes_covered_by_hyperedges(hyperedge_ids)
¶
Collect unique node IDs incident to the provided hyperedges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_ids
|
Tensor
|
Hyperedge IDs to inspect. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
nodes_covered_by_hyperedge |
Tensor
|
Unique node IDs covered by the input hyperedges. |
Source code in hypertorch/data/splitter.py
__pick_covering_hyperedge(hyperedge_ids_by_split, missing_node_ids, split_to_cover_idx)
¶
Choose the donor hyperedge that covers the most currently missing nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_ids_by_split
|
list[Tensor]
|
Hyperedge IDs assigned to each split. |
required |
missing_node_ids
|
Tensor
|
Node IDs still missing from the target split. |
required |
split_to_cover_idx
|
int
|
Index of the split being rebalanced. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
split_idx |
int
|
The index of the donor split containing the selected hyperedge. |
hyperedge_id |
Tensor
|
The ID of the selected hyperedge. |
Source code in hypertorch/data/splitter.py
NodeDatasetSplitter
¶
Bases: Splitter['Dataset', tuple[list['Dataset'], list[float]]]
Split a node-classification dataset by supervised node IDs.
Attributes:
| Name | Type | Description |
|---|---|---|
node_space_setting |
NodeSpaceSetting
|
Whether to preserve the full graph in every split. |
shuffle |
bool | None
|
Whether to shuffle node IDs before splitting. |
seed |
int | None
|
Random seed used when shuffling. |
Source code in hypertorch/data/splitter.py
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 | |
__init__(node_space_setting='transductive', shuffle=False, seed=None)
¶
Initialize the node dataset splitter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_space_setting
|
NodeSpaceSetting
|
Whether to preserve the full graph in every split. |
'transductive'
|
shuffle
|
bool | None
|
Whether to shuffle node IDs before splitting. |
False
|
seed
|
int | None
|
Optional random seed for reproducibility. |
None
|
Source code in hypertorch/data/splitter.py
split(to_split, **kwargs)
¶
Split a dataset and return materialized node-classification partitions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_split
|
Dataset
|
The dataset to split. |
required |
kwargs
|
Any
|
ratios: Desired split ratios, used for initial split construction and as a reference during rebalancing. Expected as a keyword argument. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
split_datasets |
tuple[list[Dataset], list[float]]
|
The list of split datasets. |
Source code in hypertorch/data/splitter.py
NodeHDataSplitter
¶
Bases: Splitter['HData', 'HData']
Materialize an HData split from explicit node IDs.
Attributes:
| Name | Type | Description |
|---|---|---|
node_space_setting |
NodeSpaceSetting
|
Whether to preserve the full node space in the split. |
Source code in hypertorch/data/splitter.py
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 | |
__init__(node_space_setting='transductive')
¶
Initialize the node HData splitter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_space_setting
|
NodeSpaceSetting
|
Whether to preserve the full node space in the split. |
'transductive'
|
Source code in hypertorch/data/splitter.py
split(to_split, **kwargs)
¶
Build an HData for a single split from the given node IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_split
|
HData
|
The original |
required |
kwargs
|
Any
|
split_node_ids: The node IDs that should be included in the split, expected as a keyword argument. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
The splitted instance with remapped node and hyperedge IDs. |
Source code in hypertorch/data/splitter.py
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 | |
NodeIDSplitter
¶
Bases: Splitter['Tensor', tuple[list['Tensor'], list[float]]]
Splitter for node-ID based dataset partitioning.
Attributes:
| Name | Type | Description |
|---|---|---|
num_nodes |
int
|
Number of nodes in the source hypergraph. |
device |
device
|
Device for generated node ID tensors. |
Source code in hypertorch/data/splitter.py
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 | |
__init__(num_nodes, device)
¶
Initialize the node ID splitter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int
|
Number of nodes in the source hypergraph. |
required |
device
|
device
|
Device for generated node ID tensors. |
required |
Source code in hypertorch/data/splitter.py
get_node_ids_permutation(shuffle, seed)
¶
Return node IDs in deterministic or shuffled order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shuffle
|
bool | None
|
Whether to randomly permute the node IDs. |
required |
seed
|
int | None
|
Optional random seed used when |
required |
Returns:
| Name | Type | Description |
|---|---|---|
node_ids_permutation |
Tensor
|
Ordered or shuffled node IDs on the HData device. |
Source code in hypertorch/data/splitter.py
get_sampleable_node_ids_permutation(sampleable_node_ids, shuffle, seed)
¶
Return sampleable node IDs in deterministic or shuffled order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shuffle
|
bool | None
|
Whether to randomly permute the node IDs. |
required |
seed
|
int | None
|
Optional random seed used when |
required |
Returns:
| Name | Type | Description |
|---|---|---|
node_ids_permutation |
Tensor
|
Ordered or shuffled node IDs on the HData device. |
Source code in hypertorch/data/splitter.py
validate_splits_have_nodes(node_ids_by_split)
¶
Validate that every split has at least one node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_ids_by_split
|
list[Tensor]
|
Node IDs assigned to each split. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any split is empty after splitting. |
Source code in hypertorch/data/splitter.py
get_split_ratios(node_ids_by_split)
¶
Compute realized split ratios from node counts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_ids_by_split
|
list[Tensor]
|
Node IDs assigned to each split. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ratios |
list[float]
|
Ratios derived from the number of nodes in each split. |
Source code in hypertorch/data/splitter.py
split(to_split, **kwargs)
¶
Split node IDs by cumulative ratio boundaries.
Early splits use cumulative floor boundaries to avoid over-consuming nodes. The final split receives any remaining nodes caused by rounding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_split
|
Tensor
|
Node IDs to partition. |
required |
kwargs
|
Any
|
ratios: Desired split ratios, used for initial split construction and as a reference during rebalancing. Expected as a keyword argument. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
node_ids_by_split |
list[Tensor]
|
The updated node IDs for each split. |
ratios |
list[float]
|
The final split ratios. |
Source code in hypertorch/data/splitter.py
SparseHyperedgeDatasetSplitter
¶
Bases: Splitter['Dataset', tuple[list['Dataset'], list[float]]]
Split a dataset by materializing sparse hyperedge partitions.
In the transductive setting, only the configured train split keeps the full node space, while the other splits are local sub-hypergraphs.
Attributes:
| Name | Type | Description |
|---|---|---|
node_space_setting |
NodeSpaceSetting
|
Whether to preserve full or local node spaces. |
shuffle |
bool | None
|
Whether to shuffle hyperedges before splitting. |
seed |
int | None
|
Random seed used when shuffling. |
Source code in hypertorch/data/splitter.py
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 | |
__init__(node_space_setting='transductive', shuffle=False, seed=None)
¶
Initialize the sparse dataset splitter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_space_setting
|
NodeSpaceSetting
|
Whether to preserve full or local node spaces. |
'transductive'
|
shuffle
|
bool | None
|
Whether to shuffle hyperedges before splitting. Defaults to |
False
|
seed
|
int | None
|
Optional random seed for reproducibility. Defaults to |
None
|
Source code in hypertorch/data/splitter.py
split(to_split, **kwargs)
¶
Split a dataset and return materialized sparse split datasets plus final ratios.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_split
|
Dataset
|
The dataset to split. |
required |
kwargs
|
Any
|
ratios: Desired split ratios. Expected as a keyword argument. cover_all_nodes_in_train_split: Whether transductive splits should move hyperedges into the train split until all nodes are covered. train_split_idx: The index of the split to treat as the train split. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
split_datasets |
list[Dataset]
|
The list of split datasets. |
final_ratios |
list[float]
|
The list of final hyperedge-count ratios. |
Source code in hypertorch/data/splitter.py
__validate_train_split_idx(train_split_idx, ratios)
¶
Validate the split index used as the transductive train split.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_split_idx
|
int
|
Index of the split treated as the train split. |
required |
ratios
|
list[float]
|
Requested split ratios. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the index is incompatible with the node-space setting or ratios. |
Source code in hypertorch/data/splitter.py
SparseHyperedgeHDataSplitter
¶
Bases: Splitter['HData', 'HData']
Materialize sparse HData splits from explicit hyperedge IDs.
Attributes:
| Name | Type | Description |
|---|---|---|
node_space_setting |
NodeSpaceSetting
|
Whether to preserve the full node space in the split. |
Source code in hypertorch/data/splitter.py
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 464 465 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 | |
__init__(node_space_setting='transductive')
¶
Initialize the sparse HData splitter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_space_setting
|
NodeSpaceSetting
|
Whether to preserve the full node space in the split. |
'transductive'
|
Source code in hypertorch/data/splitter.py
split(to_split, **kwargs)
¶
Build an HData for a single split from the given hyperedge IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_split
|
HData
|
The original HData containing the full hypergraph. |
required |
kwargs
|
Any
|
split_hyperedge_ids: The hyperedge IDs that should be included in the split, expected as a keyword argument. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
The splitted instance with remapped node and hyperedge IDs. |
Source code in hypertorch/data/splitter.py
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 464 465 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 | |
Splitter
¶
Bases: ABC, Generic[_ToSplitType, _SplitResultType]
Abstract base class for splitting objects into parts.
Source code in hypertorch/data/splitter.py
split(to_split, **kwargs)
abstractmethod
¶
Split the input object and return the split result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_split
|
_ToSplitType
|
The object to split. |
required |
**kwargs
|
Any
|
Additional keyword arguments that may be required by specific splitter implementations. |
{}
|
Returns:
| Type | Description |
|---|---|
_SplitResultType
|
The result of splitting the input object. |
Source code in hypertorch/data/splitter.py
list_datasets()
¶
get_dataset_by_name(dataset_name)
¶
Instantiate a supported dataset by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
str
|
Name returned by |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dataset |
Dataset
|
Loaded dataset instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the dataset name is not registered. |
Source code in hypertorch/data/supported_datasets.py
create_sampler_from_strategy(strategy)
¶
Factory function to create a sampler instance based on the provided sampling strategy type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
SamplingStrategy
|
An instance of SamplingStrategy enum indicating which sampling strategy to use. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
sampler |
BaseSampler
|
An instance of a subclass of BaseSampler corresponding to the specified strategy. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |