PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // HyperRetrain.cc 00004 // 00005 // Copyright (C) 2003-2004 ApSTAT Technologies Inc. 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 // Author: Pascal Vincent 00036 00037 /* ******************************************************* 00038 * $Id: HyperRetrain.cc 7310 2007-05-24 18:04:57Z tihocan $ 00039 ******************************************************* */ 00040 00043 #include "HyperRetrain.h" 00044 #include "HyperLearner.h" 00045 #include <plearn_learners/testers/PTester.h> 00046 00047 namespace PLearn { 00048 using namespace std; 00049 00050 HyperRetrain::HyperRetrain(): 00051 provide_tester_expdir(false), 00052 call_forget(true) 00053 {} 00054 00055 PLEARN_IMPLEMENT_OBJECT( 00056 HyperRetrain, 00057 "Retrain a learner without changing its hyperparameters.", 00058 "It is possible to specify a new splitter in order for instance to\n" 00059 "retrain the current best learner on the entire (train + valid) dataset\n" 00060 "at the end of hyper-parameter optimization. This can be done by setting\n" 00061 "the 'splitter' option to something like:\n" 00062 " splitter = FractionSplitter(splits = 1 1 [0:1])\n" 00063 "\n" 00064 "One may also provide no splitter, and just continue training the\n" 00065 "learner by setting the 'call_forget' option to false (this can be\n" 00066 "useful after using a HyperSetOption to modify the learner's options).\n" 00067 ); 00068 00070 // declareOptions // 00072 void HyperRetrain::declareOptions(OptionList& ol) 00073 { 00074 declareOption(ol, "splitter", &HyperRetrain::splitter, 00075 OptionBase::buildoption, 00076 "Splitter to use for (re)training. If not provided, the current\n" 00077 "splitter will be used."); 00078 00079 declareOption(ol, "provide_tester_expdir", 00080 &HyperRetrain::provide_tester_expdir, 00081 OptionBase::buildoption, 00082 "Should the tester be provided with an expdir for retraining."); 00083 00084 declareOption(ol, "call_forget", &HyperRetrain::call_forget, 00085 OptionBase::buildoption, 00086 "Whether to call forget() before training. Note that even if set to\n" 00087 "0, forget() might be called in setTrainingSet(..) if a new splitter\n" 00088 "is provided."); 00089 00090 // Now call the parent class' declareOptions 00091 inherited::declareOptions(ol); 00092 } 00093 00095 // build_ // 00097 void HyperRetrain::build_() 00098 {} 00099 00101 // build // 00103 void HyperRetrain::build() 00104 { 00105 inherited::build(); 00106 build_(); 00107 } 00108 00110 // makeDeepCopyFromShallowCopy // 00112 void HyperRetrain::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00113 { 00114 inherited::makeDeepCopyFromShallowCopy(copies); 00115 deepCopyField(splitter, copies); 00116 } 00117 00119 // optimize // 00121 Vec HyperRetrain::optimize() 00122 { 00123 PP<PTester> tester = hlearner->tester; 00124 00125 string testerexpdir = ""; 00126 if(expdir!="" && provide_tester_expdir) 00127 testerexpdir = expdir+"retrain/"; 00128 tester->setExperimentDirectory(testerexpdir); 00129 00130 PP<Splitter> default_splitter = tester->splitter; 00131 00132 if (!splitter.isNull()) 00133 tester->splitter = splitter; 00134 00135 Vec results = tester->perform(call_forget); 00136 00137 // restore default splitter 00138 tester->splitter = default_splitter; 00139 return results; 00140 } 00141 00143 // getResultNames // 00145 TVec<string> HyperRetrain::getResultNames() const 00146 { 00147 return hlearner->tester->getStatNames(); 00148 } 00149 00150 00151 } // end of namespace PLearn 00152 00153 00154 /* 00155 Local Variables: 00156 mode:c++ 00157 c-basic-offset:4 00158 c-file-style:"stroustrup" 00159 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00160 indent-tabs-mode:nil 00161 fill-column:79 00162 End: 00163 */ 00164 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :