HPO Optimizers API¶
GAM Optimizer¶
GAMOptimizer ¶
GAMOptimizer(
objective_function: Callable[[dict], float],
search_space: SearchSpace,
settings: GAMOptSettings,
known_observations: list[dict] | None = None,
)
Bases: BaseOptimizer
Optimizer based on Generalized Additive Models. Trained model is used to suggest next node in the search space for evaluation.
Parameters:
-
objective_function(Callable[[dict], float]) –Target function that will be used in every evaluation. Output of this function should be 'float', as this is the value for which algorithms try to optimize solution. Function should take dict filled with 'key: value' pairs that are 'argument: corresponding value'.
-
search_space(SearchSpace) –Instance containing information about nodes in the solutions space that will be evaluated during the optimization.
-
settings(GAMOptSettings) –Instance with settings required for configuring the optimization process.
Attributes:
-
evaluations(list[dict]) –Already evaluated hyperparameters combinations with corresponding score.
-
max_iterations(int) –Validated maximum number of iterations during HPO.
Source code in ai4rag/core/hpo/gam_opt.py
Attributes¶
max_iterations property writable ¶
Get max possible number of iterations for the HPO.
Methods:¶
search ¶
Actual function performing hyperparameter optimization for the selected objective function.
Returns:
-
dict[str, Any]–The best set of parameters with achieved score.
Raises:
-
OptimizationError–When there were no successful evaluations for given constraints.
Source code in ai4rag/core/hpo/gam_opt.py
evaluate_initial_random_nodes ¶
Perform evaluation of randomly chosen n nodes from the solutions space. Evaluations are performed until desired number of successful evaluations is reached or maximum number of evaluations is reached.
When the optimizer has been warm-started with known observations, already-successful evaluations count toward the n_random_nodes target and already-evaluated combinations are excluded from candidates.
The selection is stratified: combinations that introduce at least one new unique value for any categorical (string-valued) parameter are moved to the front of the queue before the random fill. This guarantees that every distinct categorical value (e.g. search_mode="vector" vs search_mode="hybrid") is evaluated at least once before GAM training begins, regardless of how skewed the raw search space is.
A warning is logged when n_random_nodes is smaller than the estimated minimum required to guarantee full categorical coverage.
Source code in ai4rag/core/hpo/gam_opt.py
GAMOptSettings dataclass ¶
GAMOptSettings(max_evals: int, random_state: int = 64, n_random_nodes: int = 4, evals_per_trial: int = 1)
Bases: OptimizerSettings
Settings for the GAMOptimizer. For the detailed description of parameters for Generalized Additive Models, please see pygam documentation.
Parameters:
-
max_evals(int) –Maximum number of evaluations performed during optimization process.
-
n_random_nodes(int, default:4) –Number of random configurations to evaluate before starting GAM iterations. The initial sample is stratified: for every string-valued categorical parameter (e.g.
search_mode,chunking_method,ranker_strategy), at least one configuration for each unique value is guaranteed to appear before the random fill, regardless of raw search space imbalance. Integer/float parameters are not stratified. Set this to at least the number of unique values of the most varied string parameter to guarantee full categorical coverage; a warning is emitted when the value is too small. -
evals_per_trial(int, default:1) –Number of configurations to evaluate per GAM iteration.
-
random_state(int, default:64) –Inherited from OptimizerSettings. Controls shuffle order of initial random exploration phase. Does NOT control GAM model randomness (GAM training is deterministic).
Random Optimizer¶
RandomOptimizer ¶
RandomOptimizer(objective_function: Callable[[dict], float], search_space: SearchSpace, settings: RandomOptSettings)
Bases: BaseOptimizer
Optimizer running random search on the given search space.
Source code in ai4rag/core/hpo/random_opt.py
Methods:¶
search ¶
Actual function performing hyperparameter optimization for the selected objective function.
Returns:
-
dict[str, Any]–The best set of parameters with achieved score.
Raises:
-
OptimizationError–When there were no successful evaluations for given constraints.
Source code in ai4rag/core/hpo/random_opt.py
RandomOptSettings dataclass ¶
Bases: OptimizerSettings
Settings for random optimizer.
Parameters:
-
max_evals(int) –Maximum number of configurations to evaluate.
-
random_state(int, default:64) –Inherited from OptimizerSettings. Controls shuffle order of search space combinations.