![]() |
GURLS++
2.0.00
C++ Implementation of GURLS Matlab Toolbox
|
00001 /* 00002 * The GURLS Package in C++ 00003 * 00004 * Copyright (C) 2011-1013, IIT@MIT Lab 00005 * All rights reserved. 00006 * 00007 * authors: M. Santoro 00008 * email: msantoro@mit.edu 00009 * website: http://cbcl.mit.edu/IIT@MIT/IIT@MIT.html 00010 * 00011 * Redistribution and use in source and binary forms, with or without 00012 * modification, are permitted provided that the following conditions 00013 * are met: 00014 * 00015 * * Redistributions of source code must retain the above 00016 * copyright notice, this list of conditions and the following 00017 * disclaimer. 00018 * * Redistributions in binary form must reproduce the above 00019 * copyright notice, this list of conditions and the following 00020 * disclaimer in the documentation and/or other materials 00021 * provided with the distribution. 00022 * * Neither the name(s) of the copyright holders nor the names 00023 * of its contributors or of the Massacusetts Institute of 00024 * Technology or of the Italian Institute of Technology may be 00025 * used to endorse or promote products derived from this software 00026 * without specific prior written permission. 00027 * 00028 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00029 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00030 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00031 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00032 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00033 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00034 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00035 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00036 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00037 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00038 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00039 * POSSIBILITY OF SUCH DAMAGE. 00040 */ 00041 00042 00043 #ifndef _GURLS_CALIBRATESGD_H_ 00044 #define _GURLS_CALIBRATESGD_H_ 00045 00046 #include "gurls++/options.h" 00047 #include "gurls++/optlist.h" 00048 #include "gurls++/gmat2d.h" 00049 #include "gurls++/gmath.h" 00050 00051 #include "gurls++/paramsel.h" 00052 #include "gurls++/perf.h" 00053 #include "gurls++/gurls.h" 00054 00055 namespace gurls { 00056 00062 template <typename T> 00063 class ParamSelCalibrateSGD: public ParamSelection<T>{ 00064 00065 public: 00081 GurlsOptionsList* execute(const gMat2D<T>& X, const gMat2D<T>& Y, const GurlsOptionsList& opt); 00082 }; 00083 00084 template <typename T> 00085 GurlsOptionsList *ParamSelCalibrateSGD<T>::execute(const gMat2D<T>& X, const gMat2D<T>& Y, const GurlsOptionsList &opt) 00086 { 00087 // n_estimates = 1; 00088 const unsigned long n_estimates = 1; 00089 00090 // [n,d] = size(X); 00091 const unsigned long n = X.rows(); 00092 const unsigned long t = X.cols(); 00093 00094 GurlsOptionsList* tmp = new GurlsOptionsList("ParamSelCalibrateSGD", true); 00095 00096 OptTaskSequence *seq = new OptTaskSequence(); 00097 GurlsOptionsList * process = new GurlsOptionsList("processes", false); 00098 OptProcess* process1 = new OptProcess(); 00099 00100 *seq << "split:ho" << "kernel:linear" << "paramsel:hodual" << "optimizer:rlsdual"; 00101 *process1 << GURLS::compute << GURLS::compute << GURLS::computeNsave << GURLS::computeNsave; 00102 00103 tmp->addOpt("seq", seq); 00104 00105 process->addOpt("one", process1); 00106 tmp->addOpt("processes", process); 00107 00108 00109 if(tmp->hasOpt("hoperf")) 00110 tmp->removeOpt("hoperf"); 00111 if(tmp->hasOpt("singlelambda")) 00112 tmp->removeOpt("singlelambda"); 00113 00114 tmp->addOpt("hoperf", opt.getOptAsString("hoperf")); 00115 tmp->addOpt("singlelambda", new OptFunction(OptFunction::dynacast(opt.getOpt("singlelambda"))->getName())); 00116 00117 00118 GURLS g; 00119 00120 // sub_size = opt.subsize; 00121 const int subsize = static_cast<int>(opt.getOptAsNumber("subsize")); 00122 00123 unsigned long* idx = new unsigned long[n]; //will use only the first subsize elements 00124 T* lambdas = new T[n_estimates]; 00125 00126 gMat2D<T> Mx(subsize, t); 00127 gMat2D<T> My(subsize, Y.cols()); 00128 00129 // for i = 1:n_estimates, 00130 for(unsigned long i=0; i<n_estimates; ++i) 00131 { 00132 // idx = randsample(n, sub_size); 00133 randperm(n, idx); 00134 00135 // M = X(idx,:); 00136 subMatrixFromRows(X.getData(), n, t, idx, subsize, Mx.getData()); 00137 00138 // if ~exist([opt.calibfile '.mat'],'file') 00139 // fprintf('\n\tCalibrating...'); 00140 // %% Step 1 : Hold out parameter selection in the dual 00141 // name = opt.calibfile; 00142 // tmp.hoperf = opt.hoperf; 00143 // tmp = defopt(name); 00144 // tmp.seq = {'split:ho','kernel:linear','paramsel:hodual','rls:dual'}; 00145 // tmp.process{1} = [2,2,2,2]; 00146 // tmp.singlelambda = opt.singlelambda; 00147 00148 // gurls(M,y(idx,:),tmp,1); 00149 subMatrixFromRows(Y.getData(), Y.rows(), Y.cols(), idx, subsize, My.getData()); 00150 00151 g.run(Mx, My, *tmp, "one"); 00152 00153 // end 00154 // fprintf('\n\tLoading existing calibration'); 00155 // load([opt.calibfile '.mat']); 00156 // lambdas(i) = opt.singlelambda(opt.paramsel.lambdas); 00157 00158 const gMat2D<T> &ll = tmp->getOptValue<OptMatrix<gMat2D<T> > >("paramsel.lambdas"); 00159 lambdas[i] = opt.getOptAs<OptFunction>("singlelambda")->getValue(ll.getData(), ll.getSize()); 00160 00161 // % Add rescaling 00162 // end 00163 } 00164 00165 delete[] idx; 00166 00167 GurlsOptionsList* paramsel; 00168 00169 if(opt.hasOpt("paramsel")) 00170 { 00171 GurlsOptionsList* tmp_opt = new GurlsOptionsList("tmp"); 00172 tmp_opt->copyOpt("paramsel", opt); 00173 00174 paramsel = GurlsOptionsList::dynacast(tmp_opt->getOpt("paramsel")); 00175 tmp_opt->removeOpt("paramsel", false); 00176 delete tmp_opt; 00177 00178 paramsel->removeOpt("lambdas"); 00179 paramsel->removeOpt("W"); 00180 } 00181 else 00182 paramsel = new GurlsOptionsList("paramsel"); 00183 00184 00185 // params.lambdas = mean(lambdas); 00186 gMat2D<T> *lambda = new gMat2D<T>(1,1); 00187 lambda->getData()[0] = sumv(lambdas, n_estimates)/n_estimates; 00188 paramsel->addOpt("lambdas", new OptMatrix<gMat2D<T> >(*lambda)); 00189 00190 00191 // params.W = opt.rls.W; 00192 GurlsOptionsList* rls = tmp->getOptAs<GurlsOptionsList>("optimizer"); 00193 paramsel->addOpt("W", rls->getOpt("W")); 00194 rls->removeOpt("W", false); 00195 00196 delete tmp; 00197 delete[] lambdas; 00198 00199 return paramsel; 00200 } 00201 00202 00203 } 00204 00205 #endif // _GURLS_CALIBRATESGD_H_