Train¶
hyperbench.train
¶
LaTexTableLogger
¶
Bases: Logger
A Lightning Logger that accumulates metrics and writes a LaTex comparison table.
Multiple instances (one per model) share a class-level store keyed by experiment_name. Every time finalize() is called (after fit() or test() for each model), the current state of all accumulated metrics is written to a LaTex file. The last model to finalize produces the most complete table.
This means the file is progressively updated as models finish training/testing, so you can open it mid-run to see partial results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_dir
|
str | Path
|
Base directory where the comparison/ subfolder will be created. |
required |
model_name
|
str
|
The model's full name (e.g., "mlp:mean"). |
required |
experiment_name
|
str
|
Shared key that groups all models in the same experiment. |
required |
precision
|
int
|
Decimal places for metric values in the table. |
4
|
Source code in hyperbench/train/latex_logger.py
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 | |
store
property
¶
Access the shared store for the current experiment.
log_metrics(metrics, step=None)
¶
Accumulate metrics for this model. Called by Lightning on every log step.
Keeps only the latest value for each metric name. For example, if "val_auc" is logged at step 10 and step 20, only the step 20 value is kept.
Source code in hyperbench/train/latex_logger.py
finalize(status)
¶
Write the LaTex comparison table with all accumulated metrics so far.
Called by Lightning after fit() and after test() for each model. Since models train/test sequentially, each finalize() overwrites the file with all data accumulated up to that point. The file grows more complete over time.
Source code in hyperbench/train/latex_logger.py
__split_results()
¶
Split all accumulated metrics into test vs train/val groups.
Metrics are classified by their name prefix: - "test_" → test_results - "train_" → train_results - "val_*" → val_results - anything else (e.g., "epoch") → ignored
Source code in hyperbench/train/latex_logger.py
MarkdownTableLogger
¶
Bases: Logger
A Lightning Logger that accumulates metrics and writes a markdown comparison table.
Multiple instances (one per model) share a class-level store keyed by experiment_name. Every time finalize() is called (after fit() or test() for each model), the current state of all accumulated metrics is written to a markdown file. The last model to finalize produces the most complete table.
This means the file is progressively updated as models finish training/testing, so partial results are available while running.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_dir
|
str | Path
|
Base directory where the comparison/ subfolder will be created. |
required |
model_name
|
str
|
The model's full name (e.g., "mlp:mean"). |
required |
experiment_name
|
str
|
Shared key that groups all models in the same experiment. |
required |
precision
|
int
|
Decimal places for metric values in the table. |
4
|
Source code in hyperbench/train/markdown_logger.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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 | |
store
property
¶
Access the shared store for the current experiment.
log_metrics(metrics, step=None)
¶
Accumulate metrics for this model. Called by Lightning on every log step.
Keeps only the latest value for each metric name. For example, if "val_auc" is logged at step 10 and step 20, only the step 20 value is kept.
Source code in hyperbench/train/markdown_logger.py
finalize(status)
¶
Write the markdown comparison table with all accumulated metrics so far.
Called by Lightning after fit() and after test() for each model. Since models train/test sequentially, each finalize() overwrites the file with all data accumulated up to that point. The file grows more complete over time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
str
|
The stage that just completed, e.g., "fit" or "test". |
required |
Source code in hyperbench/train/markdown_logger.py
__split_results()
¶
Split all accumulated metrics into test vs train/val groups.
Metrics are classified by their name prefix: - "test" → test_results - "train" → train_results - "val*" → val_results - anything else (e.g., "epoch") → ignored
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, float]]
|
Tuple of (test_results, train_results, val_results), where each is a dict |
dict[str, dict[str, float]]
|
mapping model names to their respective metric dicts. Models with no metrics |
dict[str, dict[str, float]]
|
in a category are excluded from that category's dict. |
Source code in hyperbench/train/markdown_logger.py
clear(experiment_name)
¶
Remove accumulated data for an experiment. Args: experiment_name: The experiment name whose data should be cleared.
__build_comparison_table(results, precision=4)
¶
Build a markdown comparison table from model results.
Examples:
Input:
Output:
| Model | test_auc | test_loss |
| --- | --- | --- |
| gat:default | 0.8200 | - |
| mlp:mean | 0.8500 | 0.3200 |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
Mapping[str, Mapping[str, float]]
|
Mapping of model names to metric dictionaries. |
required |
precision
|
int
|
Number of decimal places for numeric metric values. |
4
|
Returns:
| Type | Description |
|---|---|
str
|
Markdown table string. Returns an empty string if |
Source code in hyperbench/train/markdown_logger.py
__save_comparison_tables(test_results, save_dir, train_results=None, val_results=None, filename='results.md', precision=4)
¶
Build and save markdown comparison tables to a file.
Writes two sections: - "## Test Results" with the test metrics table - "## Train/Val Results" with the train/val metrics table (if provided)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
test_results
|
Mapping[str, Mapping[str, float]]
|
Dict from test_all(), mapping model names to test metric dicts. |
required |
save_dir
|
str | Path
|
Directory where the markdown file will be written. |
required |
train_results
|
Mapping[str, Mapping[str, float]] | None
|
Optional dict mapping model names to train metric dicts. |
None
|
val_results
|
Mapping[str, Mapping[str, float]] | None
|
Optional dict mapping model names to val metric dicts. |
None
|
filename
|
str
|
Name of the output file. |
'results.md'
|
precision
|
int
|
Decimal places for metric values. |
4
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the written file. |
Source code in hyperbench/train/markdown_logger.py
SameNodeSpaceNegativeSampler
¶
Bases: NegativeSampler, ABC
Base class for negative samplers that sample only from existing nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_attr_enricher
|
HyperedgeAttrsEnricher | None
|
An optional :class: |
None
|
hyperedge_weights_enricher
|
HyperedgeWeightsEnricher | None
|
An optional :class: |
None
|
return_0based_negatives
|
bool
|
|
False
|
Source code in hyperbench/train/negative_sampler.py
GeneratedNodesNegativeSampler
¶
Bases: NegativeSampler, ABC
Base class for negative samplers that generate new nodes instead of sampling from existing ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_feature_enricher
|
NodeEnricher
|
A :class: |
required |
hyperedge_attr_enricher
|
HyperedgeAttrsEnricher | None
|
An optional :class: |
None
|
hyperedge_weights_enricher
|
HyperedgeWeightsEnricher | None
|
An optional :class: |
None
|
return_0based_negatives
|
bool
|
|
False
|
Source code in hyperbench/train/negative_sampler.py
NegativeSampler
¶
Bases: ABC
Abstract base class for negative samplers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
return_0based_negatives
|
bool
|
|
False
|
Source code in hyperbench/train/negative_sampler.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
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. |
None
|
Returns:
| Type | Description |
|---|---|
HData
|
The negative samples as a new :class: |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the method is not implemented in a subclass. |
Source code in hyperbench/train/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 :class: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.
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 :class: |
None
|
hyperedge_weights_enricher
|
HyperedgeWeightsEnricher | None
|
An optional :class: |
None
|
return_0based_negatives
|
bool
|
|
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If either argument is not positive. |
Source code in hyperbench/train/negative_sampler.py
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 | |
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 :class: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. |
None
|
Returns:
| Type | Description |
|---|---|
HData
|
A new :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in hyperbench/train/negative_sampler.py
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 | |
NegativeSamplingSchedule
¶
Bases: Enum
When to run negative sampling during training.
Source code in hyperbench/train/negative_sampling_scheduler.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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
negative_sampler
|
NegativeSampler
|
An instance of a |
required |
negative_sampling_schedule
|
NegativeSamplingSchedule
|
An instance of |
EVERY_EPOCH
|
negative_sampling_every_n
|
int
|
An integer specifying the interval for sampling negatives when the schedule is set to |
1
|
Source code in hyperbench/train/negative_sampling_scheduler.py
config
property
¶
Returns the configuration of the negative sampling scheduler as a dictionary.
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:
| Type | Description |
|---|---|
bool
|
True if negatives should be resampled for the current epoch, False otherwise. |
Source code in hyperbench/train/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:
| Type | Description |
|---|---|
HData
|
A batch of negative samples, either freshly sampled or from cache. |
Source code in hyperbench/train/negative_sampling_scheduler.py
MultiModelTrainer
¶
A trainer class to handle training multiple models with individual trainers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_configs
|
list[ModelConfig]
|
A list of ModelConfig objects, each containing a model and its associated trainer (if any). |
required |
experiment_name
|
str | None
|
Name for this experiment run's log directory. When |
None
|
accelerator
|
str | Accelerator
|
Supports passing different accelerator types ("cpu", "gpu", "tpu", "hpu", "mps", "auto") as well as custom accelerator instances. |
'auto'
|
devices
|
list[int] | str | int
|
The devices to use. Can be set to a positive number (int or str), a sequence of device indices
(list or str), the value |
'auto'
|
strategy
|
str | Strategy
|
Supports different training strategies with aliases as well custom strategies.
Defaults to |
'auto'
|
num_nodes
|
int
|
Number of GPU nodes for distributed training.
Defaults to |
1
|
precision
|
Any | None
|
Double precision (64, '64' or '64-true'), full precision (32, '32' or '32-true'),
16bit mixed precision (16, '16', '16-mixed') or bfloat16 mixed precision ('bf16', 'bf16-mixed').
Can be used on CPU, GPU, TPUs, or HPUs.
Defaults to |
None
|
max_epochs
|
int | None
|
Stop training once this number of epochs is reached. Disabled by default (None).
If both max_epochs and max_steps are not specified, defaults to |
None
|
min_epochs
|
int | None
|
Force training for at least these many epochs. Disabled by default (None). |
None
|
max_steps
|
int
|
Stop training after this number of steps. Disabled by default (-1). If |
-1
|
min_steps
|
int | None
|
Force training for at least these number of steps. Disabled by default ( |
None
|
check_val_every_n_epoch
|
int | None
|
Perform a validation loop after every |
1
|
logger
|
Logger | Iterable[Logger] | bool | None
|
Logger (or iterable collection of loggers) for experiment tracking. A |
None
|
default_root_dir
|
str | Path | None
|
Default path for logs and weights when no logger/ckpt_callback passed.
Defaults to |
None
|
enable_autolog_hparams
|
bool
|
Whether to log hyperparameters at the start of a run.
Defaults to |
True
|
log_every_n_steps
|
int | None
|
How often to log within steps.
Defaults to |
None
|
profiler
|
Profiler | str | None
|
To profile individual steps during training and assist in identifying bottlenecks.
Defaults to |
None
|
fast_dev_run
|
int | bool
|
Runs n if set to |
False
|
enable_checkpointing
|
bool
|
If |
True
|
enable_progress_bar
|
bool
|
Whether to enable the progress bar by default.
Defaults to |
True
|
enable_model_summary
|
bool | None
|
Whether to enable model summarization by default.
Defaults to |
None
|
callbacks
|
list[Callback] | Callback | None
|
Add a callback or list of callbacks.
Defaults to |
None
|
auto_start_tensorboard
|
bool
|
When |
False
|
tensorboard_port
|
int
|
Port for the auto-launched TensorBoard server.
Defaults to |
6006
|
auto_wait
|
bool
|
When |
False
|
Source code in hyperbench/train/trainer.py
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 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 | |
wait()
¶
Wait until the user presses Enter, keeping process alive. If no process is running, this method does nothing.