PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // TestLearner.cc 00004 // 00005 // Copyright (C) 2005 Dan Popovici 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 /* ******************************************************* 00036 * $Id: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $ 00037 ******************************************************* */ 00038 00039 // Authors: Dan Popovici 00040 00044 #include "TestLearner.h" 00045 00046 namespace PLearn { 00047 using namespace std; 00048 00049 PLEARN_IMPLEMENT_OBJECT( 00050 TestLearner, 00051 "ONE LINE DESCRIPTION", 00052 "MULTI-LINE \nHELP"); 00053 00054 TestLearner::TestLearner() 00055 /* ### Initialize all fields to their default value here */ 00056 { 00057 // ... 00058 00059 // ### You may (or not) want to call build_() to finish building the object 00060 // ### (doing so assumes the parent classes' build_() have been called too 00061 // ### in the parent classes' constructors, something that you must ensure) 00062 } 00063 00064 void TestLearner::declareOptions(OptionList& ol) 00065 { 00066 // ### Declare all of this object's options here 00067 // ### For the "flags" of each option, you should typically specify 00068 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00069 // ### OptionBase::tuningoption. Another possible flag to be combined with 00070 // ### is OptionBase::nosave 00071 00072 // ### ex: 00073 // declareOption(ol, "myoption", &TestLearner::myoption, OptionBase::buildoption, 00074 // "Help text describing this option"); 00075 // ... 00076 00077 // Now call the parent class' declareOptions 00078 00079 declareOption(ol,"dir_path", &TestLearner::dir_path, OptionBase::buildoption, 00080 "The Path to the training data"); 00081 inherited::declareOptions(ol); 00082 } 00083 00084 void TestLearner::build_() 00085 { 00086 // ### This method should do the real building of the object, 00087 // ### according to set 'options', in *any* situation. 00088 // ### Typical situations include: 00089 // ### - Initial building of an object from a few user-specified options 00090 // ### - Building of a "reloaded" object: i.e. from the complete set of all serialised options. 00091 // ### - Updating or "re-building" of an object after a few "tuning" options have been modified. 00092 // ### You should assume that the parent class' build_() has already been called. 00093 } 00094 00095 // ### Nothing to add here, simply calls build_ 00096 void TestLearner::build() 00097 { 00098 inherited::build(); 00099 build_(); 00100 } 00101 00102 00103 void TestLearner::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00104 { 00105 inherited::makeDeepCopyFromShallowCopy(copies); 00106 00107 // ### Call deepCopyField on all "pointer-like" fields 00108 // ### that you wish to be deepCopied rather than 00109 // ### shallow-copied. 00110 // ### ex: 00111 // deepCopyField(trainvec, copies); 00112 00113 // ### Remove this line when you have fully implemented this method. 00114 PLERROR("TestLearner::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00115 00116 00117 } 00118 00119 00120 int TestLearner::outputsize() const 00121 { 00122 // Compute and return the size of this learner's output (which typically 00123 // may depend on its inputsize(), targetsize() and set options). 00124 return 1 ; 00125 } 00126 00127 void TestLearner::forget() 00128 { 00131 00138 stage = 0 ; 00139 } 00140 00141 void TestLearner::train() 00142 { 00143 // The role of the train method is to bring the learner up to stage==nstages, 00144 // updating train_stats with training costs measured on-line in the process. 00145 00146 /* TYPICAL CODE: 00147 00148 static Vec input // static so we don't reallocate/deallocate memory each time... 00149 static Vec target // (but be careful that static means shared!) 00150 input.resize(inputsize()) // the train_set's inputsize() 00151 target.resize(targetsize()) // the train_set's targetsize() 00152 real weight 00153 */ 00154 00155 if (!train_stats) // make a default stats collector, in case there's none 00156 train_stats = new VecStatsCollector() ; 00157 00158 train_costs.resize(1); 00159 train_costs[0] = 0 ; 00160 00161 train_stats->update(train_costs) ; 00162 train_stats->finalize() ; // finalize statistics for this epoch 00163 } 00164 00165 00166 void TestLearner::computeOutput(const Vec& input, Vec& output) const 00167 { 00168 // Compute the output from the input. 00169 int nout = outputsize(); 00170 output.resize(nout); 00171 output[0] = 1 ; 00172 // ... 00173 } 00174 00175 void TestLearner::computeCostsFromOutputs(const Vec& input, const Vec& output, 00176 const Vec& target, Vec& costs) const 00177 { 00178 // Compute the costs from *already* computed output. 00179 costs.resize(1) ; 00180 costs[0] = 1 ; 00181 } 00182 00183 TVec<string> TestLearner::getTestCostNames() const 00184 { 00185 // Return the names of the costs computed by computeCostsFromOutpus 00186 // (these may or may not be exactly the same as what's returned by getTrainCostNames). 00187 // ... 00188 TVec<string> t(1) ; 00189 t[0] = "binary_class_error" ; 00190 return t ; 00191 } 00192 00193 TVec<string> TestLearner::getTrainCostNames() const 00194 { 00195 TVec<string> t(1) ; 00196 t[0] = "binary_class_error" ; 00197 return t ; 00198 // Return the names of the objective costs that the train method computes and 00199 // for which it updates the VecStatsCollector train_stats 00200 // (these may or may not be exactly the same as what's returned by getTestCostNames). 00201 // ... 00202 } 00203 00204 00205 } // end of namespace PLearn 00206 00207 00208 /* 00209 Local Variables: 00210 mode:c++ 00211 c-basic-offset:4 00212 c-file-style:"stroustrup" 00213 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00214 indent-tabs-mode:nil 00215 fill-column:79 00216 End: 00217 */ 00218 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :