PLearn 0.1
LearnerProcessedVMatrix.cc
Go to the documentation of this file.
00001 
00002 // -*- C++ -*-
00003 
00004 // LearnerProcessedVMatrix.cc
00005 //
00006 // Copyright (C) 2003  Pascal Vincent 
00007 // 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 /* *******************************************************      
00037  * $Id: LearnerProcessedVMatrix.cc 5171 2006-03-13 20:45:38Z lamblin $ 
00038  ******************************************************* */
00039 
00041 #include "LearnerProcessedVMatrix.h"
00042 
00043 namespace PLearn {
00044 using namespace std;
00045 
00046 
00047 PLEARN_IMPLEMENT_OBJECT(LearnerProcessedVMatrix, "ONE LINE DESCR", 
00048                         "LearnerProcessedVMatrix implements a VMatrix processed on the fly by a learner (which will optionally be first trained on the source vmatrix)");
00049 
00050 
00051 LearnerProcessedVMatrix::LearnerProcessedVMatrix()
00052     :train_learner('-')
00053 {
00054     build_();
00055 }
00056 
00057 void LearnerProcessedVMatrix::getNewRow(int i, const Vec& v) const
00058 {
00059     static Vec sv;
00060     sv.resize(source.width());
00061     source->getRow(i,sv);
00062     int nin = source->inputsize();
00063     int nout = learner->outputsize();
00064     Vec input = sv.subVec(0,nin);
00065     Vec output = v.subVec(0,nout);
00066     learner->computeOutput(input,output);
00067     int rest = source.width()-nin;
00068     v.subVec(nout,rest) << sv.subVec(nin,rest);
00069 }
00070 
00071 void LearnerProcessedVMatrix::declareOptions(OptionList& ol)
00072 {
00073     declareOption(ol, "source", &LearnerProcessedVMatrix::source, OptionBase::buildoption,
00074                   "The source vmatrix whose inputs wil be porcessed by the learner.\n"
00075                   "If present, the target and weight columns will be appended to the processed input in the resulting matrix.");
00076   
00077     declareOption(ol, "learner", &LearnerProcessedVMatrix::learner, OptionBase::buildoption,
00078                   "The learner used to process the VMat's input.");
00079 
00080     declareOption(ol, "train_learner", &LearnerProcessedVMatrix::train_learner, OptionBase::buildoption, 
00081                   "Indicates if the learner should be trained on the source, upon building, and if so on what part.\n"
00082                   " '-': don't train \n"
00083                   " 'S': supervised training using input and target (possibly weighted if weight is  present) \n"
00084                   " 'U': unsupervised training using only input part (possibly weighted if weight is present). \n");
00085 
00086     // Now call the parent class' declareOptions
00087     inherited::declareOptions(ol);
00088 }
00089 
00090 void LearnerProcessedVMatrix::build_()
00091 {
00092     if (source && learner) {
00093         switch(train_learner) {
00094         case '-':
00095             break;
00096         case 'S':
00097             learner->setTrainingSet(source);
00098             learner->setTrainStatsCollector(new VecStatsCollector());
00099             learner->train();
00100             break;
00101         case 'U':
00102         {
00103             VMat inputs = source.subMatColumns(0,source->inputsize());
00104             learner->setTrainingSet(inputs);
00105             learner->setTrainStatsCollector(new VecStatsCollector());
00106             learner->train();
00107         }
00108         }
00109         length_ = source->length();
00110         width_ = learner->outputsize() + source->targetsize() + source->weightsize();
00111     }
00112 }
00113 
00114 // ### Nothing to add here, simply calls build_
00115 void LearnerProcessedVMatrix::build()
00116 {
00117     inherited::build();
00118     build_();
00119 }
00120 
00121 void LearnerProcessedVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00122 {
00123     inherited::makeDeepCopyFromShallowCopy(copies);
00124     deepCopyField(source, copies);
00125     deepCopyField(learner, copies);
00126 }
00127 
00128 } // end of namespace PLearn
00129 
00130 
00131 /*
00132   Local Variables:
00133   mode:c++
00134   c-basic-offset:4
00135   c-file-style:"stroustrup"
00136   c-file-offsets:((innamespace . 0)(inline-open . 0))
00137   indent-tabs-mode:nil
00138   fill-column:79
00139   End:
00140 */
00141 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines