Commits

Ruben Martinez-Cantin committed 25e8e32

Adding score/learning separate selector

Comments (0)

Files changed (8)

examples/bo_cont.cpp

   par.surr_name = "sStudentTProcessJef";
   par.kernel.name = "kSum(kSEISO,kConst)";
   par.mean.name = "mConst";
-  par.l_type = L_ML;
+  par.sc_type = SC_ML;
   par.n_iterations = 200;       // Number of iterations
   par.n_init_samples = 50;
   par.verbose_level = 2;

include/kernelregressor.hpp

      * \brief Computes the initial surrogate model and updates the
      * kernel parameters estimation. 
      *
-     * This function requires to recompute all covariance matrixes,
-     * inverses, etc.  Use it with precaution.
+     * This function requires to recompute the full Kernel matrix (and
+     * its decomposition).  Use it with precaution.
      */
     void fitSurrogateModel();
 
     /** 
-     * \brief Sequential update of the surrogate model by adding a new point.
-     *  Add new point efficiently using Matrix Decomposition Lemma for the
-     *  inversion of the correlation matrix or adding new rows to the
-     *  Cholesky decomposition.  If retrain is true, it recomputes the
-     *  kernel hyperparameters and computes a full covariance matrix.
+     * \brief Sequential update of the surrogate model by adding a new
+     * row to the Kernel matrix, more precisely, to its Cholesky
+     * decomposition. 
+     * 
+     * It assumes that the kernel hyperparemeters do not change.
      */   
-    void updateSurrogateModel(const vectord &Xnew, double Ynew, bool retrain);
+    void updateSurrogateModel(const vectord &Xnew);
 
 
     // Getters and setters
     double getSignalVariance();
 
     /** Sets the kind of learning methodology for kernel hyperparameters */
-    void setLearnType(learning_type l_type);
+    //void setLearnType(learning_type l_type);
 
   protected:
 
 
   protected:
     matrixd mL;             ///< Cholesky decomposition of the Correlation matrix
-    learning_type mLearnType;
+    score_type mScoreType;
+    //learning_type mLearnType;
     KernelModel mKernel;
 
   private:
     precomputePrediction(); 
   };
 
-  inline void KernelRegressor::setLearnType(learning_type l_type) 
-  { mLearnType = l_type; };
+  // inline void KernelRegressor::setLearnType(learning_type l_type) 
+  // { mLearnType = l_type; };
 
   inline void KernelRegressor::computeCorrMatrix(matrixd& corrMatrix)
   { mKernel.computeCorrMatrix(mData.mX,corrMatrix,mRegularizer); }

include/nonparametricprocess.hpp

     virtual void fitSurrogateModel() = 0;
 
     /** 
-     * \brief Sequential update of the surrogate model by adding a new point.
-     *  Add new point efficiently using Matrix Decomposition Lemma for the
-     *  inversion of the correlation matrix or adding new rows to the
-     *  Cholesky decomposition.  If retrain is true, it recomputes the
-     *  kernel hyperparameters and computes a full covariance matrix.
+     * \brief Sequential update of the surrogate model by adding a new
+     * row to the Kernel matrix, more precisely, to its Cholesky
+     * decomposition. 
+     * 
+     * It assumes that the kernel hyperparemeters do not change.
      */   
-    virtual void updateSurrogateModel(const vectord &Xnew, 
-				      double Ynew, bool retrain) = 0;
+    virtual void updateSurrogateModel(const vectord &Xnew) = 0;
 
 
     // Getters and setters

include/parameters.h

   
   typedef enum {
     L_FIXED,
-    L_ML,
-    L_MAP,
-    L_LOO,
-    L_BAYES,
+    L_EMPIRICAL,
+    L_DISCRETE,
+    L_MCMC,
     L_ERROR = -1
   } learning_type;
 
+  typedef enum {
+    SC_MTL,
+    SC_ML,
+    SC_MAP,
+    SC_LOOCV,
+    SC_ERROR = -1
+  } score_type;
+
+
   /** Kernel configuration parameters */
   typedef struct {
     char*  name;                 /**< Name of the kernel function */
     size_t n_inner_iterations;   /**< Maximum inner optimizer evaluations */
     size_t n_init_samples;       /**< Number of samples before optimization */
     size_t n_iter_relearn;       /**< Number of samples before relearn kernel */
-    size_t init_method;   /**< Sampling method for initial set 1-LHS, 2-Sobol (if available), other uniform */
+
+    /** Sampling method for initial set 1-LHS, 2-Sobol (if available),
+     *  other value-uniformly distributed */
+    size_t init_method;          
 
     size_t verbose_level;        /**< 1-Error,2-Warning,3-Info. 4-6 log file*/
     char* log_filename;          /**< Log file path (if applicable) */
     double noise;                /**< Observation noise (and nugget) */
     double alpha;                /**< Inverse Gamma prior for signal var */
     double beta;                 /**< Inverse Gamma prior for signal var*/
+    score_type sc_type;          /**< Score type for kernel hyperparameters (ML,MAP,etc) */
     learning_type l_type;        /**< Type of learning for the kernel params*/
     double epsilon;              /**< For epsilon-greedy exploration */
 
   /* BAYESOPT_API const char* surrogate2str(surrogate_name name); */
   BAYESOPT_API const char* learn2str(learning_type name);
 
+  /* surrogate_name str2surrogate (const char* name); */
+  BAYESOPT_API score_type str2score(const char* name);
+
+  /* BAYESOPT_API const char* surrogate2str(surrogate_name name); */
+  BAYESOPT_API const char* score2str(score_type name);
+
+
   BAYESOPT_API bopt_params initialize_parameters_to_default(void);
 
 #ifdef __cplusplus

src/bayesoptbase.cpp

 
   void BayesOptBase::stepOptimization(size_t ii)
   {
-    vectord xNext = nextPoint(); // Find what is the next point.
-    
+    // Find what is the next point.
+    vectord xNext = nextPoint(); 
     double yNext = evaluateSampleInternal(xNext);
+    addSample(xNext,yNext);
 
     // Update surrogate model
     bool retrain = ((mParameters.n_iter_relearn > 0) && 
 		    ((ii + 1) % mParameters.n_iter_relearn == 0));
     
-    addSample(xNext,yNext);
-    mGP->updateSurrogateModel(xNext,yNext,retrain); 
+    if (retrain)  // Full update
+      {
+	mGP->fitSurrogateModel();
+      }
+    else          // Incremental update
+      {
+	mGP->updateSurrogateModel(xNext);
+      } 
     plotStepData(ii,xNext,yNext);
   }
 

src/empiricalbayesprocess.cpp

 					       const Dataset& data, randEngine& eng):
     KernelRegressor(dim,parameters,data,eng)
   { 
-    if (mLearnType == L_BAYES)
-      {
-	FILE_LOG(logERROR) << "Empirical Bayes model and full Bayes learning are incompatible.";
-	throw 1;
-      }
+    // if (mLearnType == L_BAYES)
+    //   {
+    // 	FILE_LOG(logERROR) << "Empirical Bayes model and full Bayes learning are incompatible.";
+    // 	throw std::invalid_argument("Trying full Bayes learning for an empirical Bayes model.");
+    //   }
 
     size_t nhp = mKernel.nHyperParameters();
     kOptimizer = new NLOPT_Optimization(this,nhp);
 
     //TODO: Generalize
-    if (parameters.l_type == L_ML)
+    if (parameters.sc_type == SC_ML)
       {
 	kOptimizer->setAlgorithm(BOBYQA);    // local search to avoid underfitting
       }
 
   void EmpiricalBayesProcess::updateKernelParameters()
   {
-    if (mLearnType == L_FIXED)
-      {
-	FILE_LOG(logDEBUG) << "Fixed hyperparameters. Not learning";
-      }
-    else
-      {
+    // if (mLearnType == L_FIXED)
+    //   {
+    // 	FILE_LOG(logDEBUG) << "Fixed hyperparameters. Not learning";
+    //   }
+    // else
+    //   {
 	vectord optimalTheta = mKernel.getHyperParameters();
 	
 	FILE_LOG(logDEBUG) << "Initial kernel parameters: " << optimalTheta;
 	kOptimizer->run(optimalTheta);
 	mKernel.setHyperParameters(optimalTheta);
 	FILE_LOG(logDEBUG) << "Final kernel parameters: " << optimalTheta;	
-      }
+      // }
   };
 
   double EmpiricalBayesProcess::evaluateKernelParams()
   { 
-    switch(mLearnType)
+    switch(mScoreType)
       {
-      case L_ML:
+      case SC_MTL:
 	return negativeTotalLogLikelihood();
-      case L_FIXED:
+      case SC_ML:
 	return negativeLogLikelihood();
-      case L_MAP:
+      case SC_MAP:
 	// It is a minus because the prior is the positive and we want
 	// the negative.
 	return negativeLogLikelihood()-mKernel.kernelLogPrior();
-      case L_LOO:
+      case SC_LOOCV:
 	return negativeCrossValidation(); 
       default:
 	FILE_LOG(logERROR) << "Learning type not supported";

src/kernelregressor.cpp

   KernelRegressor::KernelRegressor(size_t dim, bopt_params parameters,
 				   const Dataset& data, randEngine& eng):
     NonParametricProcess(dim,parameters,data,eng), mRegularizer(parameters.noise),
-    mKernel(dim, parameters)
-  { 
-    setLearnType(parameters.l_type);
-  }
+    mKernel(dim, parameters),mScoreType(parameters.sc_type)
+  { }
 
   KernelRegressor::~KernelRegressor(){}
 
 
 
-  void KernelRegressor::updateSurrogateModel( const vectord &Xnew,
-					      double Ynew, bool retrain)
+  void KernelRegressor::updateSurrogateModel(const vectord &Xnew)
   {
-    assert( mData.mX[1].size() == Xnew.size() );
-
-    if (retrain)
-      {
-	//addSample(Xnew,Ynew);
-	fitSurrogateModel();	
-      }
-    else
-      {
-	//	addSample(Xnew,Ynew);
-	vectord newK = computeCrossCorrelation(Xnew);
-	newK(newK.size()-1) += mRegularizer;   // We add it to the last element
-	utils::cholesky_add_row(mL,newK);
-	//double selfCorrelation = computeSelfCorrelation(Xnew) + mRegularizer;
-	//addNewPointToCholesky(newK,selfCorrelation);
-	precomputePrediction(); 
-      }
+    vectord newK = computeCrossCorrelation(Xnew);
+    newK(newK.size()-1) += mRegularizer;   // We add it to the last element
+    utils::cholesky_add_row(mL,newK);
+    precomputePrediction(); 
   } // updateSurrogateModel
 
 

src/parameters.cpp

 
 learning_type str2learn(const char* name)
 {
-  if      (!strcmp(name,  "L_ML"))
-    return L_ML;
-  else if (!strcmp(name,  "L_MAP"))
-    return L_MAP;
-  else if (!strcmp(name,  "L_LOO"))
-    return L_LOO;
+  if      (!strcmp(name,"L_FIXED"))     return L_FIXED;
+  else if (!strcmp(name,"L_EMPIRICAL")) return L_EMPIRICAL;
+  else if (!strcmp(name,"L_DISCRETE"))  return L_DISCRETE;
+  else if (!strcmp(name,"L_MCMC"))      return L_MCMC;
   else return L_ERROR;
 }
 
 {
   switch(name)
     {
-    case L_ML: return "L_ML"; 
-    case L_MAP: return "L_MAP"; 
-    case L_LOO: return "L_LOO"; 
+    case L_FIXED:     return "L_FIXED"; 
+    case L_EMPIRICAL: return "L_EMPIRICAL"; 
+    case L_DISCRETE:  return "L_DISCRETE"; 
+    case L_MCMC:      return "L_MCMC"; 
     case L_ERROR:
     default: return "ERROR!";
     }
 }
 
+
+score_type str2score(const char* name)
+{
+  if      (!strcmp(name,  "SC_MTL"))   return SC_MTL;
+  else if (!strcmp(name,  "SC_ML"))    return SC_ML;
+  else if (!strcmp(name,  "SC_MAP"))   return SC_MAP;
+  else if (!strcmp(name,  "SC_LOOCV")) return SC_LOOCV;
+  else return SC_ERROR;
+}
+
+
+
+
+const char* score2str(score_type name)
+{
+  switch(name)
+    {
+    case SC_MTL:   return "SC_MTL"; 
+    case SC_ML:    return "SC_ML"; 
+    case SC_MAP:   return "SC_MAP"; 
+    case SC_LOOCV: return "SC_LOOCV"; 
+    case SC_ERROR:
+    default: return "ERROR!";
+    }
+}
+
+
 /*
 char DEF_LOG_FILE[128] = "bayesopt.log";
 char DEF_SUR_NAME[128] = "sGaussianProcess";
   params.noise = DEFAULT_NOISE;
   params.alpha = PRIOR_ALPHA;
   params.beta = PRIOR_BETA;
-  params.l_type = L_MAP;
+  params.l_type = L_EMPIRICAL;
+  params.sc_type = SC_MAP;
   params.epsilon = 0.0;
   
   params.crit_name = new char[128];