PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // RankingFromKernel.cc 00004 // 00005 // Copyright (C) 2006 Pierre-Jean L Heureux 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: Pierre-Jean L Heureux 00040 00044 #include "RankingFromKernel.h" 00045 #include <plearn/io/PPath.h> 00046 00047 namespace PLearn { 00048 using namespace std; 00049 00050 PLEARN_IMPLEMENT_OBJECT( 00051 RankingFromKernel, 00052 "This learner will compute \\frac{\\sum_{actives} K(i,j)}{\\sum_{inactives} K(i,j)} for a given kernel K. ", 00053 "A lift_output is available to compute ranking based costs. The target must be 1 or 0. \n"); 00054 00055 RankingFromKernel::RankingFromKernel() 00056 /* ### Initialize all fields to their default value here */ 00057 { 00058 // ... 00059 00060 // ### You may (or not) want to call build_() to finish building the object 00061 // ### (doing so assumes the parent classes' build_() have been called too 00062 // ### in the parent classes' constructors, something that you must ensure) 00063 } 00064 00065 void RankingFromKernel::declareOptions(OptionList& ol) 00066 { 00067 // ### Declare all of this object's options here 00068 // ### For the "flags" of each option, you should typically specify 00069 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00070 // ### OptionBase::tuningoption. Another possible flag to be combined with 00071 // ### is OptionBase::nosave 00072 00073 // ### ex: 00074 declareOption(ol, "logKernel", &RankingFromKernel::logKernel, OptionBase::buildoption, 00075 "A kernel taking an input and returning the log of its result."); 00076 00077 // Now call the parent class' declareOptions 00078 inherited::declareOptions(ol); 00079 } 00080 00081 void RankingFromKernel::build_() 00082 { 00083 // ### This method should do the real building of the object, 00084 // ### according to set 'options', in *any* situation. 00085 // ### Typical situations include: 00086 // ### - Initial building of an object from a few user-specified options 00087 // ### - Building of a "reloaded" object: i.e. from the complete set of all serialised options. 00088 // ### - Updating or "re-building" of an object after a few "tuning" options have been modified. 00089 // ### You should assume that the parent class' build_() has already been called. 00090 } 00091 00092 // ### Nothing to add here, simply calls build_ 00093 void RankingFromKernel::build() 00094 { 00095 inherited::build(); 00096 build_(); 00097 } 00098 00099 00100 void RankingFromKernel::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00101 { 00102 inherited::makeDeepCopyFromShallowCopy(copies); 00103 00104 // ### Call deepCopyField on all "pointer-like" fields 00105 // ### that you wish to be deepCopied rather than 00106 // ### shallow-copied. 00107 // ### ex: 00108 // deepCopyField(trainvec, copies); 00109 00110 // ### Remove this line when you have fully implemented this method. 00111 PLERROR("RankingFromKernel::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00112 } 00113 00114 00115 int RankingFromKernel::outputsize() const 00116 { 00117 // Compute and return the size of this learner's output (which typically 00118 // may depend on its inputsize(), targetsize() and set options). 00119 return 1; 00120 } 00121 00122 void RankingFromKernel::forget() 00123 { 00126 00132 } 00133 00134 void RankingFromKernel::train() 00135 { 00136 // The role of the train method is to bring the learner up to stage==nstages, 00137 // updating train_stats with training costs measured on-line in the process. 00138 00139 /* TYPICAL CODE: 00140 00141 static Vec input // static so we don't reallocate/deallocate memory each time... 00142 static Vec target // (but be careful that static means shared!) 00143 input.resize(inputsize()) // the train_set's inputsize() 00144 target.resize(targetsize()) // the train_set's targetsize() 00145 real weight 00146 00147 // This generic PLearner method does a number of standard stuff useful for 00148 // (almost) any learner, and return 'false' if no training should take 00149 // place. See PLearner.h for more details. 00150 if (!initTrain()) 00151 return; 00152 00153 while(stage<nstages) 00154 { 00155 // clear statistics of previous epoch 00156 train_stats->forget() 00157 00158 //... train for 1 stage, and update train_stats, 00159 // using train_set->getSample(input, target, weight) 00160 // and train_stats->update(train_costs) 00161 00162 ++stage 00163 train_stats->finalize() // finalize statistics for this epoch 00164 } 00165 */ 00166 if (train_set->targetsize() != 1) PLERROR("This PLearner is not built for multi-target problems"); 00167 PLWARNING("Train not implemented"); 00168 } 00169 00170 00171 void RankingFromKernel::computeOutput(const Vec& input, Vec& output) const 00172 { 00173 // Compute the output from the input. 00174 // int nout = outputsize(); 00175 // output.resize(nout); 00176 // ... 00177 int i; 00178 real log_k,log_result, weight; 00179 00180 log_act.resize(0); 00181 log_inact.resize(0); 00182 for (i=0; i < train_set->length();i++){ 00183 train_set->getExample(i,x, target, weight); 00184 log_k = logKernel->evaluate(input,x); 00185 if ( fast_exact_is_equal(target[0],1)) { 00186 log_act.append(log_k); 00187 }else{ 00188 log_inact.append(log_k); 00189 } 00190 } 00191 log_result = logadd(log_act) - logadd(log_inact); 00192 output.resize(1); 00193 output[0] = exp(log_result); 00194 } 00195 00196 void RankingFromKernel::computeCostsFromOutputs(const Vec& input, const Vec& output, 00197 const Vec& target, Vec& costs) const 00198 { 00199 costs.resize(nTestCosts()); 00200 if(fast_exact_is_equal(target[0],1)) 00201 costs[0] = output[0]; 00202 else 00203 costs[0] = -output[0]; 00204 } 00205 00206 TVec<string> RankingFromKernel::getTestCostNames() const 00207 { 00208 static TVec<string> cost; 00209 if (cost.isEmpty()) 00210 cost.append("lift_output"); 00211 return cost; 00212 } 00213 00214 TVec<string> RankingFromKernel::getTrainCostNames() const 00215 { 00216 // Return the names of the objective costs that the train method computes and 00217 // for which it updates the VecStatsCollector train_stats 00218 // (these may or may not be exactly the same as what's returned by getTestCostNames). 00219 // ... 00220 00221 static TVec<string> costs; 00222 costs.resize(0); 00223 return costs; 00224 } 00225 00226 00227 } // end of namespace PLearn 00228 00229 00230 /* 00231 Local Variables: 00232 mode:c++ 00233 c-basic-offset:4 00234 c-file-style:"stroustrup" 00235 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00236 indent-tabs-mode:nil 00237 fill-column:79 00238 End: 00239 */ 00240 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :