Struct tpe::TpeOptimizer[][src]

pub struct TpeOptimizer<T = DefaultEstimatorBuilder> { /* fields omitted */ }
Expand description

Optimizer using TPE.

This try to search out the parameter value which could minimize the evaluation result.

Note that an instance of TpeOptimizer can handle only one hyperparameter. So if you want to optimize multiple hyperparameters simultaneously, please create an optimizer for each hyperparameter.

Implementations

Makes a new TpeOptimizer with the default settings.

If you want to customize the settings, please use TpeOptimizerBuilder instead.

Returns the next value of the optimization target parameter to be evaluated.

Note that, before the first asking, it might be worth to give some evaluation results of randomly sampled observations to TpeOptimizer (via the tell method) to reduce bias due to too few samples.

Tells the evaluation result of a hyperparameter value to the optimizer.

Note that the param should be NaN if the hyperparameter was not used in the evaluation (this could be happen when the entire search space is conditional).

Retruns all told parameter and objective values.

Note that the order of items in the returned iterator doesn’t reflect the order TpeOptimizer::tell called.

Examples

You can this method to store and load the state of a TpeOptimizer.

let temp_file = tempfile::NamedTempFile::new()?;
let mut rng = rand::thread_rng();

fn objective(x: f64) -> f64 {
    x.powi(2)
}

{
    let mut optim =
        tpe::TpeOptimizer::new(tpe::parzen_estimator(), tpe::range(-5.0, 5.0)?);

    // Runs 100 trials.
    for _ in 0..100 {
        let x = optim.ask(&mut rng)?;
        let v = objective(x);
        optim.tell(x, v)?;
    }

    // Stores the trials.
    let mut file = std::fs::OpenOptions::new()
        .write(true)
        .open(temp_file.path())?;
    serde_json::to_writer(&mut file, &optim.trials().collect::<Vec<_>>())?;
}

{
    let mut optim =
        tpe::TpeOptimizer::new(tpe::parzen_estimator(), tpe::range(-5.0, 5.0)?);

    // Loads the previous trials.
    let mut file = std::fs::File::open(temp_file.path())?;
    let trials: Vec<(f64, f64)> = serde_json::from_reader(&mut file)?;
    for (x, v) in trials {
        optim.tell(x, v)?;
    }

    // Runs additional 100 trials.
    for _ in 0..100 {
        let x = optim.ask(&mut rng)?;
        let v = objective(x);
        optim.tell(x, v)?;
    }
    assert_eq!(optim.trials().count(), 200);
}

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Should always be Self

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more

Checks if self is actually part of its subset T (and can be converted to it).

Use with care! Same as self.to_subset but without any property checks. Always succeeds.

The inclusion map: converts self to the equivalent element of its superset.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.