Configuration¶
build_pytorch_autotunenet ¶
build_pytorch_autotunenet(torch_optimizer: Optimizer, raw_config: dict, seed: int | None = None) -> PyTorchHyperParameterAdapter
Validate a configuration and build the adapter, optimizer and stability guard from it.
Every section except parameter_space is optional; missing keys take the
defaults below.
| Section | Key | Default | Meaning |
|---|---|---|---|
parameter_space |
name | required | [low, high] (linear range), {low, high, log: true} (log range) or {choices: [...]} |
tuning |
tune_n_steps |
1 |
tune every n calls |
tuning |
warmup_epochs |
2 |
calls before tuning starts |
tuning |
warmup_metric_threshold |
None |
start tuning once the loss (-metric) is at or below this value |
tuning |
max_delta |
0.5 |
maximum relative change per step, in (0, 1); null = unclipped |
tuning |
score |
level |
what TPE learns from: level (metric reached) or progress (improvement during the interval) |
tuning |
spike_factor |
None |
divergence guard in on_batch_end: cut the LR when a batch loss exceeds this × the recent median |
metrics |
smoothing_window |
3 |
moving-average window |
search |
n_startup_trials |
3 |
random TPE trials before the model is used |
search |
history_window |
None |
TPE learns only from the last n scored configurations |
stability |
max_regression |
0.2 |
relative drop from the best score counted as a regression |
stability |
patience |
2 |
consecutive regressions before rollback |
stability |
cooldown |
3 |
steps with rollback suppressed after one |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
torch_optimizer
|
Optimizer
|
The |
required |
raw_config
|
dict
|
The configuration as a dictionary (e.g. loaded from YAML). |
required |
seed
|
int | None
|
Seed for the TPE sampler. |
None
|
Returns:
| Type | Description |
|---|---|
PyTorchHyperParameterAdapter
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
The configuration is invalid (see |
Examples:
>>> adapter = build_pytorch_autotunenet(
... optimizer,
... raw_config={
... "parameter_space": {"lr": {"low": 1e-4, "high": 1e-2, "log": True}},
... "tuning": {"warmup_epochs": 2, "max_delta": 0.5},
... "stability": {"patience": 2, "cooldown": 3},
... },
... seed=42,
... )
Source code in src/autotunenet/factory.py
load_config ¶
Load and validate a configuration from a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the YAML file. |
required |
Returns:
| Type | Description |
|---|---|
AutoTuneConfig
|
The validated |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
The file does not exist. |
ValueError
|
The configuration is invalid. |
Source code in src/autotunenet/config/loader.py
load_config_from_dict ¶
Validate a configuration given as a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_config
|
dict[str, Any]
|
The configuration, with the same sections as the YAML file. |
required |
Returns:
| Type | Description |
|---|---|
AutoTuneConfig
|
The validated |
Raises:
| Type | Description |
|---|---|
ValueError
|
The configuration is invalid. |
Source code in src/autotunenet/config/loader.py
AutoTuneConfig ¶
A validated AutoTuneNet configuration.
Sections: parameter_space (required), tuning, metrics, search and
stability; see build_pytorch_autotunenet for every key and its
default. Missing sections become empty dictionaries.
Attributes:
| Name | Type | Description |
|---|---|---|
raw_config |
The configuration as given. |
|
parameter_space |
The normalized parameter space. |
|
tuning |
|
|
metrics |
|
|
search |
|
|
stability |
|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_config
|
dict[str, Any]
|
The configuration dictionary. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
|
ValueError
|
A value has the wrong type or is out of range
(e.g. |
Source code in src/autotunenet/config/schema.py
normalize_parameter_space ¶
normalize_parameter_space(raw_space: dict[str, Any]) -> dict[str, tuple[float, float] | dict[str, Any] | list[Any]]
Convert a config-style parameter space into the form ParameterSpace expects.
Config files (YAML/JSON) have no tuple type, so the config syntax is: lr: [1e-4, 1e-1] -> continuous range (low, high), linear lr: {low: 1e-4, high: 1e-1} -> continuous range, linear lr: {low: 1e-4, high: 1e-1, log: true} -> continuous range, log scale batch_size: {choices: [32, 64]} -> categorical choices
Tuples are passed through unchanged as ranges. Numeric strings (such as
1e-4, which PyYAML reads as a string) are converted to floats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_space
|
dict[str, Any]
|
Name to range or choices, in config syntax. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, tuple[float, float] | dict[str, Any] | list[Any]]
|
Name to |
Raises:
| Type | Description |
|---|---|
ValueError
|
The space is not a dictionary, a range does not have
exactly two bounds, a bound is not numeric, |