Changeset 67
- Timestamp:
- Apr 27, 2004, 5:04:31 PM (19 years ago)
- Location:
- trunk/src
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/GaussianKernelFunction.cc
r42 r67 17 17 } 18 18 19 double GaussianKernelFunction::operator()(const gslapi::vector& a, 20 const gslapi::vector& b) const 19 double GaussianKernelFunction::operator()(const gslapi::vector& a1, 20 const gslapi::vector& a2, 21 const gslapi::vector& w1, 22 const gslapi::vector& w2) const 21 23 { 22 u_int nof_valid_datapairs = 0; 23 double tmp = 0; 24 // check that a size == b size ! 25 if(a.size() != b.size()) 26 std::cerr << "vectors must have same length\n"; 27 for(u_int k=0; k<a.size(); k++) 28 // Check that both a and b are Numbers (NaN is NOT equal to NaN) 29 if (a(k) == a(k) & b(k) == b(k)) { 30 tmp += (a(k)-b(k))*(a(k)-b(k)); 31 nof_valid_datapairs++; 32 } 33 //cout << nof_valid_datapairs << endl; 34 if (nof_valid_datapairs == 0) 35 std::cerr << "data file error: two columns " 36 << " have no pair of valid data" << std::endl; 37 38 tmp/=nof_valid_datapairs / a.size(); //correcting for missing values 39 40 41 return exp(tmp/pow(sigma_,2)); 24 double distance = 0; 25 double normalization_factor = 0; 26 for (size_t i=0; i<a1.size(); i++) { 27 distance += w1(i) * w2(i) * (a1(i)-a2(i)) * (a1(i)-a2(i)); 28 normalization_factor += w1(i) * w2(i); 29 } 30 // to make it coherent with no weight case 31 normalization_factor /= a1.size(); 32 return exp(distance/normalization_factor/pow(sigma_,2)); 42 33 } 43 34 -
trunk/src/GaussianKernelFunction.h
r42 r67 5 5 6 6 #include "KernelFunction.h" 7 #include "vector.h" 7 8 8 9 namespace theplu { … … 24 25 25 26 public: 26 /** 27 Constructor taking the sigma_ , i.e. the width of the Gaussian, 28 as input. Default is sigma_ = 1. 29 */ 27 ///Constructor taking the sigma_ , i.e. the width of the Gaussian,as 28 ///input. Default is sigma_ = 1. 29 /// 30 30 GaussianKernelFunction(double = 1); 31 /**32 33 */31 32 ///Destructor 33 /// 34 34 virtual ~GaussianKernelFunction(void) {}; 35 /**36 returning the scalar product of two vectors in feature space using37 the Gaussian kernel.38 @return39 \f$ exp((x - y)^{2}/\sigma^2) \f$ \n40 41 42 */43 double operator()(const gslapi::vector&, const gslapi::vector&) const;44 35 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 38 /// 39 inline double operator()(const gslapi::vector& a1, 40 const gslapi::vector& a2) 41 {return exp(-(a1-a2)*(a1-a2)/pow(sigma_,2));} 42 43 ///returning the scalar product of two vectors in feature space using the 44 ///Gaussian kernel with weights. 45 /// 46 double operator()(const gslapi::vector& a1, 47 const gslapi::vector& a2, 48 const gslapi::vector& w1, 49 const gslapi::vector& w2) const; 50 45 51 private: 46 52 double sigma_; -
trunk/src/Kernel.cc
r51 r67 13 13 : k_(data.rows(),data.rows()) 14 14 { 15 16 15 for(u_int i=0;i<data.rows();i++) 16 for(u_int j=0;j<i+1;j++) 17 17 k_(i,j)=kf(data[i],data[j]); 18 18 … … 23 23 } 24 24 25 Kernel::Kernel(const gslapi::matrix& data, const KernelFunction& kf, 26 const gslapi::matrix& weight) 27 : k_(data.rows(),data.rows()) 28 { 29 for(u_int i=0;i<data.rows();i++) 30 for(u_int j=0;j<i+1;j++) 31 k_(i,j)=kf(data[i],data[j],weight[i],weight[j]); 32 33 // Copy lower triangle to upper triangle of Kernel matrix 34 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 } 38 39 25 40 Kernel::~Kernel(void) 26 41 { -
trunk/src/Kernel.h
r51 r67 22 22 public: 23 23 /// 24 /// Constructor taking the data matrix and KernelFunction as input. 24 /// Constructor taking the data matrix and KernelFunction as 25 /// input. Note: can not handle NaNs. When dealing with missing values, 26 /// use constructor taking a weight matrix. 27 Kernel(const gslapi::matrix&, const KernelFunction&); 28 25 29 /// 26 Kernel(const gslapi::matrix&, const KernelFunction&); 30 /// Constructor taking the data matrix, the KernelFunction and a weight 31 /// matrix as input. 32 Kernel(const gslapi::matrix&, const KernelFunction&, const gslapi::matrix&); 33 27 34 /// 28 35 /// Destructor 29 ///30 36 virtual ~Kernel(void); 31 /// 37 32 38 /// @return Kernel matrix 33 39 /// -
trunk/src/KernelFunction.h
r42 r67 33 33 const gslapi::vector&) const = 0; 34 34 35 virtual double operator()(const gslapi::vector&, 36 const gslapi::vector&, 37 const gslapi::vector&, 38 const gslapi::vector&) const = 0; 39 35 40 }; // class KernelFunction 36 41 -
trunk/src/PolynomialKernelFunction.cc
r42 r67 17 17 } 18 18 19 double PolynomialKernelFunction::operator()(const gslapi::vector& a ,20 const gslapi::vector& b) const19 double PolynomialKernelFunction::operator()(const gslapi::vector& a1, 20 const gslapi::vector& a2) const 21 21 { 22 using namespace std; 22 if(order_>1) 23 return pow(a1*a2,order_); 24 return a1*a2; 25 } 23 26 24 u_int nof_valid_datapairs = 0; 25 double tmp = 0; 26 // check that a size == b size ! 27 if(a.size() != b.size()) 28 cerr << "vectors must have same length\n"; 29 for(u_int k=0; k<a.size(); k++) 30 // Check that both a and b are Numbers (NaN is NOT equal to NaN) 31 if (a(k) == a(k) & b(k) == b(k)) { 32 tmp += a(k)*b(k); 33 nof_valid_datapairs++; 34 } 35 //cout << nof_valid_datapairs << endl; 36 if (nof_valid_datapairs == 0) 37 cerr << "data file error: two columns " 38 << " have no pair of valid data\n"; 39 40 tmp/=nof_valid_datapairs / a.size(); //correcting for missing values 41 42 if(order_>1) 43 return pow(1+tmp,order_); 44 return tmp; 27 double PolynomialKernelFunction::operator()(const gslapi::vector& a1, 28 const gslapi::vector& a2, 29 const gslapi::vector& w1, 30 const gslapi::vector& w2) const 31 { 32 double scalar = 0; 33 double normalization_factor = 0; 34 for (size_t i=0; i<a1.size(); i++) { 35 scalar += w1(i) * w2(i) * a1(i) * a2(i); 36 normalization_factor += w1(i) * w2(i); 37 } 38 // to make it coherent with no weight case 39 normalization_factor /= a1.size(); 40 if(order_>1) 41 return pow(scalar/normalization_factor,order_); 42 return scalar/normalization_factor; 45 43 } 46 44 -
trunk/src/PolynomialKernelFunction.h
r42 r67 14 14 namespace cpptools { 15 15 16 /** 17 Class calculating one element in the kernel matrix using the 18 polynomial kernel. 19 */ 16 ///Class calculating one element in the kernel matrix using the polynomial 17 ///kernel. 18 /// 20 19 21 20 … … 24 23 25 24 public: 26 /** 27 Constructor taking the order of the polynomial as input. Default 28 is order=1 yielding the linear kernel function. 29 */ 25 ///Constructor taking the order of the polynomial as input. Default is 26 ///order=1 yielding the linear kernel function. 27 /// 30 28 PolynomialKernelFunction(int = 1); 31 /**32 33 */29 30 ///Destructor 31 /// 34 32 virtual ~PolynomialKernelFunction(void) {}; 35 /** 36 returning the scalar product of two vectors in feature space using 37 the polynomial kernel. 38 @return 39 If order is larger than one: \f$ (1+x \cdot y)^{order} \f$ \n 40 If order is one (linear): \f$ x \cdot y \f$ 41 42 */ 33 34 ///returning the scalar product of two vectors in feature space using the 35 ///polynomial kernel. @return If order is larger than one: \f$ (1+x \cdot 36 ///y)^{order} \f$ \n If order is one (linear): \f$ x \cdot y \f$ 37 /// 43 38 double operator()(const gslapi::vector&, const gslapi::vector&) const; 39 40 ///returning the scalar product of two vectors in feature space using the 41 ///polynomial kernel with weights. 42 /// 43 double operator()(const gslapi::vector&, const gslapi::vector&, 44 const gslapi::vector&, const gslapi::vector&) const; 44 45 45 46 private:
Note: See TracChangeset
for help on using the changeset viewer.