Loading...
Searching...
No Matches
gpr::bo::ParameterSearch Class Reference

Ask-and-tell minimization of a noisy loss over a parameter box. More...

#include <gpr/bo/ParameterSearch.h>

Public Member Functions

 ParameterSearch (std::vector< ParameterBound > bounds, ParameterSearchConfig config={})
int dimension () const noexcept
int observationCount () const noexcept
const ParameterSearchConfig & config () const noexcept
const std::vector< ParameterBound > & bounds () const noexcept
void observe (const ParameterObservation &observation)
 Record one evaluated parameter vector.
void observe (const Eigen::VectorXd &parameters, double loss)
 Record a loss observed with the shared noise variance.
void observe (const Eigen::VectorXd &parameters, double loss, double noise_variance)
 Record a loss observed with its own noise variance.
std::vector< Eigen::VectorXd > propose (int count)
 Propose up to count parameter vectors to evaluate next.
ParameterSearchResult best ()
 The incumbent, its loss and the number of observations.
void predict (const Eigen::MatrixXd &parameters, Eigen::VectorXd &mean, Eigen::VectorXd &standard_deviation)
 Surrogate prediction of the latent loss, in the units of the observed losses, at each row of parameters.
void predictFeasibility (const Eigen::MatrixXd &parameters, Eigen::VectorXd &probability)
 Probability of feasibility P(c(x) <= 0) at each row of parameters. One everywhere while no constraint value is observed.
const ScalarHyperparameters & hyperparameters ()
 Hyperparameters of the loss surrogate, on the unit cube and for the standardized loss.

Detailed Description

Ask-and-tell minimization of a noisy loss over a parameter box.

The caller evaluates the loss. observe() records one result, with its noise variance when the caller knows it, and propose() returns the next parameter vectors to run. The surrogate is a ScalarSurrogate on the unit cube, fitted to losses standardized to zero mean and unit variance.

propose(q) builds a batch with the constant liar heuristic of Ginsbourger, Le Riche and Carraro (2010): after each pick the surrogate is conditioned on a made-up observation at that point, whose value is the incumbent (the "CL-min" lie), and the acquisition is maximized again. The hyperparameters are not refitted on the lies. The q points can then be evaluated together, for example in one allocation.

An observation may carry a constraint value c(x), feasible when c(x) <= 0. A second scalar GP models c, and expected improvement is multiplied by the probability of feasibility P(c(x) <= 0), as in Gardner et al. (2014) and Gelbart, Snoek and Adams (2014). The feasibility model is a GP regression on the constraint value, or on labels -1 (feasible) and +1 (infeasible) when only the outcome is known. It is not a classification likelihood: with labels the probability is a heuristic score and is not calibrated.

With ParameterSearchConfig::noisy_objective the incumbent is the smallest posterior mean at the evaluated points instead of the smallest observed loss: the plug-in form of expected improvement for a noisy oracle, reviewed by Picheny, Wagner and Ginsbourger (2013). One lucky noisy run then does not set the bar every later candidate has to beat.

Definition at line 764 of file ParameterSearch.h.

Constructor & Destructor Documentation

◆ ParameterSearch()

gpr::bo::ParameterSearch::ParameterSearch ( std::vector< ParameterBound > bounds,
ParameterSearchConfig config = {} )
inlineexplicit

Definition at line 766 of file ParameterSearch.h.

767 {})
768 : bounds_(std::move(bounds)), config_(std::move(config))
769 {
770 if (bounds_.empty()) {
771 throw std::invalid_argument("parameter search needs a bound");
772 }
773 if (config_.candidate_count <= 0 || config_.fit_starts <= 0 ||
774 !(config_.length_scale > 0.0) ||
775 !std::isfinite(config_.length_scale) ||
776 !(config_.noise_variance >= 0.0) ||
777 !std::isfinite(config_.noise_variance) ||
778 !(config_.constraint_noise_variance >= 0.0) ||
779 !std::isfinite(config_.constraint_noise_variance)) {
780 throw std::invalid_argument("parameter search config is invalid");
781 }
782 bool any_categorical = false;
783 for (const ParameterBound& bound: bounds_) {
784 validateBound(bound);
785 categorical_.push_back(bound.categorical);
786 any_categorical = any_categorical || bound.categorical;
787 }
788 if (!any_categorical) categorical_.clear();
789 primes_ = detail::firstPrimes(dimension());
790 }
int dimension() const noexcept
const std::vector< ParameterBound > & bounds() const noexcept
const ParameterSearchConfig & config() const noexcept
void validateBound(const ParameterBound &bound)
Throw std::invalid_argument unless the axis is well formed.

Member Function Documentation

◆ best()

ParameterSearchResult gpr::bo::ParameterSearch::best ( )
inlinenodiscard

The incumbent, its loss and the number of observations.

The incumbent is the feasible evaluated parameter vector with the smallest loss. With noisy_objective it is the one with the smallest posterior mean, and loss is that mean.

Exceptions
std::logic_errorwithout a feasible observation.

Definition at line 946 of file ParameterSearch.h.

947 {
948 refresh();
949 const Incumbent incumbent = findIncumbent(*model_);
950 if (!incumbent.exists) {
951 throw std::logic_error(
952 "parameter search has no feasible observation");
953 }
954 ParameterSearchResult result;
955 result.parameters = toParameters({incumbent.unit}).front();
956 result.loss = incumbent.loss;
957 result.evaluations = observationCount();
958 return result;
959 }
int observationCount() const noexcept

◆ bounds()

const std::vector< ParameterBound > & gpr::bo::ParameterSearch::bounds ( ) const
inlinenodiscardnoexcept

Definition at line 807 of file ParameterSearch.h.

808 {
809 return bounds_;
810 }

◆ config()

const ParameterSearchConfig & gpr::bo::ParameterSearch::config ( ) const
inlinenodiscardnoexcept

Definition at line 802 of file ParameterSearch.h.

803 {
804 return config_;
805 }

◆ dimension()

int gpr::bo::ParameterSearch::dimension ( ) const
inlinenodiscardnoexcept

Definition at line 792 of file ParameterSearch.h.

793 {
794 return static_cast<int>(bounds_.size());
795 }

◆ hyperparameters()

const ScalarHyperparameters & gpr::bo::ParameterSearch::hyperparameters ( )
inlinenodiscard

Hyperparameters of the loss surrogate, on the unit cube and for the standardized loss.

Exceptions
std::logic_errorwithout an observed loss.

Definition at line 995 of file ParameterSearch.h.

996 {
997 refresh();
998 requireObjective();
999 return model_->surrogate.hyperparameters();
1000 }

◆ observationCount()

int gpr::bo::ParameterSearch::observationCount ( ) const
inlinenodiscardnoexcept

Definition at line 797 of file ParameterSearch.h.

798 {
799 return static_cast<int>(losses_.size());
800 }

◆ observe() [1/3]

void gpr::bo::ParameterSearch::observe ( const Eigen::VectorXd & parameters,
double loss )
inline

Record a loss observed with the shared noise variance.

Definition at line 861 of file ParameterSearch.h.

862 {
863 ParameterObservation observation;
864 observation.parameters = parameters;
865 observation.loss = loss;
866 observe(observation);
867 }
void observe(const ParameterObservation &observation)
Record one evaluated parameter vector.

◆ observe() [2/3]

void gpr::bo::ParameterSearch::observe ( const Eigen::VectorXd & parameters,
double loss,
double noise_variance )
inline

Record a loss observed with its own noise variance.

Definition at line 870 of file ParameterSearch.h.

872 {
873 if (!(noise_variance >= 0.0)) {
874 throw std::invalid_argument(
875 "observation noise variance must be non-negative");
876 }
877 ParameterObservation observation;
878 observation.parameters = parameters;
879 observation.loss = loss;
880 observation.noise_variance = noise_variance;
881 observe(observation);
882 }

◆ observe() [3/3]

void gpr::bo::ParameterSearch::observe ( const ParameterObservation & observation)
inline

Record one evaluated parameter vector.

The parameters are moved to the lattice of their axes and clamped to the box. Either every observation carries a noise variance or none does; a mixture throws std::invalid_argument. A run that produced no loss is recorded with a non-finite loss and a positive constraint: it informs the feasibility model only.

Definition at line 821 of file ParameterSearch.h.

822 {
823 if (observation.parameters.size() != dimension()) {
824 throw std::invalid_argument("parameter count differs");
825 }
826 const bool has_constraint = !std::isnan(observation.constraint);
827 if (has_constraint && !std::isfinite(observation.constraint)) {
828 throw std::invalid_argument("constraint value is not finite");
829 }
830 if (has_constraint &&
831 config_.acquisition != ParameterAcquisition::ExpectedImprovement) {
832 throw std::invalid_argument(
833 "a constraint needs the expected improvement acquisition");
834 }
835 const bool failed_run = has_constraint && observation.constraint > 0.0;
836 if (!observation.parameters.allFinite() ||
837 (!std::isfinite(observation.loss) && !failed_run)) {
838 throw std::invalid_argument("parameter observation is not finite");
839 }
840 const bool has_noise = observation.noise_variance >= 0.0;
841 if (std::isnan(observation.noise_variance) ||
842 (has_noise && !std::isfinite(observation.noise_variance))) {
843 throw std::invalid_argument(
844 "observation noise variance must be non-negative");
845 }
846 if (!losses_.empty() && has_noise != row_noise_) {
847 throw std::invalid_argument(
848 "supply a noise variance for every observation or for none");
849 }
850 row_noise_ = has_noise;
851 units_.push_back(toUnit(observation.parameters));
852 losses_.push_back(std::isfinite(observation.loss)
853 ? observation.loss
854 : std::numeric_limits<double>::quiet_NaN());
855 noises_.push_back(has_noise ? observation.noise_variance : 0.0);
856 constraints_.push_back(observation.constraint);
857 model_.reset();
858 }

◆ predict()

void gpr::bo::ParameterSearch::predict ( const Eigen::MatrixXd & parameters,
Eigen::VectorXd & mean,
Eigen::VectorXd & standard_deviation )
inline

Surrogate prediction of the latent loss, in the units of the observed losses, at each row of parameters.

Exceptions
std::logic_errorwithout an observed loss.

Definition at line 966 of file ParameterSearch.h.

968 {
969 const Eigen::MatrixXd queries = toUnitRows(parameters);
970 refresh();
971 requireObjective();
972 model_->surrogate.predict(queries, mean, standard_deviation);
973 mean = (mean.array() * model_->scale + model_->mean).matrix();
974 standard_deviation *= model_->scale;
975 }

◆ predictFeasibility()

void gpr::bo::ParameterSearch::predictFeasibility ( const Eigen::MatrixXd & parameters,
Eigen::VectorXd & probability )
inline

Probability of feasibility P(c(x) <= 0) at each row of parameters. One everywhere while no constraint value is observed.

Exceptions
std::logic_errorwithout observations.

Definition at line 982 of file ParameterSearch.h.

984 {
985 const Eigen::MatrixXd queries = toUnitRows(parameters);
986 refresh();
987 probability = feasibility(*model_, queries);
988 }

◆ propose()

std::vector< Eigen::VectorXd > gpr::bo::ParameterSearch::propose ( int count)
inlinenodiscard

Propose up to count parameter vectors to evaluate next.

Without observations the points are the next ones of a Halton sequence. Otherwise they maximize the acquisition one after the other under the constant liar. Before any feasible loss is known the acquisition is the probability of feasibility alone. Fewer than count vectors come back when the box holds fewer unevaluated lattice points.

Definition at line 894 of file ParameterSearch.h.

895 {
896 if (count <= 0) {
897 throw std::invalid_argument("proposal count must be positive");
898 }
899 std::vector<Eigen::VectorXd> chosen;
900 if (losses_.empty()) {
901 for (int attempt = 0; static_cast<int>(chosen.size()) < count &&
902 attempt < 64 * count;
903 ++attempt) {
904 const Eigen::VectorXd point = nextHalton();
905 if (detail::nearObserved(point, chosen, kDuplicateTolerance)) {
906 continue;
907 }
908 chosen.push_back(point);
909 }
910 return toParameters(chosen);
911 }
912 refresh();
913 Model working = *model_;
914 const Incumbent incumbent = findIncumbent(*model_);
915 const Eigen::VectorXd center =
916 incumbent.exists ? incumbent.unit : units_.back();
917 for (int slot = 0; slot < count; ++slot) {
918 std::vector<Eigen::VectorXd> candidates;
919 candidates.reserve(
920 static_cast<std::size_t>(config_.candidate_count) + 16);
921 for (int k = 0; k < config_.candidate_count; ++k) {
922 candidates.push_back(nextHalton());
923 }
924 detail::appendLocalCandidates(center, bounds_, &candidates);
925 const int pick =
926 bestCandidate(working, incumbent, chosen, &candidates);
927 if (pick < 0) break;
928 chosen.push_back(candidates[static_cast<std::size_t>(pick)]);
929 if (slot + 1 < count && incumbent.exists) {
930 loadObservations(&working, chosen, incumbent.standardized);
931 if (!working.surrogate.fit()) break;
932 }
933 }
934 return toParameters(chosen);
935 }

The documentation for this class was generated from the following file: