Changeset 545
- Timestamp:
- Mar 6, 2006, 2:35:45 PM (17 years ago)
- Location:
- trunk/lib/classifier
- Files:
-
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/classifier/DataLookup2D.h
r537 r545 22 22 /// Default constructor. 23 23 /// 24 inline DataLookup2D( void) : owner_(false){};24 inline DataLookup2D(const bool owner=false) : owner_(owner){}; 25 25 26 26 -
trunk/lib/classifier/Kernel.h
r542 r545 10 10 11 11 #include <cctype> 12 #include <vector> 12 13 13 14 namespace theplu { … … 37 38 /// @note Can not handle NaNs. 38 39 /// 39 Kernel(const MatrixLookup& data, const KernelFunction& kf) 40 : data_(data), kf_(&kf) {}; 40 Kernel(const MatrixLookup& data, const KernelFunction& kf); 41 42 Kernel(const MatrixLookup& data, const KernelFunction& kf, 43 const MatrixLookup& weight); 41 44 45 /// 46 /// @todo doc 47 /// 48 Kernel(const Kernel& kernel, const std::vector<size_t>& index); 49 42 50 /// 43 51 /// Destructor 44 52 /// 45 virtual ~Kernel(void) {};53 virtual ~Kernel(void); 46 54 47 55 /// … … 56 64 inline size_t columns(void) const { return size(); } 57 65 66 inline const MatrixLookup& data(void) const { return *data_; } 67 58 68 /// 59 69 /// @return number of rows in Kernel … … 64 74 /// @brief number of samples 65 75 /// 66 inline size_t size(void) const { return data_ .columns(); }76 inline size_t size(void) const { return data_->columns(); } 67 77 68 78 … … 71 81 const size_t i) const=0; 72 82 83 /// 84 /// Created Kernel is built from selected features in data. The 85 /// @a index corresponds to which rows in data to use for the 86 /// calculation of the returned Kernel. 87 /// 88 /// @return Dynamically allocated Kernel based on selected features 89 /// 90 /// @Note Returns a dynamically allocated Kernel, which has 91 /// to be deleted by the caller to avoid memory leaks. 92 /// 93 virtual const Kernel* selected(const std::vector<size_t>& index) const=0; 94 95 /// 96 /// @return true if kernel is calculated using weights 97 /// 98 virtual bool weighted(void) const=0; 99 100 inline const MatrixLookup& weights(void) const { return *weights_; } 101 73 102 protected: 74 // Peter should be a copy75 const MatrixLookup & data_;103 const MatrixLookup* data_; 104 const MatrixLookup* weights_; 76 105 const KernelFunction* kf_; 106 const bool data_owner_; 107 const bool weight_owner_; 77 108 78 109 private: -
trunk/lib/classifier/KernelLookup.cc
r537 r545 13 13 namespace classifier { 14 14 15 KernelLookup::KernelLookup(const Kernel& kernel )16 : DataLookup2D( ), kernel_(&kernel)15 KernelLookup::KernelLookup(const Kernel& kernel, const bool own) 16 : DataLookup2D(own), kernel_(&kernel) 17 17 { 18 18 column_index_.reserve(kernel.size()); … … 71 71 } 72 72 73 const MatrixLookup* KernelLookup::weights(void) const 74 { return new MatrixLookup(kernel_->weights(),column_index_); } 75 76 const KernelLookup* 77 KernelLookup::selected(const std::vector<size_t>& index) const 78 { 79 const Kernel* kernel = kernel_->selected(index); 80 return new KernelLookup(*kernel, true); 81 } 73 82 74 83 }} // of namespace classifier and namespace theplu -
trunk/lib/classifier/KernelLookup.h
r542 r545 22 22 23 23 /// 24 /// Constructor 24 /// @brief Constructor from a Kernel 25 /// 26 /// @parameter own if true @a kernel is deleted in destructor, 27 /// i.e., it must be dynamically allocated. 25 28 /// 26 29 /// @note If underlying Kernel goes out of scope or is deleted, the … … 28 31 /// undefined. 29 32 /// 30 KernelLookup(const Kernel& kernel );33 KernelLookup(const Kernel& kernel, const bool own=false); 31 34 32 35 /// … … 102 105 { return (*kernel_)(row_index_[row],column_index_[column]); } 103 106 107 /// 108 /// Each column in returned MatrixLook corresponds to the column 109 /// in KernelLookup. 110 /// 111 /// @Note Returns a dynamically allocated MatrixLookup2D, which has 112 /// to be deleted by the caller to avoid memory leaks. 113 /// 114 inline const MatrixLookup* data(void) const 115 { return new MatrixLookup(kernel_->data(),column_index_); } 116 117 118 /// 119 /// @todo doc 120 /// 104 121 inline double element(const DataLookup1D& vec, const size_t i) const 105 122 { return kernel_->element(vec, row_index_[i]); } 106 123 124 /// 125 /// @todo doc 126 /// 107 127 inline double element(const DataLookup1D& vec, const DataLookup1D& w, 108 128 const size_t i) const 109 129 { return kernel_->element(vec, w, row_index_[i]); } 130 131 /// 132 /// @todo doc 133 /// 134 /// @Note Returns a dynamically allocated KernelLookup, which has 135 /// to be deleted by the caller to avoid memory leaks. 136 /// 137 const KernelLookup* selected(const std::vector<size_t>&) const; 138 139 /// 140 /// @return true if underlying Kernel is weighted 141 /// 142 inline bool weighted(void) const { return kernel_->weighted(); } 143 144 /// 145 /// @todo doc 146 /// 147 /// @Note Returns a dynamically allocated KernelLookup, which has 148 /// to be deleted by the caller to avoid memory leaks. 149 /// 150 const MatrixLookup* weights(void) const; 151 110 152 111 153 private: -
trunk/lib/classifier/KernelWeighted_MEV.cc
r528 r545 12 12 const size_t column) const 13 13 { 14 return (*kf_)(DataLookup1D( data_,row,false),15 DataLookup1D( data_,column,false),16 DataLookup1D( weights_,row,false),17 DataLookup1D( weights_,column,false));14 return (*kf_)(DataLookup1D(*data_,row,false), 15 DataLookup1D(*data_,column,false), 16 DataLookup1D(*weights_,row,false), 17 DataLookup1D(*weights_,column,false)); 18 18 } 19 20 KernelWeighted_MEV::KernelWeighted_MEV(const MatrixLookup& data, 21 const KernelFunction& kf, 22 const MatrixLookup& weights) 23 : Kernel(data,kf, weights) 24 { 25 } 26 27 28 KernelWeighted_MEV::KernelWeighted_MEV(const KernelWeighted_MEV& other, 29 const std::vector<size_t>& index) 30 : Kernel(other, index) 31 { 32 } 33 34 35 const Kernel* 36 KernelWeighted_MEV::selected(const std::vector<size_t>& index) const 37 { 38 return new KernelWeighted_MEV(*this, index); 39 } 19 40 20 41 -
trunk/lib/classifier/KernelWeighted_MEV.h
r542 r545 38 38 /// behaviour of the object is undefined 39 39 /// 40 inline KernelWeighted_MEV(const MatrixLookup& data, 41 const KernelFunction& kf, 42 const MatrixLookup& weights) 43 : Kernel(data,kf), weights_(weights) {} 40 KernelWeighted_MEV(const MatrixLookup& data, 41 const KernelFunction& kf, 42 const MatrixLookup& weights); 43 44 /// 45 /// @todo doc 46 /// 47 KernelWeighted_MEV(const KernelWeighted_MEV& other, 48 const std::vector<size_t>& index); 44 49 45 50 /// … … 54 59 inline double element(const DataLookup1D& vec, const size_t i) const 55 60 { 56 return (*kf_)(vec, DataLookup1D( data_,i),61 return (*kf_)(vec, DataLookup1D(*data_,i), 57 62 DataLookup1D(vec.size(),1.0), 58 DataLookup1D( weights_,i));63 DataLookup1D(*weights_,i)); 59 64 } 60 65 66 /// 67 /// @todo doc 68 /// 61 69 inline double element(const DataLookup1D& vec, const DataLookup1D& w, 62 70 const size_t i) const 63 71 { 64 return (*kf_)(vec, DataLookup1D( data_,i),w,DataLookup1D(weights_,i));72 return (*kf_)(vec, DataLookup1D(*data_,i),w,DataLookup1D(*weights_,i)); 65 73 } 74 75 /// 76 /// @todo doc 77 /// 78 const Kernel* selected(const std::vector<size_t>& index) const; 79 80 inline bool weighted(void) const { return true; } 66 81 67 82 private: … … 71 86 KernelWeighted_MEV(const KernelWeighted_MEV&); 72 87 73 const MatrixLookup& weights_;74 88 75 89 }; -
trunk/lib/classifier/KernelWeighted_SEV.cc
r527 r545 14 14 15 15 16 KernelWeighted_SEV::KernelWeighted_SEV(const MatrixLookup& data, 17 const KernelFunction& kf, 18 const MatrixLookup& weights) 19 : Kernel(data,kf), weights_(weights) 20 { 21 kernel_matrix_ = gslapi::matrix(data.columns(),data.columns()); 22 for (size_t i=0; i<kernel_matrix_.rows(); i++) 23 for (size_t j=i; j<kernel_matrix_.columns(); j++) 24 kernel_matrix_(i,j) = kernel_matrix_(j,i) = 25 (*kf_)(DataLookup1D(data_,i,false), 26 DataLookup1D(data_,j,false), 27 DataLookup1D(weights_,i,false), 28 DataLookup1D(weights_,j,false)); 29 } 16 KernelWeighted_SEV::KernelWeighted_SEV(const MatrixLookup& data, 17 const KernelFunction& kf, 18 const MatrixLookup& weights) 19 : Kernel(data,kf,weights) 20 { 21 kernel_matrix_ = gslapi::matrix(data.columns(),data.columns()); 22 for (size_t i=0; i<kernel_matrix_.rows(); i++) 23 for (size_t j=i; j<kernel_matrix_.columns(); j++) 24 kernel_matrix_(i,j) = kernel_matrix_(j,i) = 25 (*kf_)(DataLookup1D(*data_,i,false), 26 DataLookup1D(*data_,j,false), 27 DataLookup1D(*weights_,i,false), 28 DataLookup1D(*weights_,j,false)); 29 } 30 31 32 KernelWeighted_SEV::KernelWeighted_SEV(const KernelWeighted_SEV& other, 33 const std::vector<size_t>& index) 34 : Kernel(other, index) 35 { 36 } 37 38 39 const KernelWeighted_SEV* 40 KernelWeighted_SEV::selected(const std::vector<size_t>& index) const 41 { 42 return new KernelWeighted_SEV(*this, index); 43 } 44 30 45 31 46 -
trunk/lib/classifier/KernelWeighted_SEV.h
r542 r545 43 43 const MatrixLookup& weights); 44 44 45 /// 46 /// @todo doc 47 /// 48 KernelWeighted_SEV(const KernelWeighted_SEV& other, 49 const std::vector<size_t>& index); 45 50 46 51 /// … … 56 61 inline double element(const DataLookup1D& vec, const size_t i) const 57 62 { 58 return (*kf_)(vec, DataLookup1D( data_,i),63 return (*kf_)(vec, DataLookup1D(*data_,i), 59 64 DataLookup1D(vec.size(),1.0), 60 DataLookup1D( weights_,i));65 DataLookup1D(*weights_,i)); 61 66 } 62 67 … … 64 69 const size_t i) const 65 70 { 66 return (*kf_)(vec, DataLookup1D( data_,i),w,DataLookup1D(weights_,i));71 return (*kf_)(vec, DataLookup1D(*data_,i),w,DataLookup1D(*weights_,i)); 67 72 } 73 74 /// 75 /// @todo doc 76 /// 77 const KernelWeighted_SEV* selected(const std::vector<size_t>& index) const; 78 79 /// 80 /// @return true 81 /// 82 inline bool weighted(void) const { return true; } 68 83 69 84 private: … … 72 87 73 88 gslapi::matrix kernel_matrix_; 74 const MatrixLookup& weights_;75 89 76 90 }; // class Kernel_SEV -
trunk/lib/classifier/Kernel_MEV.cc
r542 r545 8 8 namespace classifier { 9 9 10 Kernel_MEV::Kernel_MEV(const Kernel_MEV& kernel, 11 const std::vector<size_t>& index) 12 : Kernel(kernel,index) 13 { 14 } 15 16 10 17 double Kernel_MEV::operator()(const size_t row, const size_t column) const 11 18 { 12 return (*kf_)(DataLookup1D( data_,row,false),13 DataLookup1D( data_,column,false));19 return (*kf_)(DataLookup1D(*data_,row,false), 20 DataLookup1D(*data_,column,false)); 14 21 } 15 22 23 24 const Kernel_MEV* Kernel_MEV::selected(const std::vector<size_t>& index) const 25 { 26 return new Kernel_MEV(*this, index); 27 } 28 29 30 16 31 }} // of namespace classifier and namespace theplu -
trunk/lib/classifier/Kernel_MEV.h
r542 r545 37 37 : Kernel(data,kf) {} 38 38 39 /// 40 /// @todo doc 41 /// 42 Kernel_MEV(const Kernel_MEV& kernel, const std::vector<size_t>& index); 43 44 39 45 /// 40 46 /// Destructor … … 52 58 /// 53 59 inline double element(const DataLookup1D& vec, const size_t i) const 54 { return kf_->operator()(vec, DataLookup1D( data_,i)); }60 { return kf_->operator()(vec, DataLookup1D(*data_,i)); } 55 61 56 62 inline double element(const DataLookup1D& vec, const DataLookup1D& w, 57 63 const size_t i) const 58 {return (*kf_)(vec, DataLookup1D(data_,i), w, DataLookup1D(w.size(),1.0));} 64 {return (*kf_)(vec, DataLookup1D(*data_,i), w, DataLookup1D(w.size(),1.0));} 65 66 const Kernel_MEV* selected(const std::vector<size_t>& index) const; 67 68 inline bool weighted(void) const { return false; } 59 69 60 70 private: -
trunk/lib/classifier/Kernel_SEV.cc
r542 r545 21 21 for (size_t j=i; j<kernel_matrix_.columns(); j++) 22 22 kernel_matrix_(i,j) = kernel_matrix_(j,i) = 23 (*kf_)(DataLookup1D( data_,i,false),DataLookup1D(data_,j,false));23 (*kf_)(DataLookup1D(*data_,i,false),DataLookup1D(*data_,j,false)); 24 24 } 25 25 26 Kernel_SEV::Kernel_SEV(const Kernel_SEV& other, 27 const std::vector<size_t>& index) 28 : Kernel(other, index) 29 { 30 } 26 31 27 32 double Kernel_SEV::element(const DataLookup1D& vec, const size_t i) const 28 33 { 29 return kf_->operator()(vec, DataLookup1D( data_,i));34 return kf_->operator()(vec, DataLookup1D(*data_,i)); 30 35 } 31 36 … … 33 38 const size_t i) const 34 39 { 35 return (*kf_)(vec, DataLookup1D(data_,i), w, DataLookup1D(w.size(),1.0)); 40 return (*kf_)(vec, DataLookup1D(*data_,i), w, DataLookup1D(w.size(),1.0)); 41 } 42 43 const Kernel* Kernel_SEV::selected(const std::vector<size_t>& index) const 44 { 45 return new Kernel_SEV(*this, index); 36 46 } 37 47 -
trunk/lib/classifier/Kernel_SEV.h
r542 r545 46 46 Kernel_SEV(const Kernel_SEV&); 47 47 48 ///49 /// Destructor50 48 /// 51 inline virtual ~Kernel_SEV(void) {}; 49 /// @todo doc 50 /// 51 Kernel_SEV(const Kernel_SEV& kernel, const std::vector<size_t>& index); 52 52 53 53 /// … … 65 65 const size_t i) const; 66 66 67 const Kernel* selected(const std::vector<size_t>& index) const; 67 68 69 inline bool weighted(void) const { return false; } 68 70 private: 69 71 gslapi::matrix kernel_matrix_; -
trunk/lib/classifier/Makefile.am
r527 r545 16 16 GaussianKernelFunction.cc \ 17 17 InputRanker.cc \ 18 Kernel.cc \ 18 19 Kernel_MEV.cc \ 19 20 Kernel_SEV.cc \ -
trunk/lib/classifier/MatrixLookup.h
r537 r545 85 85 86 86 /// 87 /// Constructor taking the column (default) or row index vector 88 /// as input. 87 /// Constructor taking the column (default) or row index vector as 88 /// input. If @a row is false the created MatrixLookup will have 89 /// equally many rows as @a matrix. 89 90 /// 90 91 /// @note If underlying matrix goes out of scope or is deleted, the -
trunk/lib/classifier/SVM.cc
r544 r545 4 4 5 5 #include <c++_tools/classifier/DataLookup2D.h> 6 #include <c++_tools/classifier/InputRanker.h> 6 7 #include <c++_tools/gslapi/matrix.h> 7 8 #include <c++_tools/gslapi/vector.h> … … 29 30 max_epochs_(10000000), 30 31 output_(target.size(),0), 32 owner_(false), 31 33 sample_(target.size()), 32 34 trained_(false), … … 41 43 bias_(0), 42 44 C_inverse_(0), 43 kernel_(&kernel),44 45 max_epochs_(10000000), 45 46 output_(target.size(),0), … … 48 49 tolerance_(0.00000001) 49 50 { 51 52 if (kernel.weighted()){ 53 const MatrixLookup* data = kernel.data(); 54 const MatrixLookup* weights = kernel.weights(); 55 ranker_ = new InputRanker(*data, target, score, *weights); 56 delete data; 57 delete weights; 58 } 59 else{ 60 const MatrixLookup* data = kernel.data(); 61 ranker_ = new InputRanker(*data, target, score); 62 delete data; 63 } 64 65 std::vector<size_t> index; 66 index.reserve(nof_inputs); 67 for (size_t i=0; i<nof_inputs; i++) 68 index.push_back(ranker_->id(i)); 69 kernel_ = kernel.selected(index); 70 owner_ = true; 71 50 72 } 51 73 … … 58 80 const KernelLookup& tmp = dynamic_cast<const KernelLookup&>(data); 59 81 SVM* sc; 60 if ( score_)82 if (ranker_) 61 83 sc = new SVM(tmp,target,*score_,nof_inputs_); 62 84 else … … 69 91 void SVM::predict(const DataLookup2D& input, gslapi::matrix& prediction) const 70 92 { 93 // Peter, should check success of dynamic_cast 94 const KernelLookup* input_kernel= &dynamic_cast<const KernelLookup&>(input); 95 96 if (ranker_) {// feature selection 97 std::vector<size_t> index; 98 index.reserve(nof_inputs_); 99 for (size_t i=0; i<nof_inputs_; i++) 100 index.push_back(ranker_->id(i)); 101 input_kernel = input_kernel->selected(index); 102 } 103 71 104 assert(input.rows()==alpha_.size()); 72 105 prediction=gslapi::matrix(2,input.columns(),0); 73 106 for (size_t i = 0; i<input.columns(); i++){ 74 107 for (size_t j = 0; i<input.rows(); i++) 75 prediction(0,i) += target(j)*alpha_(i)* input(j,i);108 prediction(0,i) += target(j)*alpha_(i)*(*input_kernel)(j,i); 76 109 prediction(0,i) += bias_; 77 110 } -
trunk/lib/classifier/SVM.h
r543 r545 251 251 unsigned long int max_epochs_; 252 252 gslapi::vector output_; 253 bool owner_; 253 254 Index sample_; 254 255 bool trained_;
Note: See TracChangeset
for help on using the changeset viewer.