PLearn 0.1
SequentialLearner.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // SequentialLearner.cc
00004 //
00005 // Copyright (C) 2003 Rejean Ducharme, Yoshua Bengio
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 
00038 #include "SequentialLearner.h"
00039 
00040 namespace PLearn {
00041 using namespace std;
00042 
00043 
00044 PLEARN_IMPLEMENT_ABSTRACT_OBJECT(SequentialLearner, "ONE LINE DESCR", "NO HELP");
00045 
00046 SequentialLearner::SequentialLearner()
00047     : last_train_t(-1), last_call_train_t(-1), last_test_t(-1),
00048       init_train_size(1), max_seq_len(-1), max_train_len(-1), train_step(1), horizon(1),
00049       outputsize_(1)
00050 {}
00051 
00052 void SequentialLearner::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00053 {
00054     inherited::makeDeepCopyFromShallowCopy(copies);
00055     deepCopyField(predictions, copies);
00056     deepCopyField(errors, copies);
00057 } 
00058 
00059 void SequentialLearner::build_()
00060 {
00061     if(max_seq_len != -1)
00062     {
00063         if( outputsize() == 0 )
00064             PLERROR("SequentialLearner::build_\n"
00065                     "outputsize() returns 0 but predictions will later be assumed to have nonzero width.");
00066         predictions.resize(max_seq_len, outputsize());
00067         predictions.fill(MISSING_VALUE);
00068 
00069         int n_test_costs = nTestCosts();
00070         if( n_test_costs > 0 )
00071         {
00072             errors.resize(max_seq_len, n_test_costs);
00073             errors.fill(MISSING_VALUE);
00074         }
00075     }  
00076 }
00077   
00078 void SequentialLearner::build()
00079 {
00080     inherited::build();
00081     build_();
00082 }
00083 
00084 void SequentialLearner::declareOptions(OptionList& ol)
00085 {
00086     declareOption(ol, "init_train_size", &SequentialLearner::init_train_size, OptionBase::buildoption,
00087                   "Before the length of train_set reaches init_train_size, train doesn't do anything.\n"
00088                   "Default = 1.");
00089 
00090     declareOption(ol, "max_seq_len", &SequentialLearner::max_seq_len,
00091                   OptionBase::buildoption,
00092                   "Maximum length that the training matrix will ever reach;\n"
00093                   "this is used as an optimization to preallocate buffers\n"
00094                   "and avoid reallocations as training/testing proceeds.");
00095 
00096     declareOption(ol, "max_train_len", &SequentialLearner::max_train_len,
00097                   OptionBase::buildoption,
00098                   "Maximum number of (input,target) pairs used for training;\n"
00099                   "for longer training sequences, only the last max_train_len\n"
00100                   "pairs are actually used for training");
00101 
00102     declareOption(ol, "train_step", &SequentialLearner::train_step,
00103                   OptionBase::buildoption,
00104                   "How often we have to re-train a model;\n"
00105                   "value of 1 = after every time step");
00106 
00107     declareOption(ol, "horizon", &SequentialLearner::horizon,
00108                   OptionBase::buildoption,
00109                   "How much to offset the target columns with respect to\n"
00110                   "the input columns");
00111 
00112     declareOption(ol, "outputsize", &SequentialLearner::outputsize_,
00113                   OptionBase::buildoption,
00114                   "Size of the output vector (number of outputs)");
00115 
00116     inherited::declareOptions(ol);
00117 }
00118 
00119 void SequentialLearner::setTrainingSet(VMat training_set, bool call_forget)
00120 {
00121     train_set = training_set;
00122     if (call_forget) forget();
00123 }
00124 
00125 void SequentialLearner::forget()
00126 {
00127     if (predictions.isNotEmpty())
00128         predictions.fill(MISSING_VALUE);
00129     if (errors.isNotEmpty())
00130         errors.fill(MISSING_VALUE);
00131     last_train_t      = -1;
00132     last_call_train_t = -1;
00133     last_test_t       = -1;
00134 }
00135 
00137 int SequentialLearner::outputsize() const
00138 { return outputsize_; }
00139 
00140 int SequentialLearner::nTestCosts() const
00141 { return getTestCostNames().size(); }
00142 
00143 void SequentialLearner::computeOutputAndCosts(const Vec& input,
00144                                               const Vec& target, Vec& output, Vec& costs) const
00145 { PLERROR("The method computeOutputAndCosts is not defined for this SequentialLearner"); }
00146 
00147 void SequentialLearner::computeCostsOnly(const Vec& input, const Vec& target,
00148                                          Vec& costs) const
00149 { PLERROR("The method computeCostsOnly is not defined for this SequentialLearner"); }
00150 
00151 void SequentialLearner::computeOutput(const Vec& input, Vec& output) const
00152 { PLERROR("The method computeOutput is not defined for this SequentialLearner"); }
00153 
00154 void SequentialLearner::computeCostsFromOutputs(const Vec& input,
00155                                                 const Vec& output, const Vec& target, Vec& costs) const
00156 { PLERROR("The method computeCostsFromOutputs is not defined for this SequentialLearner"); }
00157 
00158 void SequentialLearner::matlabSave(const string& matlab_subdir)
00159 {
00160     inherited::matlabSave(matlab_subdir);
00161 
00162     string save_dir = append_slash(getExperimentDirectory()) + matlab_subdir;
00163     Vec dummy, add(1); add[0] = 0;
00164 
00165     TVec<string> cost_names = getTestCostNames();  
00166     Vec startX(1, real(sequenceStart()));
00167     for(int g=0; g < cost_names.length(); g++)
00168         PLearn::matlabSave(save_dir, cost_names[g], 
00169                            startX,
00170                            getCostSequence(g), add, dummy);       
00171 
00172     PLearn::matlabSave(save_dir, "Predictions", predictions, dummy, dummy);
00173 }
00174 
00175 } // end of namespace PLearn
00176 
00177 
00178 /*
00179   Local Variables:
00180   mode:c++
00181   c-basic-offset:4
00182   c-file-style:"stroustrup"
00183   c-file-offsets:((innamespace . 0)(inline-open . 0))
00184   indent-tabs-mode:nil
00185   fill-column:79
00186   End:
00187 */
00188 // 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