BayesOpt
posterior_empirical.cpp
1 /*
2 -------------------------------------------------------------------------
3  This file is part of BayesOpt, an efficient C++ library for
4  Bayesian optimization.
5 
6  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
7 
8  BayesOpt is free software: you can redistribute it and/or modify it
9  under the terms of the GNU Affero General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  BayesOpt is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Affero General Public License for more details.
17 
18  You should have received a copy of the GNU Affero General Public License
19  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
20 ------------------------------------------------------------------------
21 */
22 
23 #include "log.hpp"
24 #include "inneroptimization.hpp"
25 #include "posterior_empirical.hpp"
26 
27 namespace bayesopt
28 {
29 
30 
31  EmpiricalBayes::EmpiricalBayes(size_t dim, Parameters parameters,
32  randEngine& eng):
33  PosteriorModel(dim,parameters,eng)
34  {
35  // Configure Surrogate and Criteria Functions
36  setSurrogateModel(eng);
37  setCriteria(eng);
38 
39  // Seting kernel optimization
40  size_t nhp = mGP->nHyperParameters();
41  kOptimizer.reset(new NLOPT_Optimization(mGP.get(),nhp));
42 
43  //TODO: Generalize
44  if (mParameters.sc_type == SC_ML)
45  {
46  // local search to avoid underfitting
47  kOptimizer->setAlgorithm(BOBYQA);
48  kOptimizer->setMaxEvals(10*nhp);
49  }
50  else
51  {
52  kOptimizer->setAlgorithm(COMBINED);
53  kOptimizer->setMaxEvals(20*nhp);
54  }
55  //Limits in log space
56  kOptimizer->setLimits(svectord(nhp,-6.0),svectord(nhp,1.0));
57  }
58 
60  { } // Default destructor
61 
62 
63  void EmpiricalBayes::updateHyperParameters()
64  {
65  FILE_LOG(logDEBUG) << "------ Optimizing hyperparameters ------";
66  vectord optimalTheta = mGP->getHyperParameters();
67 
68  FILE_LOG(logDEBUG) << "Initial hyper parameters: " << optimalTheta;
69  kOptimizer->run(optimalTheta);
70  mGP->setHyperParameters(optimalTheta);
71  FILE_LOG(logDEBUG) << "Final hyper parameters: " << optimalTheta;
72  };
73 
74 
75  void EmpiricalBayes::setSurrogateModel(randEngine& eng)
76  {
78  mData,mMean,eng));
79  } // setSurrogateModel
80 
81  void EmpiricalBayes::setCriteria(randEngine& eng)
82  {
83  CriteriaFactory mCFactory;
84 
85  mCrit.reset(mCFactory.create(mParameters.crit_name,mGP.get()));
86  mCrit->setRandomEngine(eng);
87 
88  if (mCrit->nParameters() == mParameters.crit_params.size())
89  {
90  mCrit->setParameters(mParameters.crit_params);
91  }
92  else // If the number of paramerters is different, use default.
93  {
94  if (mParameters.crit_params.size() != 0)
95  {
96  FILE_LOG(logERROR) << "Expected " << mCrit->nParameters()
97  << " parameters. Got "
98  << mParameters.crit_params.size() << " instead.";
99  }
100  FILE_LOG(logINFO) << "Using default parameters for criteria.";
101  }
102  } // setCriteria
103 
104 
105 } //namespace bayesopt
106 
virtual ~EmpiricalBayes()
Default destructor.
Factory model for criterion functions This factory is based on the libgp library by Manuel Blum https...
static NonParametricProcess * create(size_t dim, Parameters parameters, const Dataset &data, MeanModel &mean, randEngine &eng)
Factory model generator for surrogate models.
Namespace of the library interface.
Definition: using.dox:1
Parameters mParameters
Configuration parameters.
vectord crit_params
Criterion hyperparameters (if needed)
Definition: parameters.hpp:111
Criteria * create(std::string name, NonParametricProcess *proc)
Factory model for criterion functions This function is based on the libgp library by Manuel Blum http...
size_t mDims
Number of dimensions.
Global exploration, local refinement (hand tuned)
boost::scoped_ptr< NonParametricProcess > mGP
Pointer to surrogate model.
std::string crit_name
Name of the criterion.
Definition: parameters.hpp:110
C++ wrapper of the NLOPT library.
boost::scoped_ptr< Criteria > mCrit
Metacriteria model.
Modules and helper macros for logging.
Local, derivative free.
score_type sc_type
Score type for kernel hyperparameters (ML,MAP,etc)
Definition: parameters.hpp:95
Dataset mData
Dataset (x-> inputs, y-> labels/output)
Bayesian optimization using different non-parametric processes as distributions over surrogate functi...