Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

HyperOptimizer.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 2002 Pascal Vincent, Frederic Morin 00005 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // 00009 // 1. Redistributions of source code must retain the above copyright 00010 // notice, this list of conditions and the following disclaimer. 00011 // 00012 // 2. Redistributions in binary form must reproduce the above copyright 00013 // notice, this list of conditions and the following disclaimer in the 00014 // documentation and/or other materials provided with the distribution. 00015 // 00016 // 3. The name of the authors may not be used to endorse or promote 00017 // products derived from this software without specific prior written 00018 // permission. 00019 // 00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 // 00031 // This file is part of the PLearn library. For more information on the PLearn 00032 // library, go to the PLearn Web site at www.plearn.org 00033 00034 00035 00036 00037 /* ******************************************************* 00038 * $Id: HyperOptimizer.cc,v 1.5 2004/02/20 21:11:48 chrish42 Exp $ 00039 * AUTHORS: Pascal Vincent & Frederic Morin 00040 * This file is part of the PLearn library. 00041 ******************************************************* */ 00042 00043 #include "HyperOptimizer.h" 00044 00045 namespace PLearn { 00046 00047 // ###### HyperOptimizer ###################################################### 00048 00049 PLEARN_IMPLEMENT_ABSTRACT_OBJECT(HyperOptimizer, "ONE LINE DESCR", "NO HELP"); 00050 00051 void 00052 HyperOptimizer::declareOptions(OptionList &ol) 00053 { 00054 declareOption(ol, "objective", &HyperOptimizer::objective, OptionBase::buildoption, 00055 "Objective function to be optimized"); 00056 declareOption(ol, "substrategy", &HyperOptimizer::substrategy, OptionBase::buildoption, 00057 "Embedded strategies performed during optimization"); 00058 inherited::declareOptions(ol); 00059 } 00060 00061 00062 // ###### HSetVal ############################################################# 00063 00064 PLEARN_IMPLEMENT_OBJECT(HSetVal, "ONE LINE DESCR", "NO HELP"); 00065 00066 void 00067 HSetVal::declareOptions(OptionList &ol) 00068 { 00069 declareOption(ol, "param", &HSetVal::param, OptionBase::buildoption, 00070 "Hyperparameters' alias to set the value for"); 00071 declareOption(ol, "value", &HSetVal::value, OptionBase::buildoption, 00072 "Value of the hyperparameters to be set"); 00073 inherited::declareOptions(ol); 00074 } 00075 00076 void 00077 HSetVal::optimize(PP<Learner> learner, const VMat &dataset, const HAliases &aliases) 00078 { 00079 string alias = const_cast<HAliases &>(aliases)[param]; 00080 learner->setOption(alias, value); 00081 } 00082 00083 00084 // ###### HTryAll ############################################################# 00085 00086 PLEARN_IMPLEMENT_OBJECT(HTryAll, "ONE LINE DESCR", "NO HELP"); 00087 00088 void 00089 HTryAll::declareOptions(OptionList &ol) 00090 { 00091 declareOption(ol, "param", &HTryAll::param, OptionBase::buildoption, 00092 "Hyperparameters' alias to set the values for"); 00093 declareOption(ol, "values", &HTryAll::values, OptionBase::buildoption, 00094 "Values of the hyperparameters to try"); 00095 inherited::declareOptions(ol); 00096 } 00097 00098 void 00099 HTryAll::optimize(PP<Learner> learner, const VMat &dataset, const HAliases &aliases) 00100 { 00101 string alias = const_cast<HAliases &>(aliases)[param]; 00102 Vec results(values.size()); 00103 00104 for (int k = 0; k < values.size(); ++k) { 00105 learner->setOption(alias, values[k]); 00106 results[k] = objective->test(learner, dataset); 00107 } 00108 learner->setOption(alias, values[argmin(results)]); 00109 } 00110 00111 00112 // ###### HCoordinateDescent ################################################## 00113 00114 PLEARN_IMPLEMENT_OBJECT(HCoordinateDescent, "ONE LINE DESCR", "NO HELP"); 00115 00116 void 00117 HCoordinateDescent::declareOptions(OptionList &ol) 00118 { 00119 declareOption(ol, "substrategy", &HCoordinateDescent::substrategy, OptionBase::buildoption, 00120 "List of HyperOptimizers to perform optimization on"); 00121 declareOption(ol, "max_iterations", &HCoordinateDescent::max_iterations, OptionBase::buildoption, 00122 "Maximum number of iterations to perform"); 00123 inherited::declareOptions(ol); 00124 } 00125 00126 void 00127 HCoordinateDescent::optimize(PP<Learner> learner, const VMat &dataset, const HAliases &aliases) 00128 { 00129 for (int i = 0; i < max_iterations; ++i) 00130 for (int j = 0; j < substrategy.size(); ++j) 00131 substrategy[j]->optimize(learner, dataset, aliases); 00132 } 00133 00134 // ###### HTryCombinations #################################################### 00135 00136 PLEARN_IMPLEMENT_OBJECT(HTryCombinations, "ONE LINE DESCR", "NO HELP"); 00137 00138 void 00139 HTryCombinations::declareOptions(OptionList &ol) 00140 { 00141 declareOption(ol, "params", &HTryCombinations::params, OptionBase::buildoption, 00142 "List of hyperparameters to optimize"); 00143 declareOption(ol, "values", &HTryCombinations::values, OptionBase::buildoption, 00144 "List of values to select for optimization of the given hyperparameters"); 00145 inherited::declareOptions(ol); 00146 } 00147 00148 void 00149 HTryCombinations::optimize(PP<Learner> learner, const VMat &dataset, const HAliases &aliases) 00150 { 00151 Array<string> str_aliases(params.size()); 00152 for (int i = 0; i < params.size(); ++i) 00153 str_aliases[i] = const_cast<HAliases &>(aliases)[params[i]]; 00154 recursive_optimize(learner, dataset, str_aliases, 0); 00155 } 00156 00157 real 00158 HTryCombinations::recursive_optimize(PP<Learner> learner, const VMat &dataset, const Array<string> &aliases, int i) 00159 { 00160 Vec results(values[i].size()); 00161 00162 for (int k = 0; k < values[i].size(); ++k) { 00163 learner->setOption(aliases[i], values[i][k]); 00164 if (i == params.size() - 1) 00165 results[k] = objective->test(learner, dataset); 00166 else 00167 results[k] = recursive_optimize(learner, dataset, aliases, i + 1); 00168 } 00169 int best_k = argmin(results); 00170 learner->setOption(aliases[i], values[i][best_k]); 00171 return results[best_k]; 00172 } 00173 00174 void 00175 HTryCombinations::build() 00176 { 00177 inherited::build(); 00178 _build(); 00179 } 00180 00181 void 00182 HTryCombinations::_build() 00183 { 00184 if (values.size() != params.size()) 00185 PLERROR("HTryCombinations::_build() - params.size() must match values.size()"); 00186 } 00187 00188 }; // end of namespace PLearn

Generated on Tue Aug 17 15:55:15 2004 for PLearn by doxygen 1.3.7