Changeset 659
- Timestamp:
- Sep 26, 2006, 3:44:24 PM (17 years ago)
- Location:
- trunk/c++_tools/classifier
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/c++_tools/classifier/DataLookup2D.cc
r658 r659 70 70 71 71 DataLookup2D::DataLookup2D(const std::vector<size_t>& row, 72 const std::vector<size_t>& col) 72 const std::vector<size_t>& col, 73 const bool own) 73 74 : row_index_(row),column_index_(col), ref_count_(NULL) 74 75 { 76 if (own) 77 ref_count_ = new u_int(1); 75 78 } 76 79 -
trunk/c++_tools/classifier/DataLookup2D.h
r658 r659 39 39 /// 40 40 DataLookup2D(const std::vector<size_t>& row, 41 const std::vector<size_t>& column); 41 const std::vector<size_t>& column, 42 const bool own=false); 42 43 43 44 /// -
trunk/c++_tools/classifier/EnsembleBuilder.cc
r635 r659 52 52 size_t k=0; 53 53 utility::matrix prediction; 54 55 56 54 try { 57 55 const KernelLookup& kernel = dynamic_cast<const KernelLookup&>(data); 58 56 while(subset_.more()) { 59 classifier(k++).predict(KernelLookup(kernel, 60 subset_.training_index(), 61 true), 62 prediction); 57 KernelLookup kernel_peter(kernel,subset_.training_index(),true); 58 classifier(k++).predict(kernel_peter,prediction); 59 63 60 for(size_t i=0; i<prediction.rows();i++) 64 61 for(size_t j=0; j<prediction.columns();j++) -
trunk/c++_tools/classifier/Kernel.cc
r658 r659 15 15 Kernel::Kernel(const MatrixLookup& data, const KernelFunction& kf, 16 16 const bool own) 17 : data_(&data), data_w_(0), kf_(&kf), data_owner_(own), 18 weight_owner_(false) 17 : data_(&data), data_w_(0), kf_(&kf), ref_count_w_(NULL) 19 18 { 19 if (own) 20 ref_count_ = new u_int(1); 21 else 22 ref_count_ = NULL; 20 23 } 21 24 … … 23 26 Kernel::Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf, 24 27 const bool own) 25 : data_(&data), data_w_(&data), kf_(&kf), data_owner_(own), 26 weight_owner_(own) 28 : data_(&data), data_w_(&data), kf_(&kf) 27 29 { 30 if (own){ 31 ref_count_ = new u_int(1); 32 ref_count_w_ = new u_int(1); 33 } 34 else { 35 ref_count_ = NULL; 36 ref_count_w_ = NULL; 37 } 28 38 } 29 39 30 40 31 41 Kernel::Kernel(const Kernel& other, const std::vector<size_t>& index) 32 : kf_(other.kf_) , data_owner_(true)42 : kf_(other.kf_) 33 43 { 34 44 data_ = other.data_->selected(index); 45 ref_count_ = new u_int(1); 46 35 47 if (other.data_w_){ 36 48 data_w_ = other.data_w_->selected(index); 37 weight_owner_=true;49 ref_count_w_ = new u_int(1); 38 50 } 39 51 else{ 40 52 data_w_=NULL; 41 weight_owner_=false;53 ref_count_w_ = NULL; 42 54 } 43 55 … … 46 58 Kernel::~Kernel() 47 59 { 48 if (data_owner_) 49 delete data_; 50 51 if (weight_owner_) 52 if (data_w_) 60 if (ref_count_) 61 if (!--(*ref_count_)) 62 delete data_; 63 64 if (ref_count_w_) 65 if (!--(*ref_count_w_)) 53 66 delete data_w_; 54 else 55 std::cerr << "Error in Kernel implementation: probably a constructor" 56 << std::endl; 57 67 58 68 } 59 69 -
trunk/c++_tools/classifier/Kernel.h
r658 r659 148 148 149 149 protected: 150 /// underly ung data150 /// underlying data 151 151 const DataLookup2D* data_; 152 152 /// same as data_ if weifghted otherwise a NULL pointer … … 154 154 /// type of Kernel Function e.g. Gaussian (aka RBF) 155 155 const KernelFunction* kf_; 156 /// if true we own data and will delete it in destructor 157 const bool data_owner_; 158 /// if true we own data_w and will delete it in destructor 159 bool weight_owner_; 156 157 /// 158 /// poiter telling how many owners to underlying data 159 /// (data_). NULL if this is not an owner. 160 /// 161 u_int* ref_count_; 162 163 /// 164 /// poiter telling how many owners to underlying weights 165 /// (data_w_). NULL if this is not an owner. 166 /// 167 u_int* ref_count_w_; 160 168 161 169 private: -
trunk/c++_tools/classifier/KernelLookup.cc
r658 r659 16 16 17 17 KernelLookup::KernelLookup(const Kernel& kernel, const bool own) 18 : DataLookup2D(), kernel_(&kernel) 19 { 20 if (own) 21 ref_count_ = new u_int(1); 22 18 : DataLookup2D(own), kernel_(&kernel) 19 { 23 20 column_index_.reserve(kernel.size()); 24 21 for(size_t i=0; i<kernel.size(); i++) … … 31 28 const std::vector<size_t>& column, 32 29 const bool owner) 33 : DataLookup2D(row,column), kernel_(&kernel) 34 { 35 if (owner) 36 ref_count_ = new u_int(1); 37 30 : DataLookup2D(row,column,owner), kernel_(&kernel) 31 { 38 32 // Checking that each row index is less than kernel.rows() 39 33 assert(row.empty() || … … 42 36 assert(column.empty() || 43 37 *(std::max_element(column.begin(),column.end()))<kernel_->size()); 44 45 38 } 46 39 … … 70 63 if (ref_count_) 71 64 ++(*ref_count_); 72 73 65 } 74 66 … … 79 71 : DataLookup2D(other,index,row), kernel_(other.kernel_) 80 72 { 73 assert(kernel_->size()); 74 81 75 // Checking that no index is out of range 82 76 assert(row_index_.empty() || … … 124 118 const KernelLookup* KernelLookup::test_kernel(const MatrixLookup& data) const 125 119 { 120 126 121 assert(data.rows()==kernel_->data().rows()); 127 122 if (!weighted()){ 128 123 utility::matrix* data_all = 129 new utility::matrix(data.rows(), rows()+data.columns()); 130 for (size_t i=0; i<data.rows(); ++i){ 131 // first columns are equal to data in kernel_ 132 for (size_t j=0; j<row_index_.size(); ++j) 133 (*data_all)(i,j) = kernel_->data()(i,row_index_[j]); 124 new utility::matrix(data.rows(), row_index_.size()+data.columns()); 125 126 for (size_t i=0; i<data_all->rows(); ++i) { 127 128 // first some columns from data in kernel_ 129 for (size_t j=0; j<row_index_.size(); ++j){ 130 (*data_all)(i,j) = kernel_->data()(i,row_index_[j]); 131 } 132 134 133 // last columns are equal to new data 135 for (size_t j=0;j<data.columns(); ++j) 134 for (size_t j=0;j<data.columns(); ++j){ 136 135 (*data_all)(i,j+row_index_.size()) = data(i,j); 136 } 137 137 } 138 138 std::vector<size_t> column_index; … … 140 140 for (size_t i=0;i<data.columns(); ++i) 141 141 column_index.push_back(i+row_index_.size()); 142 143 std::vector<size_t> row_index; 144 row_index.reserve(row_index_.size()); 145 for (size_t i=0;i<row_index_.size(); ++i) 146 row_index.push_back(i); 147 148 const MatrixLookup* tmp = new MatrixLookup(*data_all, true); 149 142 150 const Kernel* kernel = 143 kernel_->make_kernel(MatrixLookup(*data_all, true), true); 144 return new KernelLookup(*kernel, row_index_, column_index, true); 151 kernel_->make_kernel(*tmp, true); 152 153 return new KernelLookup(*kernel, row_index, column_index, true); 145 154 } 146 155 … … 155 164 156 165 for (size_t i=0; i<data.rows(); ++i){ 157 // first columns are equal to data in kernel_ 166 167 // first some columns from data in kernel_ 158 168 for (size_t j=0; j<row_index_.size(); ++j){ 159 (*data_all)(i,j) = kernel_data.data(i,row_index_[j]); 169 (*data_all)(i,j) = kernel_data.data(i,row_index_[j]); 160 170 (*weight_all)(i,j) = kernel_data.weight(i,row_index_[j]); 161 171 } 172 162 173 // last columns are equal to new data 163 174 for (size_t j=0;j<data.columns(); ++j){ … … 169 180 for (size_t i=0;i<data.columns(); ++i) 170 181 column_index.push_back(i+row_index_.size()); 171 const Kernel* kernel = 172 kernel_->make_kernel(MatrixLookupWeighted(*data_all, *weight_all, true)); 182 183 std::vector<size_t> row_index; 184 row_index.reserve(row_index_.size()); 185 for (size_t i=0;i<row_index_.size(); ++i) 186 row_index.push_back(i); 187 188 MatrixLookupWeighted* tmp = new MatrixLookupWeighted(*data_all, 189 *weight_all, true); 190 const Kernel* kernel = kernel_->make_kernel(*tmp, true); 173 191 return new KernelLookup(*kernel, row_index_, column_index, true); 174 192 } -
trunk/c++_tools/classifier/KernelLookup.h
r658 r659 217 217 218 218 /// 219 /// Weighted version of element function. Using weights @a w all 220 /// identical to unity results in same as using the unweighted 221 /// version above. 219 /// Function to calculate a new Kernel element using the 220 /// underlying KernelFunction. The value is calulated between @a 221 /// vec and the data vector of the \f$ i \f$ th sample, in other 222 /// words, the sample corresponding to the \f$ i \f$ th row or 223 /// \f$ i \f$ th column. In case KernelLookup is a sub-Kernel and not 224 /// symmetric, the kernel value is calculated between @a vec and 225 /// the data vector corresponding to \f$ i \f$ th row. 222 226 /// 223 227 inline double element(const DataLookupWeighted1D& vec, const size_t i) const … … 240 244 inline bool weighted(void) const { return kernel_->weighted(); } 241 245 246 inline const Kernel* kernel(void) const { return kernel_; } 247 242 248 private: 243 249 const KernelLookup& operator=(const KernelLookup&); -
trunk/c++_tools/classifier/SVM.cc
r640 r659 95 95 const KernelLookup& input_kernel = dynamic_cast<const KernelLookup&>(input); 96 96 97 const KernelLookup* kernel_pointer;98 kernel_pointer = &input_kernel;99 100 97 assert(input.rows()==alpha_.size()); 101 98 prediction = utility::matrix(2,input.columns(),0); 102 99 for (size_t i = 0; i<input.columns(); i++){ 103 100 for (size_t j = 0; j<input.rows(); j++){ 104 prediction(0,i) += target(j)*alpha_(j)* (*kernel_pointer)(j,i);101 prediction(0,i) += target(j)*alpha_(j)*input_kernel(j,i); 105 102 assert(target(j)); 106 103 }
Note: See TracChangeset
for help on using the changeset viewer.