PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // StatefulLearner.cc 00004 // 00005 // Copyright (C) 2004 Réjean Ducharme 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: StatefulLearner.cc 3994 2005-08-25 13:35:03Z chapados $ 00037 ******************************************************* */ 00038 00039 // Authors: Réjean Ducharme 00040 00044 #include "StatefulLearner.h" 00045 00046 namespace PLearn { 00047 using namespace std; 00048 00049 StatefulLearner::StatefulLearner() 00050 : current_test_t(-1), test_start_time(-1) 00051 {} 00052 00053 PLEARN_IMPLEMENT_ABSTRACT_OBJECT( 00054 StatefulLearner, 00055 "PLearner with an internal state", 00056 "PLearner with an internal state.\n" 00057 "It replaces, for efficiency and compatibility reasons, SequentialLearner."); 00058 00059 void StatefulLearner::declareOptions(OptionList& ol) 00060 { 00061 declareOption( 00062 ol, "test_start_time", &StatefulLearner::test_start_time, 00063 OptionBase::buildoption, 00064 "Initial time at which testing should be started."); 00065 00066 inherited::declareOptions(ol); 00067 } 00068 00069 void StatefulLearner::build_() 00070 {} 00071 00072 // ### Nothing to add here, simply calls build_ 00073 void StatefulLearner::build() 00074 { 00075 inherited::build(); 00076 build_(); 00077 } 00078 00079 void StatefulLearner::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00080 { 00081 inherited::makeDeepCopyFromShallowCopy(copies); 00082 } 00083 00084 void StatefulLearner::forget() 00085 { /* empty */ } 00086 00087 void StatefulLearner::resetInternalState() 00088 { 00089 current_test_t = test_start_time; 00090 } 00091 00092 void StatefulLearner::computeOutput(const Vec& input, Vec& output) const 00093 { 00094 // PLWARNING("You called StatefulLearner::computeOutput(...), are you sure you don't want to use computeOutputAndCosts(...) instead???"); 00095 00096 // These cannot be static because of potential re-entrancy problem upon 00097 // recursive calls 00098 Vec tmp_target(targetsize()); 00099 Vec tmp_costs(nTestCosts()); 00100 tmp_target.resize(targetsize()); 00101 tmp_costs.resize(nTestCosts()); 00102 computeOutputAndCosts(input, tmp_target, output, tmp_costs); 00103 } 00104 00105 void StatefulLearner::computeCostsFromOutputs(const Vec& input, const Vec& output, 00106 const Vec& target, Vec& costs) const 00107 { 00108 PLERROR("The method computeCostsFromOutputs is not defined and has no meaning for a StatefulLearner"); 00109 } 00110 00111 void StatefulLearner::computeCostsOnly(const Vec& input, const Vec& target, Vec& costs) const 00112 { 00113 PLWARNING("You called StatefulLearner::computeCostsOnly(...), are you sure you don't want to use computeOutputAndCosts(...) instead???"); 00114 00115 static Vec tmp_output; 00116 tmp_output.resize(outputsize()); 00117 computeOutputAndCosts(input, target, tmp_output, costs); 00118 } 00119 00120 void StatefulLearner::setTrainingSet(VMat training_set, bool call_forget) 00121 { 00122 train_set = training_set; 00123 inputsize_ = train_set->inputsize(); 00124 targetsize_ = train_set->targetsize(); 00125 weightsize_ = train_set->weightsize(); 00126 00127 if ( call_forget ) 00128 forget(); 00129 } 00130 00131 void StatefulLearner::setTestSet(VMat testset) 00132 {} 00133 00134 bool StatefulLearner::isStatefulLearner() const 00135 { 00136 return true; 00137 } 00138 00139 void StatefulLearner::setTestStartTime(int t) 00140 { 00141 test_start_time = t; 00142 } 00143 00144 } // end of namespace PLearn 00145 00146 00147 /* 00148 Local Variables: 00149 mode:c++ 00150 c-basic-offset:4 00151 c-file-style:"stroustrup" 00152 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00153 indent-tabs-mode:nil 00154 fill-column:79 00155 End: 00156 */ 00157 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :