Changeset 307
- Timestamp:
- May 3, 2005, 3:28:29 PM (18 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
- 4 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/svm/Kernel_MEV.cc
r306 r307 1 1 // $Id$ 2 2 3 #include <c++_tools/svm/Kernel .h>3 #include <c++_tools/svm/Kernel_MEV.h> 4 4 5 5 #include <c++_tools/svm/KernelFunction.h> … … 10 10 namespace svm { 11 11 12 Kernel ::Kernel(const gslapi::matrix& data, const KernelFunction& kf)13 : k_(data.columns(),data.columns()), kf_(&kf), weighted_(false)12 Kernel_MEV::Kernel_MEV(const gslapi::matrix& data, const KernelFunction& kf) 13 : data_(data), kf_(&kf) 14 14 { 15 k_ = kf(k_,data);16 kf_=0;17 }18 19 Kernel::Kernel(const gslapi::matrix& data, const KernelFunction& kf,20 const gslapi::matrix& weight)21 : k_(data.columns(),data.columns()), kf_(&kf), weighted_(true)22 {23 for(u_int i=0;i<data.columns();i++)24 for(u_int j=i;j<data.columns();j++)25 k_(i,j)=k_(j,i)=kf(data[i],data[j],weight[i],weight[j]);26 27 15 } 28 16 29 17 30 Kernel ::~Kernel(void)18 Kernel_MEV::~Kernel_MEV(void) 31 19 { 32 20 } 33 21 22 double Kernel_MEV::operator()(size_t row, size_t column) const 23 { 24 return (*kf_)(data_.TEMP_col_return(row),data_.TEMP_col_return(column)); 25 } 26 27 34 28 }} // of namespace svm and namespace theplu -
trunk/lib/svm/Kernel_MEV.h
r306 r307 1 1 // $Id$ 2 2 3 #ifndef _theplu_svm_kernel_ 4 #define _theplu_svm_kernel_ 3 #ifndef _theplu_svm_kernel_mev 4 #define _theplu_svm_kernel_mev 5 5 6 6 #include <c++_tools/gslapi/matrix.h> … … 12 12 13 13 /// 14 /// Class calculating the \f$NxN\f$ kernel matrix from the \f$MxN\f$ 15 /// data matrix using KernelFunction. Each column in the data matrix 16 /// corresponds to one sample. 14 /// Class taking care of the \f$NxN\f$ kernel matrix, where 15 /// \f$N\f$ is number of samples. Type of Kernel is defined by a 16 /// KernelFunction. This Memory Efficient Versions (SEV) does not 17 /// store the kernel matrix in memory, but calculates each element 18 /// when it is needed. 17 19 /// 20 /// @see also Kernel_SEV 18 21 /// 19 class Kernel 22 class Kernel_MEV 20 23 { 21 24 22 25 public: 23 26 27 Kernel_MEV(); 28 24 29 /// 25 30 /// Constructor taking the data matrix and KernelFunction as 26 /// input. @note Can not handle NaNs. When dealing with missing values, 27 /// use constructor taking a weight matrix. 28 Kernel(const gslapi::matrix&, const KernelFunction&); 31 /// input.Each column in the data matrix corresponds to one 32 /// sample. @note Can not handle NaNs. 33 /// 34 Kernel_MEV(const gslapi::matrix&, const KernelFunction&); 29 35 30 36 /// 31 /// Constructor taking the data matrix, the KernelFunction and a weight 32 /// matrix as input. 33 Kernel(const gslapi::matrix&, const KernelFunction&, const gslapi::matrix&); 37 /// Constructor taking the \a data matrix, the KernelFunction and a 38 /// \a weight matrix as input. Each column in the data matrix 39 /// corresponds to one sample. 40 /// @todo 41 Kernel_MEV(const gslapi::matrix& data, const KernelFunction&, 42 const gslapi::matrix& weight); 34 43 35 44 /// 36 45 /// Destructor 37 virtual ~Kernel(void); 46 /// 47 virtual ~Kernel_MEV(void); 38 48 39 /// @return Kernel matrix 49 /// 50 /// @return element at position (\a row, \a column) of the Kernel 51 /// matrix 40 52 /// 41 const gslapi::matrix& get() const {return k_;} 53 virtual double operator()(size_t row,size_t column) const; 54 55 /// 56 /// @brief number of samples 57 /// 58 inline size_t size(void) const { return data_.columns(); } 42 59 43 pr ivate:44 gslapi::matrix k_;60 protected: 61 const gslapi::matrix& data_; 45 62 const KernelFunction* kf_; 46 bool weighted_;47 63 48 }; // class Kernel 64 }; // class Kernel_MEV 49 65 50 66 }} // of namespace svm and namespace theplu -
trunk/lib/svm/Kernel_SEV.cc
r306 r307 1 1 // $Id$ 2 2 3 #include <c++_tools/svm/Kernel .h>3 #include <c++_tools/svm/Kernel_SEV.h> 4 4 5 5 #include <c++_tools/svm/KernelFunction.h> 6 #include <c++_tools/svm/Kernel_MEV.h> 6 7 #include <c++_tools/gslapi/matrix.h> 7 8 #include <c++_tools/gslapi/vector.h> … … 10 11 namespace svm { 11 12 12 Kernel ::Kernel(const gslapi::matrix& data, const KernelFunction& kf)13 : k_(data.columns(),data.columns()), kf_(&kf), weighted_(false)13 Kernel_SEV::Kernel_SEV(const gslapi::matrix& data, const KernelFunction& kf) 14 : Kernel_MEV(data,kf) 14 15 { 15 k_ = kf(k_,data); 16 kf_=0; 16 kernel_ = gslapi::matrix(data.columns(),data.columns()); 17 for (size_t i=0; i<kernel_.rows(); i++) 18 for (size_t j=i; j<kernel_.columns(); j++) 19 kernel_(i,j) = kernel_(j,i) = 20 (*kf_)(data_.TEMP_col_return(i),data_.TEMP_col_return(j)); 21 17 22 } 18 23 19 Kernel::Kernel(const gslapi::matrix& data, const KernelFunction& kf, 20 const gslapi::matrix& weight) 21 : k_(data.columns(),data.columns()), kf_(&kf), weighted_(true) 22 { 23 for(u_int i=0;i<data.columns();i++) 24 for(u_int j=i;j<data.columns();j++) 25 k_(i,j)=k_(j,i)=kf(data[i],data[j],weight[i],weight[j]); 26 27 } 28 29 30 Kernel::~Kernel(void) 24 Kernel_SEV::~Kernel_SEV(void) 31 25 { 32 26 } -
trunk/lib/svm/Kernel_SEV.h
r306 r307 1 1 // $Id$ 2 2 3 #ifndef _theplu_svm_kernel_ 4 #define _theplu_svm_kernel_ 3 #ifndef _theplu_svm_kernel_sev_ 4 #define _theplu_svm_kernel_sev_ 5 5 6 6 #include <c++_tools/gslapi/matrix.h> 7 #include <c++_tools/svm/Kernel_MEV.h> 8 7 9 8 10 namespace theplu { … … 12 14 13 15 /// 14 /// Class calculating the \f$NxN\f$ kernel matrix from the \f$MxN\f$ 15 /// data matrix using KernelFunction. Each column in the data matrix 16 /// corresponds to one sample. 16 /// Class taking care of the \f$NxN\f$ kernel matrix, where 17 /// \f$N\f$ is number of samples. Type of Kernel is defined by a 18 /// KernelFunction. This Speed Efficient Versions (SEV) calculated 19 /// the kernel matrix once and the kernel is stored in memory. 17 20 /// 21 /// @see also Kernel_MEV 18 22 /// 19 class Kernel 23 class Kernel_SEV : public Kernel_MEV 20 24 { 21 25 … … 26 30 /// input. @note Can not handle NaNs. When dealing with missing values, 27 31 /// use constructor taking a weight matrix. 28 Kernel (const gslapi::matrix&, const KernelFunction&);32 Kernel_SEV(const gslapi::matrix&, const KernelFunction&); 29 33 30 /// 31 /// Constructor taking the data matrix, the KernelFunction and a weight 32 /// matrix as input. 33 Kernel(const gslapi::matrix&, const KernelFunction&, const gslapi::matrix&); 34 Kernel_SEV(const Kernel_SEV&); 34 35 35 36 /// 36 37 /// Destructor 37 virtual ~Kernel(void); 38 /// 39 virtual ~Kernel_SEV(void); 38 40 39 /// @return Kernel matrix 41 /// 42 /// @return element at position (\a row, \a column) of the Kernel 43 /// matrix 40 44 /// 41 const gslapi::matrix& get() const {return k_;} 45 inline double operator()(size_t row,size_t column) const 46 { return kernel_(row,column); } 42 47 43 48 private: 44 gslapi::matrix k_; 45 const KernelFunction* kf_; 46 bool weighted_; 49 gslapi::matrix kernel_; 47 50 48 }; // class Kernel 51 }; // class Kernel_SEV 49 52 50 53 }} // of namespace svm and namespace theplu -
trunk/lib/svm/Makefile.am
r306 r307 10 10 libsvm_la_SOURCES = \ 11 11 ConsensusInputRanker.cc CrossValidation.cc GaussianKernelFunction.cc \ 12 InputRanker.cc Kernel_ mev.cc Kernel_sev.cc \12 InputRanker.cc Kernel_MEV.cc Kernel_SEV.cc \ 13 13 PolynomialKernelFunction.cc SVM.cc -
trunk/lib/svm/SVM.cc
r301 r307 4 4 #include <c++_tools/svm/SVM.h> 5 5 6 #include <c++_tools/svm/Kernel_MEV.h> 6 7 #include <c++_tools/gslapi/matrix.h> 7 8 #include <c++_tools/gslapi/vector.h> … … 18 19 namespace svm { 19 20 20 SVM::SVM(const Kernel & kernel,21 SVM::SVM(const Kernel_MEV& kernel, 21 22 const gslapi::vector& target, 22 23 const std::vector<size_t>& train_set) … … 105 106 gslapi::vector target_train(train_set_.size()); 106 107 108 // copy submatrix to train on. Peter, might be unnecessary. 107 109 for (unsigned int i=0; i<train_set_.size(); i++) { 108 110 target_train(i) = target_(train_set_[i]); 109 111 for (unsigned int j=0; j<train_set_.size(); j++) 110 kernel_train(i,j) = kernel_ .get()(train_set_[i],train_set_[j]);112 kernel_train(i,j) = kernel_(train_set_[i],train_set_[j]); 111 113 } 112 114 // Modify the diagonal of the kernel matrix (soft margin) -
trunk/lib/svm/SVM.h
r295 r307 4 4 #define _theplu_svm_svm_ 5 5 6 #include <c++_tools/svm/Kernel .h>6 #include <c++_tools/svm/Kernel_MEV.h> 7 7 #include <c++_tools/gslapi/vector.h> 8 8 … … 29 29 /// Constructor taking the kernel matrix and the target vector as input 30 30 /// 31 SVM(const Kernel &,31 SVM(const Kernel_MEV&, 32 32 const gslapi::vector&, 33 33 const std::vector<size_t>& = std::vector<size_t>()); … … 55 55 /// 56 56 /// @return output 57 /// 58 inline theplu::gslapi::vector output(void) 59 {return kernel_.get() * alpha_.mul(target_)+ 60 theplu::gslapi::vector(alpha_.size(),bias_);} 57 /// @todo 58 theplu::gslapi::vector output(void) const; 61 59 62 60 /// … … 85 83 double bias_; 86 84 double c_; 87 Kernel kernel_; // Peter, const ref?85 const Kernel_MEV kernel_; 88 86 unsigned long int max_epochs_; 89 gslapi::vector target_; // Peter, const ref?87 const gslapi::vector& target_; 90 88 bool trained_; 91 89 std::vector<size_t> train_set_; -
trunk/test/kernel_test.cc
r301 r307 7 7 #include <c++_tools/gslapi/vector.h> 8 8 #include <c++_tools/svm/PolynomialKernelFunction.h> 9 #include <c++_tools/svm/Kernel.h> 9 #include <c++_tools/svm/Kernel_MEV.h> 10 #include <c++_tools/svm/Kernel_SEV.h> 10 11 11 12 #include <cmath> … … 17 18 using namespace theplu; 18 19 20 bool test_MEV(const gslapi::matrix& data, const svm::KernelFunction* kf, 21 const gslapi::matrix& control, const double error_bound) 22 { 23 svm::Kernel_MEV kernel(data,*kf); 24 for(u_int i=0;i<control.rows();i++) 25 for(u_int j=0;j<control.columns();j++) 26 if (fabs(kernel(i,j)-control(i,j))>error_bound) 27 return false; 28 29 return true; 30 } 31 32 bool test_SEV(const gslapi::matrix& data, const svm::KernelFunction* kf, 33 const gslapi::matrix& control, const double error_bound) 34 { 35 svm::Kernel_SEV kernel(data,*kf); 36 for(u_int i=0;i<control.rows();i++) 37 for(u_int j=0;j<control.columns();j++) 38 if (fabs(kernel(i,j)-control(i,j))>error_bound) 39 return false; 40 41 return true; 42 } 43 44 19 45 int main() 20 46 21 47 { 22 23 bool ok = true; 48 bool ok = true; 49 // Peter, the hardcoded number below should be changed. 50 double error_bound = 1e-8; 24 51 std::ifstream is("data/nm_data_centralized.txt"); 25 52 gslapi::matrix transposed_data(is); 26 53 is.close(); 27 // Because how the kernel is treated is changed, I have to transpose the data54 // Because how the kernel is treated is changed, data must be transposed. 28 55 gslapi::matrix data=transposed_data; 29 56 … … 31 58 gslapi::matrix kernel_matlab(is); 32 59 is.close(); 33 svm::KernelFunction* kf = 34 new svm::PolynomialKernelFunction(); 35 svm::Kernel kernel(data,*kf); 36 gslapi::matrix diff(kernel.get()-kernel_matlab); 37 double tmp=0; 38 for(u_int i=0;i<diff.rows();i++) 39 for(u_int j=0;j<diff.rows();j++) 40 tmp+=fabs(diff(i,j)/kernel_matlab(i,j)); 41 delete kf; 42 if (tmp > diff.rows()*diff.rows()/100000) 43 ok = false; 60 svm::KernelFunction* kf = new svm::PolynomialKernelFunction(); 61 ok = (ok && test_MEV(data,kf,kernel_matlab,error_bound) 62 & test_SEV(data,kf,kernel_matlab,error_bound)); 63 delete kf; 44 64 45 65 is.open("data/nm_kernel2.txt"); … … 47 67 is.close(); 48 68 kf = new svm::PolynomialKernelFunction(2); 49 svm::Kernel kernel2(data,*kf); 50 diff = kernel2.get()-kernel_matlab2; 51 tmp=0; 52 for(u_int i=0;i<diff.rows();i++) 53 for(u_int j=0;j<diff.rows();j++) 54 tmp+=fabs(diff(i,j)/kernel_matlab2(i,j)); 55 if (tmp > diff.rows()*diff.rows()/100) 56 ok = false; 57 69 ok = (ok && test_MEV(data,kf,kernel_matlab2,error_bound) 70 & test_SEV(data,kf,kernel_matlab2,error_bound)); 71 58 72 if (ok=true) 59 73 return 0; -
trunk/test/svm_test.cc
r301 r307 4 4 #include <c++_tools/gslapi/vector.h> 5 5 #include <c++_tools/svm/SVM.h> 6 #include <c++_tools/svm/Kernel .h>6 #include <c++_tools/svm/Kernel_SEV.h> 7 7 #include <c++_tools/svm/PolynomialKernelFunction.h> 8 8 … … 64 64 svm::KernelFunction* kf = 65 65 new svm::PolynomialKernelFunction(); 66 svm::Kernel kernel(data,*kf);66 svm::Kernel_SEV kernel(data,*kf); 67 67 //theplu::gslapi::matrix m=kernel.get(); 68 68 //std::cout << "kernel:\n" << m << "\n";
Note: See TracChangeset
for help on using the changeset viewer.