Changeset 163
- Timestamp:
- Sep 22, 2004, 7:14:06 PM (19 years ago)
- Location:
- trunk/src
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/GaussianKernelFunction.cc
r67 r163 7 7 // Thep C++ Tools 8 8 #include "GaussianKernelFunction.h" 9 #include "matrix.h" 9 10 #include "vector.h" 10 11 … … 33 34 } 34 35 36 37 const theplu::gslapi::matrix& 38 GaussianKernelFunction::operator()(theplu::gslapi::matrix& k, 39 const theplu::gslapi::matrix& data) const 40 { 41 theplu::gslapi::matrix data_transposed(data); 42 data_transposed.transpose(); 43 k = data_transposed*data; 44 // Working with the linear linear kernel in order to avoid copying 45 // row vectors from data matrix (many times) and calculate the 46 // kernel element in a more transparent way. 47 for (size_t i=0; i<k.rows()-1; i++) 48 for (size_t j=i+1; j<k.columns(); j++) 49 k(i,j)=k(j,i)=exp(-(k(i,i)+k(j,j)-2*k(i,j))/(2*sigma_)); 50 for (size_t i=0; i<k.rows(); i++) 51 k(i,i)=1; 52 return k; 53 } 54 35 55 }} // of namespace cpptools and namespace theplu -
trunk/src/GaussianKernelFunction.h
r67 r163 15 15 namespace cpptools { 16 16 17 /** 18 Class calculating one element in the kernel matrix using the 19 Gaussian kernel. 20 */ 17 /// 18 /// Class for Gaussian kernel calculations. 19 /// 21 20 22 21 … … 25 24 26 25 public: 27 ///Constructor taking the sigma_ , i.e. the width of the Gaussian,as 26 /// 27 ///Constructor taking the sigma_ , i.e. the width of the Gaussian,as 28 28 ///input. Default is sigma_ = 1. 29 29 /// 30 30 GaussianKernelFunction(double = 1); 31 31 32 ///Destructor 32 /// 33 ///Destructor 33 34 /// 34 35 virtual ~GaussianKernelFunction(void) {}; 35 36 36 ///returning the scalar product of two vectors in feature space using the 37 ///Gaussian kernel. @return \f$ exp((x - y)^{2}/\sigma^2) \f$ \n 37 /// 38 /// returning the scalar product of two vectors in feature space using the 39 /// Gaussian kernel. @return \f$ exp((x - y)^{2}/\sigma^2) \f$ \n 38 40 /// 39 41 inline double operator()(const gslapi::vector& a1, … … 49 51 const gslapi::vector& w2) const; 50 52 51 private: 53 /// 54 /// @kernel matrix 55 /// 56 const gslapi::matrix& operator()(theplu::gslapi::matrix& kernel, 57 const theplu::gslapi::matrix& data) const; 58 59 private: 52 60 double sigma_; 53 61 -
trunk/src/Kernel.cc
r67 r163 11 11 12 12 Kernel::Kernel(const gslapi::matrix& data, const KernelFunction& kf) 13 : k_(data. rows(),data.rows())13 : k_(data.columns(),data.columns()) 14 14 { 15 for(u_int i=0;i<data.rows();i++) 16 for(u_int j=0;j<i+1;j++) 17 k_(i,j)=kf(data[i],data[j]); 18 19 // Copy lower triangle to upper triangle of Kernel matrix 20 for(u_int i=0;i<data.rows()-1;i++) 21 for(u_int j=i+1;j<data.rows();j++) 22 k_(i,j)=k_(j,i); 15 k_ = kf(k_,data); 23 16 } 24 17 25 18 Kernel::Kernel(const gslapi::matrix& data, const KernelFunction& kf, 26 19 const gslapi::matrix& weight) 27 : k_(data. rows(),data.rows())20 : k_(data.columns(),data.columns()) 28 21 { 29 for(u_int i=0;i<data. rows();i++)30 for(u_int j= 0;j<i+1;j++)31 k_(i,j)=k f(data[i],data[j],weight[i],weight[j]);22 for(u_int i=0;i<data.columns();i++) 23 for(u_int j=i;j<data.columns();j++) 24 k_(i,j)=k_(j,i)=kf(data[i],data[j],weight[i],weight[j]); 32 25 33 // Copy lower triangle to upper triangle of Kernel matrix34 for(u_int i=0;i<data.rows()-1;i++)35 for(u_int j=i+1;j<data.rows();j++)36 k_(i,j)=k_(j,i);37 26 } 38 27 -
trunk/src/KernelFunction.h
r99 r163 8 8 namespace gslapi { 9 9 class vector; 10 class matrix; 10 11 } 11 12 … … 13 14 14 15 /// 15 /// Virtual Class calculating one element in the kernel matrix 16 /// (i.e. the scalar product in feature space) from the two 17 /// corresponding vector in the data matrix. 16 /// Virtual Class calculating kernel matrix. 18 17 /// 19 18 class KernelFunction … … 39 38 const gslapi::vector&) const = 0; 40 39 41 }; // class KernelFunction 40 virtual const theplu::gslapi::matrix& operator() 41 (theplu::gslapi::matrix& k, const gslapi::matrix& data) const = 0; 42 43 }; // class KernelFunction 42 44 43 45 }} // of namespace cpptools and namespace theplu -
trunk/src/PolynomialKernelFunction.cc
r67 r163 6 6 // Thep C++ Tools 7 7 #include "PolynomialKernelFunction.h" 8 #include "matrix.h" 8 9 #include "vector.h" 9 10 … … 21 22 { 22 23 if(order_>1) 23 return pow( a1*a2,order_);24 return pow(1+a1*a2,order_); 24 25 return a1*a2; 25 26 } … … 43 44 } 44 45 46 const theplu::gslapi::matrix& 47 PolynomialKernelFunction::operator()(theplu::gslapi::matrix& kernel, 48 const theplu::gslapi::matrix& data) const 49 { 50 theplu::gslapi::matrix data_transposed(data.transpose()); 51 kernel = data_transposed*data; 52 if (order_>1) 53 for (size_t i=0; i<kernel.rows(); i++) 54 for (size_t j=i; j<kernel.rows(); j++) 55 kernel(i,j)=kernel(j,i)=pow(1+kernel(i,j),order_); 56 return kernel; 57 } 45 58 }} // of namespace cpptools and namespace theplu -
trunk/src/PolynomialKernelFunction.h
r100 r163 14 14 namespace cpptools { 15 15 16 /// Class calculating one element in the kernel matrix using the polynomial17 /// kernel.16 /// 17 /// Class for polynomial kernel calculations 18 18 /// 19 19 … … 23 23 24 24 public: 25 ///Constructor taking the order of the polynomial as input. Default is 25 /// 26 ///Constructor taking the order of the polynomial as input. Default is 26 27 ///order=1 yielding the linear kernel function. 27 28 /// … … 33 34 virtual ~PolynomialKernelFunction(void) {}; 34 35 36 /// 35 37 ///returning the scalar product of two vectors in feature space using the 36 38 ///polynomial kernel. @return If order is larger than one: \f$ (1+x \cdot … … 39 41 double operator()(const gslapi::vector&, const gslapi::vector&) const; 40 42 43 /// 41 44 ///returning the scalar product of two vectors in feature space using the 42 45 ///polynomial kernel with weights. … … 45 48 const gslapi::vector&, const gslapi::vector&) const; 46 49 50 /// 51 /// @kernel matrix 52 /// 53 const gslapi::matrix& operator()(theplu::gslapi::matrix& kernel, 54 const theplu::gslapi::matrix& data) const; 55 47 56 private: 48 57 int order_;
Note: See TracChangeset
for help on using the changeset viewer.