Tuning¶
ParameterSpace ¶
Search space for the tuned hyperparameters.
Each entry maps a hyperparameter name (a key of the optimizer's
param_groups, such as lr) to one of:
| Form | Meaning |
|---|---|
(low, high) |
continuous range, linear scale |
{"low": a, "high": b, "log": True} |
continuous range; log=True samples evenly across orders of magnitude (log defaults to False) |
[v1, v2, ...] |
categorical choices |
Numeric strings such as "1e-4" are converted to floats.
Attributes:
| Name | Type | Description |
|---|---|---|
space |
dict[str, Any]
|
Name to |
log |
dict[str, bool]
|
Name to |
Examples:
>>> space = ParameterSpace({
... "lr": {"low": 1e-4, "high": 1e-1, "log": True},
... "momentum": (0.8, 0.99),
... "nesterov_like": [0.0, 1.0],
... })
>>> space.is_log("lr")
True
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
space
|
dict[str, Any]
|
Mapping from hyperparameter name to its range or choices (see the class description). |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
A range mapping lacks |
TypeError
|
An entry is not a tuple, mapping or list, or a value is not numeric. |
Warns:
| Type | Description |
|---|---|
UserWarning
|
A linear range with |
Source code in src/autotunenet/parameters.py
is_log ¶
sample ¶
Draw one random configuration (uniform, or log-uniform for log ranges).
Uses Python's random module, so seed it with random.seed for
repeatable samples. BayesianOptimizer does not use this method; it
samples through Optuna.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Name to sampled value, for every entry in the space. |
Source code in src/autotunenet/parameters.py
BayesianOptimizer ¶
BayesianOptimizer(param_space: ParameterSpace, seed: int | None = None, smoothing_window: int = DEFAULT_SMOOTHING_WINDOW, n_startup_trials: int = DEFAULT_STARTUP_TRIALS, history_window: int | None = None)
Bases: Optimizer
Proposes hyperparameters with Optuna's TPE sampler and guards against harmful ones.
Each tuning step is one ask/tell pair: suggest() asks TPE for a
configuration, and observe() reports the score it produced. In
observe() the score is smoothed (moving average) and passed to the
StabilityMonitor:
- stable: the configuration is accepted. TPE is told its raw score,
the smoothed score goes into
history, and, unless the step was a tolerated regression, it becomes the rollback target. - unstable (a regression for
patiencesteps, or a NaN/Inf score at once): the configuration is rejected, TPE is still told its real score (or the worst score seen, for NaN/Inf) so it learns to avoid that region, androllback_paramsholds the last good configuration.
Usually driven by PyTorchHyperParameterAdapter, which also applies the
proposals and rollbacks to the PyTorch optimizer.
Attributes:
| Name | Type | Description |
|---|---|---|
study |
The underlying Optuna study (direction "maximize"). |
|
smoother |
The |
|
guard |
The |
|
rollback |
The |
|
last_rollback |
Whether the latest |
|
last_instability |
Same as |
Examples:
>>> space = ParameterSpace({"lr": {"low": 1e-4, "high": 1e-1, "log": True}})
>>> opt = BayesianOptimizer(space, seed=0, n_startup_trials=3)
>>> params = opt.suggest() # {"lr": ...}
>>> accepted = opt.observe(params, score=-0.42) # higher is better
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param_space
|
ParameterSpace
|
|
required |
seed
|
int | None
|
Seed for the TPE sampler ( |
None
|
smoothing_window
|
int
|
Moving-average window used by the stability check. |
DEFAULT_SMOOTHING_WINDOW
|
n_startup_trials
|
int
|
Number of completed trials TPE samples at random before it starts using its model. Optuna's default is 10, but an online run has few tuning steps, so the default here is 3. |
DEFAULT_STARTUP_TRIALS
|
history_window
|
int | None
|
If set, TPE learns only from the last
|
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
|
Source code in src/autotunenet/bayesian_optimizer.py
rollback_params
property
¶
The last good configuration, set when observe() rejects a configuration; None until then.
suggest ¶
Ask TPE for the next configuration.
Each call starts a new trial; the next observe() call reports its score.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Hyperparameter name to proposed value, one entry per item in the space. |
Source code in src/autotunenet/bayesian_optimizer.py
observe ¶
Report the score of the configuration from the last suggest().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
dict[str, Any]
|
The hyperparameters that actually ran. They may differ
from the proposal (for example after |
required |
score
|
float
|
Higher-is-better metric, e.g. |
required |
objective
|
float | None
|
What TPE is told about this configuration, if not
|
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
rejected; then |
bool
|
(or |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
There is no pending |
ValueError
|
|
Source code in src/autotunenet/bayesian_optimizer.py
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 | |
state_dict ¶
Everything needed to continue this optimizer in a new process.
Holds only plain Python values, so it can be saved with torch.save
(and loaded with weights_only=True) or as JSON.
Source code in src/autotunenet/bayesian_optimizer.py
load_state_dict ¶
Restore a state_dict() into an optimizer built with the same space and settings.
Source code in src/autotunenet/bayesian_optimizer.py
Optimizer ¶
Bases: ABC
Abstract base class for optimizers that propose hyperparameters and learn from scores.
Subclasses implement suggest(). Scores are higher-is-better (pass
-val_loss, not val_loss).
Attributes:
| Name | Type | Description |
|---|---|---|
param_space |
The search space. |
|
history |
list[tuple[dict[str, Any], float]]
|
|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param_space
|
ParameterSpace
|
The search space, usually a |
required |
Source code in src/autotunenet/optimizer.py
suggest
abstractmethod
¶
Propose the next configuration to try.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Hyperparameter name to value. |
observe ¶
Record the score of a configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
dict[str, Any]
|
The hyperparameters that produced the score. |
required |
score
|
float
|
Higher-is-better metric (e.g. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
Source code in src/autotunenet/optimizer.py
best_result ¶
Return the best (params, score) in history, ignoring non-finite scores.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
No finite observation has been recorded yet. |
Source code in src/autotunenet/optimizer.py
best_params ¶
best_score ¶
to_score ¶
Convert a metric to a Python float.
Accepts int/float, NumPy scalars (e.g. np.float32) and one-element tensors (e.g. torch.tensor(-0.1) or -loss.detach()).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The metric to convert. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The value as a float (NaN and infinity are passed through). |
Raises:
| Type | Description |
|---|---|
ValueError
|
|