Поиск гиперпараметров агента¶
Инструменты автоматической оптимизации гиперпараметров для алгоритмов управления. Поддерживаются Optuna и Ray Tune.
Быстрый старт (Optuna)¶
import numpy as np
from tensoraerospace.optimization import HyperParamOptimizationOptuna
def objective(trial):
# Пример: минимизируем квадратичную ошибку модели с гиперпараметрами
lr = trial.suggest_float("lr", 1e-4, 1e-1, log=True)
gamma = trial.suggest_float("gamma", 0.8, 0.999)
# ... обучите/оцените модель ...
score = np.random.rand() # замените на реальную метрику качества
return score
opt = HyperParamOptimizationOptuna(direction="minimize")
opt.run_optimization(objective, n_trials=20)
best = opt.get_best_param()
print("Лучшие параметры:", best)
opt.plot_parms(figsize=(12, 4))
Классы¶
HyperParamOptimizationOptuna(direction)
¶
Bases: HyperParamOptimizationBase
Hyperparameter optimization using Optuna.
Create an Optuna study for hyperparameter optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
direction
|
str
|
Optimization direction. One of |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
run_optimization(func, n_trials)
¶
Run hyperparameter search.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
Objective function to optimize. |
required |
n_trials
|
int
|
Number of trials to run. |
required |
get_best_param()
¶
Return the best hyperparameters found by Optuna.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Any]
|
Best hyperparameters. |
plot_parms(figsize=(15.0, 5.0))
¶
Plot trial values over the optimization history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
figsize
|
tuple[float, float]
|
Matplotlib figure size. Defaults to |
(15.0, 5.0)
|
Returns:
| Type | Description |
|---|---|
Figure
|
matplotlib.figure.Figure: The created figure. |
HyperParamOptimizationRay(direction, metric=None)
¶
Bases: HyperParamOptimizationBase
Hyperparameter optimization using Ray Tune.
Create a Ray Tune optimizer wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
direction
|
str
|
Optimization direction. One of
|
required |
metric
|
Optional[str]
|
Metric name used to choose the best result when Ray Tune supports it. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
run_optimization(func, param_space, tune_config=None, **kwargs)
¶
Run a Ray Tune search.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
Trainable (callable) to execute. See Ray Tune docs. |
required |
param_space
|
Any
|
Search space definition passed to Ray Tune. |
required |
tune_config
|
TuneConfig | None
|
Optional Ray Tune configuration. Defaults to
|
None
|
**kwargs
|
Any
|
Additional keyword arguments forwarded to |
{}
|
get_best_param()
¶
Return the best configuration from the latest Ray Tune run.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Best configuration dictionary. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If optimization has not been run yet or best result cannot be determined. |
plot_parms(*args, **kwargs)
¶
Plot optimization history (not implemented yet).
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always. |
Примечания¶
direction: выберите"minimize"или"maximize"для целевой метрики.- Для Ray Tune используйте
run_optimization(func, param_space, tune_config=...)и далее обрабатывайтеself.results(см. Ray Tune docs).