Skip to main content

TpeSampler

Struct TpeSampler 

Source
pub struct TpeSampler { /* private fields */ }
Expand description

Tree-structured Parzen Estimator sampler.

This wraps the tpe crate rather than reimplementing TPE from scratch — a real integration point, not a stubbed gap. The work this type does is translating between hyperopt-core’s Distribution/Trial history and tpe’s per-parameter TpeOptimizer (one optimizer handles exactly one hyperparameter), including:

  • mapping each Distribution variant onto a tpe range + estimator (Parzen for numeric, histogram for categorical),
  • encoding values into tpe’s coordinate space (log for LogUniform, index for Categorical, [low, high+1) for IntUniform) and decoding the sampled result back,
  • honouring the study Direction by negating objective values under Maximize, since tpe always minimizes.

The sampler is effectively stateless over trial history: each suggest rebuilds a TpeOptimizer from the study snapshot and replays the relevant observations. This makes it correct under both parallel execution (it simply works from whatever snapshot it’s given) and study reload from storage, at an O(n) per-suggestion cost that is negligible next to a real objective evaluation.

§Limitations (documented, not silent)

  • The first n_startup_trials completed trials are sampled at random to seed the estimators — matching Optuna’s warmup and avoiding a biased model from too few points.
  • Every hyperopt-core Distribution variant maps onto tpe. There is no variant tpe cannot represent here; should a future distribution not map cleanly, this sampler falls back to a random draw for it rather than producing an out-of-range value.

Implementations§

Source§

impl TpeSampler

Source

pub fn new() -> TpeSampler

A TPE sampler with default warmup (n_startup_trials = 10), seeded from OS entropy.

Source

pub fn seeded(seed: u64) -> TpeSampler

A TPE sampler with a fixed seed — reproducible for tests/benchmarks.

Source

pub fn n_startup_trials(self, n: usize) -> TpeSampler

Number of initial trials to sample randomly before building TPE models.

Trait Implementations§

Source§

impl Default for TpeSampler

Source§

fn default() -> TpeSampler

Returns the “default value” for a type. Read more
Source§

impl Sampler for TpeSampler

Source§

fn suggest( &mut self, study_state: &StudyState, _trial: &Trial, param_name: &str, distribution: &Distribution, ) -> Value

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.