PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // BaseRegressorConfidence.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: BaseRegressorConfidence.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 "BaseRegressorConfidence.h" 00043 00044 namespace PLearn { 00045 using namespace std; 00046 00047 PLEARN_IMPLEMENT_OBJECT(BaseRegressorConfidence, 00048 "A PLearner to provide a confidence function to a generic base regressor.", 00049 "The algorithm computes an outut between 0 and 1 based on a local density function.\n" 00050 ); 00051 00052 BaseRegressorConfidence::BaseRegressorConfidence() 00053 : number_of_neighbors(1), 00054 sigma(0.1), 00055 raise_confidence(1.0), 00056 lower_confidence(0.0) 00057 { 00058 } 00059 00060 BaseRegressorConfidence::~BaseRegressorConfidence() 00061 { 00062 } 00063 00064 void BaseRegressorConfidence::declareOptions(OptionList& ol) 00065 { 00066 declareOption(ol, "number_of_neighbors", &BaseRegressorConfidence::number_of_neighbors, OptionBase::buildoption, 00067 "The number of nearest neighbors to consider.\n"); 00068 declareOption(ol, "sigma", &BaseRegressorConfidence::sigma, OptionBase::buildoption, 00069 "The variance of the distribution on the target.\n"); 00070 declareOption(ol, "raise_confidence", &BaseRegressorConfidence::raise_confidence, OptionBase::buildoption, 00071 "If the computed confidence is greater or equal to this level, it will be raised to 1.0.\n"); 00072 declareOption(ol, "lower_confidence", &BaseRegressorConfidence::lower_confidence, OptionBase::buildoption, 00073 "If the computed confidence is lower than this level, it will be lowered to 0.0.\n"); 00074 00075 declareOption(ol, "neighbors", &BaseRegressorConfidence::neighbors, OptionBase::learntoption, 00076 "The matrice of indices of nearest neighbors rows from the training set.\n"); 00077 declareOption(ol, "nearest_neighbbors_target_mean", &BaseRegressorConfidence::nearest_neighbbors_target_mean, OptionBase::learntoption, 00078 "The vector of neairest neighborstarget means from the trainingset.\n");; 00079 inherited::declareOptions(ol); 00080 } 00081 00082 void BaseRegressorConfidence::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00083 { 00084 inherited::makeDeepCopyFromShallowCopy(copies); 00085 deepCopyField(number_of_neighbors, copies); 00086 deepCopyField(sigma, copies); 00087 deepCopyField(raise_confidence, copies); 00088 deepCopyField(lower_confidence, copies); 00089 deepCopyField(neighbors, copies); 00090 deepCopyField(nearest_neighbbors_target_mean, copies); 00091 } 00092 00093 void BaseRegressorConfidence::build() 00094 { 00095 inherited::build(); 00096 build_(); 00097 } 00098 00099 void BaseRegressorConfidence::build_() 00100 { 00101 if (train_set) 00102 { 00103 input_to_search.resize(train_set->inputsize()); 00104 target_to_search.resize(1); 00105 input_to_compare.resize(train_set->inputsize()); 00106 target_to_compare.resize(1); 00107 neighbors.resize(train_set->length(), number_of_neighbors); 00108 nearest_neighbbors_target_mean.resize(train_set->length()); 00109 two_sigma_square = 2.0 * pow(sigma, 2); 00110 root_two_pi_sigma_square = sigma * 2.506628274631; 00111 } 00112 } 00113 00114 void BaseRegressorConfidence::train() 00115 { 00116 for(int row_to_search = 0; row_to_search < train_set.length(); row_to_search++) 00117 { 00118 BottomNI<real> neighbors_search(number_of_neighbors); 00119 nearest_neighbbors_target_mean[row_to_search] = 0.0; 00120 train_set->getExample(row_to_search, input_to_search, target_to_search, weight_to_search); 00121 for(int row_to_compare = 0; row_to_compare < train_set.length(); row_to_compare++) 00122 { 00123 train_set.getExample(row_to_compare, input_to_compare, target_to_compare, weight_to_compare); 00124 neighbors_search.update(powdistance(input_to_search, input_to_compare), row_to_compare); 00125 } 00126 neighbors_search.sort(); 00127 for(int row_to_compare = 0; row_to_compare < number_of_neighbors; row_to_compare++) 00128 { 00129 TVec< pair<real,int> > indices = neighbors_search.getBottomN(); 00130 neighbors(row_to_search, row_to_compare) = indices[row_to_compare].second; 00131 train_set->getExample(neighbors(row_to_search, row_to_compare), input_to_compare, target_to_compare, weight_to_compare); 00132 nearest_neighbbors_target_mean[row_to_search] += target_to_compare[0]; 00133 } 00134 nearest_neighbbors_target_mean[row_to_search] = nearest_neighbbors_target_mean[row_to_search] / number_of_neighbors; 00135 } 00136 } 00137 00138 void BaseRegressorConfidence::verbose(string the_msg, int the_level) 00139 { 00140 if (verbosity >= the_level) 00141 cout << the_msg << endl; 00142 } 00143 00144 void BaseRegressorConfidence::forget() 00145 { 00146 } 00147 00148 int BaseRegressorConfidence::outputsize() const 00149 { 00150 return 2; 00151 } 00152 00153 TVec<string> BaseRegressorConfidence::getTrainCostNames() const 00154 { 00155 TVec<string> return_msg(1); 00156 return_msg[0] = "mse"; 00157 return return_msg; 00158 } 00159 00160 TVec<string> BaseRegressorConfidence::getTestCostNames() const 00161 { 00162 return getTrainCostNames(); 00163 } 00164 00165 void BaseRegressorConfidence::computeOutput(const Vec& inputv, Vec& outputv) const 00166 { 00167 Vec train_set_inputv; 00168 Vec train_set_targetv; 00169 real train_set_weight; 00170 train_set_inputv.resize(train_set->inputsize()); 00171 train_set_targetv.resize(1); 00172 real distance = REAL_MAX; 00173 int nearest_neighbor = -1; 00174 for (int row = 0; row < train_set->length(); row++) 00175 { 00176 train_set->getExample(row, train_set_inputv, train_set_targetv, train_set_weight); 00177 if (powdistance(inputv, train_set_inputv) < distance) 00178 { 00179 distance = powdistance(inputv, train_set_inputv); 00180 nearest_neighbor = row; 00181 } 00182 } 00183 outputv[1] = exp(-1.0 * pow((outputv[0] - nearest_neighbbors_target_mean[nearest_neighbor]), 2) / two_sigma_square); // / root_two_pi_sigma_square? 00184 outputv[0] = nearest_neighbbors_target_mean[nearest_neighbor]; 00185 if (outputv[1] >= raise_confidence) outputv[1] = 1.0; 00186 if (outputv[1] < lower_confidence) outputv[1] = 0.0; 00187 } 00188 00189 void BaseRegressorConfidence::computeOutputAndCosts(const Vec& inputv, const Vec& targetv, Vec& outputv, Vec& costsv) const 00190 { 00191 computeOutput(inputv, outputv); 00192 computeCostsFromOutputs(inputv, outputv, targetv, costsv); 00193 } 00194 00195 void BaseRegressorConfidence::computeCostsFromOutputs(const Vec& inputv, const Vec& outputv, const Vec& targetv, Vec& costsv) const 00196 { 00197 costsv[0] = pow((outputv[0] - targetv[0]), 2); 00198 } 00199 00200 } // end of namespace PLearn 00201 00202 00203 /* 00204 Local Variables: 00205 mode:c++ 00206 c-basic-offset:4 00207 c-file-style:"stroustrup" 00208 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00209 indent-tabs-mode:nil 00210 fill-column:79 00211 End: 00212 */ 00213 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :