PLearn 0.1
ComputePurenneError.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // ComputePurenneError.cc
00004 // Copyright (c) 1998-2002 Pascal Vincent
00005 // Copyright (C) 1999-2002 Yoshua Bengio and University of Montreal
00006 // Copyright (c) 2002 Jean-Sebastien Senecal, Xavier Saint-Mleux, Rejean Ducharme
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  * $Id: ComputePurenneError.cc, v 1.0 2004/07/19 10:00:00 Bengio/Kegl/Godbout        *
00039  * This file is part of the PLearn library.                                     *
00040  ******************************************************************************** */
00041 
00042 #include "ComputePurenneError.h"
00043 
00044 namespace PLearn {
00045 using namespace std;
00046 
00047 PLEARN_IMPLEMENT_OBJECT(ComputePurenneError,
00048                         "A PLearner to compute the prediction error of Vincent.", 
00049                         "\n"
00050     );
00051 
00052 ComputePurenneError::ComputePurenneError()  
00053 {
00054 }
00055 
00056 ComputePurenneError::~ComputePurenneError()
00057 {
00058 }
00059 
00060 void ComputePurenneError::declareOptions(OptionList& ol)
00061 {
00062     inherited::declareOptions(ol);
00063 }
00064 
00065 void ComputePurenneError::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00066 {
00067     inherited::makeDeepCopyFromShallowCopy(copies);
00068 }
00069 
00070 void ComputePurenneError::build()
00071 {
00072     inherited::build();
00073     build_();
00074 }
00075 
00076 void ComputePurenneError::build_()
00077 {
00078 }
00079 
00080 void ComputePurenneError::train()
00081 {
00082     int row;
00083     Vec sample_input(train_set->inputsize());
00084     Vec sample_target(train_set->targetsize());
00085     real sample_weight;
00086     Vec sample_output(2);
00087     Vec sample_costs(4);
00088     ProgressBar* pb = NULL;
00089     if (report_progress)
00090     {
00091         pb = new ProgressBar("Purenne error: computing the train statistics: ", train_set->length());
00092     } 
00093     train_stats->forget();
00094     for (row = 0; row < train_set->length(); row++)
00095     {  
00096         train_set->getExample(row, sample_input, sample_target, sample_weight);
00097         computeOutput(sample_input, sample_output);
00098         computeCostsFromOutputs(sample_input, sample_output, sample_target, sample_costs); 
00099         train_stats->update(sample_costs);
00100         if (report_progress) pb->update(row);
00101     }
00102     train_stats->finalize();
00103     if (report_progress) delete pb; 
00104 }
00105 
00106 void ComputePurenneError::forget()
00107 {
00108 }
00109 
00110 int ComputePurenneError::outputsize() const
00111 {
00112     return 2;
00113 }
00114 
00115 TVec<string> ComputePurenneError::getTrainCostNames() const
00116 {
00117     TVec<string> return_msg(4);
00118     return_msg[0] = "mse";
00119     return_msg[1] = "class_error";
00120     return_msg[2] = "linear_class_error";
00121     return_msg[3] = "square_class_error";
00122     return return_msg;
00123 }
00124 
00125 TVec<string> ComputePurenneError::getTestCostNames() const
00126 { 
00127     return getTrainCostNames();
00128 }
00129 
00130 void ComputePurenneError::computeOutput(const Vec& inputv, Vec& outputv) const
00131 {
00132     outputv[0] = inputv[0];
00133     outputv[1] = inputv[1];
00134 }
00135 
00136 void ComputePurenneError::computeOutputAndCosts(const Vec& inputv, const Vec& targetv, Vec& outputv, Vec& costsv) const
00137 {
00138     computeOutput(inputv, outputv);
00139     computeCostsFromOutputs(inputv, outputv, targetv, costsv);
00140 }
00141 
00142 void ComputePurenneError::computeCostsFromOutputs(const Vec& inputv, const Vec& outputv, const Vec& targetv, Vec& costsv) const
00143 {
00144     costsv[0] = pow((outputv[0] - targetv[0]), 2);
00145     costsv[1] = outputv[1] == targetv[1] ? 0 : 1;
00146     costsv[2] = int(round(fabs(outputv[1] - targetv[1])));
00147     costsv[3] = pow((outputv[1] - targetv[1]), 2);
00148 }
00149 
00150 } // end of namespace PLearn
00151 
00152 
00153 /*
00154   Local Variables:
00155   mode:c++
00156   c-basic-offset:4
00157   c-file-style:"stroustrup"
00158   c-file-offsets:((innamespace . 0)(inline-open . 0))
00159   indent-tabs-mode:nil
00160   fill-column:79
00161   End:
00162 */
00163 // 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