Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

Kernel.h

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio, Rejean Ducharme and University of Montreal 00006 // Copyright (C) 2001-2002 Nicolas Chapados, Ichiro Takeuchi, Jean-Sebastien Senecal 00007 // Copyright (C) 2002 Xiangdong Wang, Christian Dorion 00008 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions are met: 00011 // 00012 // 1. Redistributions of source code must retain the above copyright 00013 // notice, this list of conditions and the following disclaimer. 00014 // 00015 // 2. Redistributions in binary form must reproduce the above copyright 00016 // notice, this list of conditions and the following disclaimer in the 00017 // documentation and/or other materials provided with the distribution. 00018 // 00019 // 3. The name of the authors may not be used to endorse or promote 00020 // products derived from this software without specific prior written 00021 // permission. 00022 // 00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 // 00034 // This file is part of the PLearn library. For more information on the PLearn 00035 // library, go to the PLearn Web site at www.plearn.org 00036 00037 00038 /* ******************************************************* 00039 * $Id: Kernel.h,v 1.33 2004/07/23 13:42:52 tihocan Exp $ 00040 * This file is part of the PLearn library. 00041 ******************************************************* */ 00042 00043 #ifndef Kernel_INC 00044 #define Kernel_INC 00045 00046 #include <plearn/base/Object.h> 00047 #include <plearn/vmat/VMat.h> 00048 //#include "PLMPI.h" 00049 00050 namespace PLearn { 00051 using namespace std; 00052 00053 class Kernel: public Object 00054 { 00055 00056 private: 00057 00058 typedef Object inherited; 00059 00061 mutable Vec evaluate_xi, evaluate_xj, k_xi_x; 00062 00064 mutable bool lock_xi, lock_xj, lock_k_xi_x; 00065 00066 protected: 00067 00068 VMat data; 00069 int data_inputsize; 00070 int n_examples; 00071 00072 static void declareOptions(OptionList& ol); 00073 00074 public: 00075 00077 bool is_symmetric; 00078 int report_progress; 00079 VMat specify_dataset; 00080 00082 Kernel(bool is__symmetric = true); 00083 00084 PLEARN_DECLARE_ABSTRACT_OBJECT(Kernel); 00085 00087 virtual real evaluate(const Vec& x1, const Vec& x2) const = 0; 00088 00090 00096 virtual void setDataForKernelMatrix(VMat the_data); 00097 00105 virtual void addDataForKernelMatrix(const Vec& newRow); 00106 00107 00109 virtual int dataInputsize() { 00110 return data_inputsize; 00111 } 00112 00114 virtual int nExamples() 00115 { return n_examples; } 00116 00118 virtual real evaluate_i_j(int i, int j) const; 00119 00125 virtual real evaluate_i_x(int i, const Vec& x, real squared_norm_of_x=-1) const; 00126 00129 virtual real evaluate_x_i(const Vec& x, int i, real squared_norm_of_x=-1) const; 00130 00134 virtual real evaluate_i_x_again(int i, const Vec& x, real squared_norm_of_x=-1, bool first_time = false) const; 00135 virtual real evaluate_x_i_again(const Vec& x, int i, real squared_norm_of_x=-1, bool first_time = false) const; 00136 00138 virtual void computeGramMatrix(Mat K) const; 00139 00142 virtual void setParameters(Vec paramvec); 00143 virtual Vec getParameters() const; 00144 00146 00147 void apply(VMat m1, VMat m2, Mat& result) const; 00148 Mat apply(VMat m1, VMat m2) const; 00149 void apply(VMat m, const Vec& x, Vec& result) const; 00150 void apply(Vec x, VMat m, Vec& result) const; 00151 00153 void evaluate_all_i_x(const Vec& x, Vec& k_xi_x, real squared_norm_of_x=-1, int istart = 0) const; 00154 00156 void evaluate_all_x_i(const Vec& x, Vec& k_x_xi, real squared_norm_of_x=-1, int istart = 0) const; 00157 00158 inline real operator()(const Vec& x1, const Vec& x2) const 00159 { return evaluate(x1,x2); } 00160 00162 bool hasData(); 00163 00165 inline VMat getData() {return this->data;} 00166 00170 bool isInData(const Vec& x, int* i = 0) const; 00171 00176 void computeNearestNeighbors(const Vec& x, Mat& k_xi_x_sorted, int knn) const; 00177 00181 static TMat<int> computeKNNeighbourMatrixFromDistanceMatrix(const Mat& D, int knn, bool insure_self_first_neighbour=true, bool report_progress = false); 00182 00186 static Mat computeNeighbourMatrixFromDistanceMatrix(const Mat& D, bool insure_self_first_neighbour=true, bool report_progress = false); 00187 00188 Mat estimateHistograms(VMat d, real sameness_threshold, real minval, real maxval, int nbins) const; 00189 Mat estimateHistograms(Mat input_and_class, real minval, real maxval, int nbins) const; 00190 real test(VMat d, real threshold, real sameness_below_threshold, real sameness_above_threshold) const; 00191 virtual void build(); 00192 //virtual void oldwrite(ostream& out) const; 00193 //virtual void oldread(istream& in); 00194 virtual ~Kernel(); 00195 00196 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00197 00198 private: 00199 00200 void build_(); 00201 00202 }; 00203 DECLARE_OBJECT_PTR(Kernel); 00204 00209 class Ker: public PP<Kernel> 00210 { 00211 public: 00212 Ker() {} 00213 Ker(Kernel* v) :PP<Kernel>(v) {} 00214 Ker(const Ker& other) :PP<Kernel>(other) {} 00215 00216 real operator()(const Vec& x1, const Vec& x2) const 00217 { return ptr->evaluate(x1,x2); } 00218 }; 00219 00220 DECLARE_OBJECT_PP(Ker, Kernel); 00221 00222 template <> 00223 inline 00224 void deepCopyField(Ker& field, CopiesMap& copies) 00225 { 00226 if (field) 00227 field = static_cast<Kernel*>(field->deepCopy(copies)); 00228 } 00229 00230 // last column of data is supposed to be a classnum 00231 // returns a matrix of (index1, index2, distance) 00232 Mat findClosestPairsOfDifferentClass(int k, VMat data, Ker dist); 00233 00236 00237 inline Array<Ker> operator&(const Ker& k1, const Ker& k2) 00238 { return Array<Ker>(k1,k2); } 00239 00240 /*!************ 00241 * CostFunc * 00242 ************ 00243 */ 00244 00246 typedef Ker CostFunc; 00247 00248 /*!********************************************************************** 00249 FINANCIAL STUFF 00250 ********************************************************************** 00251 */ 00252 00254 typedef CostFunc ProfitFunc; 00255 00256 00257 } // end of namespace PLearn 00258 00259 #endif 00260

Generated on Tue Aug 17 15:56:21 2004 for PLearn by doxygen 1.3.7