pub struct HyperparameterTuner {
pub config: TunerConfig,
pub history: Vec<TuningResult>,
pub next_trial_id: u64,
}Expand description
Hyperparameter tuner supporting random search, grid search, and simplified Bayesian optimization with UCB acquisition.
Fields§
§config: TunerConfig§history: Vec<TuningResult>§next_trial_id: u64Implementations§
Source§impl HyperparameterTuner
impl HyperparameterTuner
Sourcepub fn new(config: TunerConfig) -> Self
pub fn new(config: TunerConfig) -> Self
Create a new tuner with the given configuration.
Sourcepub fn add_spec(&mut self, spec: HpSpec) -> &mut Self
pub fn add_spec(&mut self, spec: HpSpec) -> &mut Self
Add a hyperparameter specification to the tuner (builder-style).
Sourcepub fn sample_value(spec: &HpSpec, rng: &mut u64) -> HpValue
pub fn sample_value(spec: &HpSpec, rng: &mut u64) -> HpValue
Sample a single value for one spec using the xorshift64 PRNG.
Sourcepub fn sample_config(&self, rng: &mut u64) -> HpConfig
pub fn sample_config(&self, rng: &mut u64) -> HpConfig
Sample a complete configuration (one value per spec).
Sourcepub fn record_result(&mut self, config: HpConfig, score: f64, now: u64) -> u64
pub fn record_result(&mut self, config: HpConfig, score: f64, now: u64) -> u64
Record an evaluation result. Returns the assigned trial_id.
Sourcepub fn best_config(&self) -> Option<&TuningResult>
pub fn best_config(&self) -> Option<&TuningResult>
Return a reference to the best trial in history, or None if empty.
Sourcepub fn suggest_next(&self, rng: &mut u64) -> HpConfig
pub fn suggest_next(&self, rng: &mut u64) -> HpConfig
Suggest the next configuration to evaluate.
- For
RandomSearch/ no strategy context: sample a random config. - For Bayesian: draw 10 random candidates, evaluate UCB for each, and return the one with the highest UCB.
Sourcepub fn run_random_search(
&mut self,
n_trials: u32,
scorer: impl FnMut(&HpConfig) -> f64,
rng: &mut u64,
) -> Vec<TuningResult>
pub fn run_random_search( &mut self, n_trials: u32, scorer: impl FnMut(&HpConfig) -> f64, rng: &mut u64, ) -> Vec<TuningResult>
Run n_trials random evaluations using the provided scorer, record all
results, and return them sorted by score (best first).
Sourcepub fn grid_configs(&self) -> Vec<HpConfig>
pub fn grid_configs(&self) -> Vec<HpConfig>
Enumerate all grid configurations.
Continuous→ 5 evenly-spaced values across[lo, hi]Discrete→ every integer in[lo, hi]Categorical→ every choice
Sourcepub fn run_grid_search(
&mut self,
scorer: impl FnMut(&HpConfig) -> f64,
) -> Vec<TuningResult>
pub fn run_grid_search( &mut self, scorer: impl FnMut(&HpConfig) -> f64, ) -> Vec<TuningResult>
Enumerate all grid configurations, evaluate each with scorer, record
results, and return them sorted best-first.
Sourcepub fn run_bayesian(
&mut self,
n_trials: u32,
n_initial: u32,
exploration_weight: f64,
scorer: impl FnMut(&HpConfig) -> f64,
rng: &mut u64,
) -> Vec<TuningResult>
pub fn run_bayesian( &mut self, n_trials: u32, n_initial: u32, exploration_weight: f64, scorer: impl FnMut(&HpConfig) -> f64, rng: &mut u64, ) -> Vec<TuningResult>
Run Bayesian (UCB) optimization. Draws n_initial random configs to
warm-start, then proposes n_trials - n_initial UCB-guided candidates.
Sourcepub fn importance_scores(&self) -> HashMap<String, f64>
pub fn importance_scores(&self) -> HashMap<String, f64>
Compute a simple variance-of-scores importance score for each parameter.
For each parameter, group history trials into equal-width buckets across
the parameter’s range and compute the variance of per-bucket mean scores.
Returns 0.0 for any parameter if there are fewer than 2 trials.
Sourcepub fn stats(&self) -> TunerStats
pub fn stats(&self) -> TunerStats
Aggregate statistics over all recorded trials.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for HyperparameterTuner
impl RefUnwindSafe for HyperparameterTuner
impl Send for HyperparameterTuner
impl Sync for HyperparameterTuner
impl Unpin for HyperparameterTuner
impl UnsafeUnpin for HyperparameterTuner
impl UnwindSafe for HyperparameterTuner
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more