Changeset 640
- Timestamp:
- Sep 7, 2006, 7:11:18 AM (17 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/c++_tools/classifier/DataLookup2D.cc
r608 r640 15 15 16 16 17 DataLookup2D::DataLookup2D() 18 : ref_count_(NULL) 19 { 20 } 21 22 17 23 DataLookup2D::DataLookup2D(const DataLookup2D& m, 18 24 const std::vector<size_t>& row, 19 25 const std::vector<size_t>& col) 20 : owner_(false)26 : ref_count_(NULL) 21 27 { 22 28 assert(row_index_.empty()); … … 39 45 const std::vector<size_t>& index, 40 46 const bool row) 41 : owner_(false)47 : ref_count_(NULL) 42 48 { 43 49 if (row){ … … 62 68 63 69 DataLookup2D::DataLookup2D(const std::vector<size_t>& row, 64 const std::vector<size_t>& col, 65 const bool owner) 66 : row_index_(row),column_index_(col), owner_(owner) 70 const std::vector<size_t>& col) 71 : row_index_(row),column_index_(col), ref_count_(NULL) 67 72 { 68 73 } … … 71 76 72 77 DataLookup2D::DataLookup2D(const DataLookup2D& mv) 73 : row_index_(mv.row_index_),column_index_(mv.column_index_), owner_(false) 78 : row_index_(mv.row_index_),column_index_(mv.column_index_), 79 ref_count_(NULL) 74 80 { 75 81 } 76 82 77 83 78 DataLookup2D::DataLookup2D(const size_t rows, const size_t columns, 79 const bool owner) 84 DataLookup2D::DataLookup2D(const size_t rows, const size_t columns) 80 85 : row_index_(std::vector<size_t>(rows,0)), 81 column_index_(std::vector<size_t>(columns,0)), 82 owner_(owner) 86 column_index_(std::vector<size_t>(columns,0)), ref_count_(NULL) 87 83 88 { 84 89 } … … 90 95 row_index_ = other.row_index_; 91 96 column_index_ = other.column_index_; 92 owner_=false;93 97 } 94 98 return *this; -
trunk/c++_tools/classifier/DataLookup2D.h
r631 r640 31 31 /// Default constructor. 32 32 /// 33 inline DataLookup2D(const bool owner=false) : owner_(owner){};33 DataLookup2D(void); 34 34 35 35 36 36 /// 37 37 /// Constructor taking the @a row index vector and @a column index 38 /// vector as input. If @a owner is set true, the object is 39 /// consider as owner of the underlying data (and the data is 40 /// deleted at destruction). 38 /// vector as input. 41 39 /// 42 40 DataLookup2D(const std::vector<size_t>& row, 43 const std::vector<size_t>& column, 44 const bool owner = false); 41 const std::vector<size_t>& column); 45 42 46 43 /// … … 68 65 /// created in daughter classes. 69 66 /// 70 DataLookup2D(const size_t, const size_t , const bool owner);67 DataLookup2D(const size_t, const size_t); 71 68 72 69 … … 148 145 std::vector<size_t> row_index_; 149 146 std::vector<size_t> column_index_; 150 bool owner_; 147 u_int* ref_count_; 148 151 149 }; 152 150 -
trunk/c++_tools/classifier/FeatureSelectorIR.cc
r624 r640 4 4 5 5 #include "FeatureSelector.h" 6 #include "MatrixLookup.h" 6 7 #include "InputRanker.h" 7 8 … … 36 37 void FeatureSelectorIR::update(const MatrixLookup& data, const Target& target) 37 38 { 39 assert(data.columns()==target.size()); 38 40 InputRanker ir = InputRanker(data, target, score_); 39 41 features_.resize(N_); 40 42 std::copy(ir.rank().begin()+first_, ir.rank().begin()+first_+N_, 41 43 features_.begin()); 42 43 44 } 44 45 -
trunk/c++_tools/classifier/InputRanker.h
r630 r640 52 52 /// id (column) \a i 53 53 /// 54 inline const std::vector<size_t> rank(void) const {return rank_;}54 inline const std::vector<size_t>& rank(void) const {return rank_;} 55 55 56 56 -
trunk/c++_tools/classifier/Kernel.cc
r628 r640 30 30 : kf_(other.kf_), data_owner_(true) 31 31 { 32 // Peter go through this code; look fishy!33 32 data_ = other.data_->selected(index); 34 if ( data_w_){33 if (other.data_w_){ 35 34 data_w_ = other.data_w_->selected(index); 36 35 weight_owner_=true; -
trunk/c++_tools/classifier/Kernel.h
r629 r640 119 119 /// @return true if kernel is calculated using weights 120 120 /// 121 // Peter make non-virtual?122 121 inline bool weighted(void) const { return data_w_; } 123 122 -
trunk/c++_tools/classifier/KernelLookup.cc
r628 r640 14 14 15 15 KernelLookup::KernelLookup(const Kernel& kernel, const bool own) 16 : DataLookup2D( own), kernel_(&kernel)16 : DataLookup2D(), kernel_(&kernel) 17 17 { 18 if (own) 19 ref_count_ = new u_int(1); 20 18 21 column_index_.reserve(kernel.size()); 19 22 for(size_t i=0; i<kernel.size(); i++) … … 26 29 const std::vector<size_t>& column, 27 30 const bool owner) 28 : DataLookup2D(row,column ,owner), kernel_(&kernel)31 : DataLookup2D(row,column), kernel_(&kernel) 29 32 { 33 if (owner) 34 ref_count_ = new u_int(1); 35 30 36 // Checking that each row index is less than kernel.rows() 31 37 assert(row.empty() || … … 38 44 39 45 40 KernelLookup::KernelLookup(const KernelLookup& kernel,46 KernelLookup::KernelLookup(const KernelLookup& other, 41 47 const std::vector<size_t>& row, 42 48 const std::vector<size_t>& column) 43 : DataLookup2D( kernel,row,column), kernel_(kernel.kernel_)49 : DataLookup2D(other,row,column), kernel_(other.kernel_) 44 50 { 51 ref_count_=other.ref_count_; 52 if (ref_count_) 53 ++(*ref_count_); 45 54 } 46 55 47 56 48 KernelLookup::KernelLookup(const KernelLookup& kernel)49 : DataLookup2D( kernel), kernel_(kernel.kernel_)57 KernelLookup::KernelLookup(const KernelLookup& other) 58 : DataLookup2D(other), kernel_(other.kernel_) 50 59 { 51 60 // Checking that no index is out of range 52 61 assert(row_index_.empty() || 53 *(max_element(row_index_.begin(), row_index_.end()))<kernel_->size()); 62 *(max_element(row_index_.begin(), row_index_.end()))< 63 kernel_->size()); 54 64 assert(column_index_.empty() || 55 65 *(max_element(column_index_.begin(), column_index_.end()))< 56 66 kernel_->size()); 67 ref_count_=other.ref_count_; 68 if (ref_count_) 69 ++(*ref_count_); 57 70 58 71 } 59 72 60 73 61 KernelLookup::KernelLookup(const KernelLookup& kl,74 KernelLookup::KernelLookup(const KernelLookup& other, 62 75 const std::vector<size_t>& index, 63 76 const bool row) 64 : DataLookup2D( kl,index,row), kernel_(kl.kernel_)77 : DataLookup2D(other,index,row), kernel_(other.kernel_) 65 78 { 66 79 // Checking that no index is out of range 67 80 assert(row_index_.empty() || 68 *(max_element(row_index_.begin(), row_index_.end()))<kernel_->size()); 81 *(max_element(row_index_.begin(), row_index_.end()))< 82 kernel_->size()); 69 83 assert(column_index_.empty() || 70 84 *(max_element(column_index_.begin(), column_index_.end()))< 71 85 kernel_->size()); 72 86 ref_count_=other.ref_count_; 87 if (ref_count_) 88 ++(*ref_count_); 73 89 } 74 90 … … 76 92 KernelLookup::~KernelLookup(void) 77 93 { 78 if (owner_) 79 delete kernel_; 94 if (ref_count_) 95 if (!--(*ref_count_)) 96 delete kernel_; 80 97 } 81 98 -
trunk/c++_tools/classifier/KernelLookup.h
r628 r640 47 47 /// kernel. By default @a owner is set to false, which means 48 48 /// KernelLookup does not own the underlying Kernel. If 49 /// KernelLookup owns the Kernel the n theKernel will be deleted49 /// KernelLookup owns the Kernel the Kernel will be deleted 50 50 /// in the destructor. 51 51 /// … … 53 53 /// KernelLookup becomes invalid and the result of further use is 54 54 /// undefined. 55 /// 56 /// @note Do not construct two KernelLookups from the same @a 57 /// kernel with @a owner set to true because that will cause 58 /// multiple deletion of @a kernel. 55 59 /// 56 60 KernelLookup(const Kernel& kernel, const bool owner=false); -
trunk/c++_tools/classifier/Kernel_SEV.h
r629 r640 57 57 58 58 /// 59 /// @todo remove this function59 /// @todo doc 60 60 /// 61 61 const Kernel* selected(const std::vector<size_t>& index) const; -
trunk/c++_tools/classifier/MatrixLookup.cc
r631 r640 41 41 42 42 MatrixLookup::MatrixLookup(const utility::matrix& data, 43 const std::vector<size_t>& index,44 const bool row)43 const std::vector<size_t>& index, 44 const bool row) 45 45 : DataLookup2D(), data_(&data) 46 46 { … … 69 69 70 70 71 MatrixLookup::MatrixLookup(const MatrixLookup& dv) 72 : DataLookup2D(dv), data_(dv.data_) 73 { 74 } 75 76 77 78 MatrixLookup::MatrixLookup(const MatrixLookup& ml, 71 MatrixLookup::MatrixLookup(const MatrixLookup& other) 72 : DataLookup2D(other), data_(other.data_) 73 { 74 ref_count_=other.ref_count_; 75 if (ref_count_) 76 ++(*ref_count_); 77 } 78 79 80 81 MatrixLookup::MatrixLookup(const MatrixLookup& other, 79 82 const std::vector<size_t>& row, 80 83 const std::vector<size_t>& col) 81 : DataLookup2D(ml,row,col), data_(ml.data_) 82 { 83 } 84 85 86 87 MatrixLookup::MatrixLookup(const MatrixLookup& ml, 84 : DataLookup2D(other,row,col), data_(other.data_) 85 { 86 ref_count_=other.ref_count_; 87 if (ref_count_) 88 ++(*ref_count_); 89 } 90 91 92 93 MatrixLookup::MatrixLookup(const MatrixLookup& other, 88 94 const std::vector<size_t>& index, bool row) 89 : DataLookup2D(ml,index,row), data_(ml.data_) 90 { 95 : DataLookup2D(other,index,row), data_(other.data_) 96 { 97 ref_count_=other.ref_count_; 98 if (ref_count_) 99 ++(*ref_count_); 100 91 101 // Checking that no index is out of range 92 102 assert(row_index_.empty() || … … 101 111 MatrixLookup::MatrixLookup(const size_t rows, const size_t columns, 102 112 const double value) 103 : DataLookup2D(rows,columns , true)113 : DataLookup2D(rows,columns) 104 114 { 105 115 data_ = new utility::matrix(1,1,value); 116 ref_count_= new u_int(1); 106 117 } 107 118 108 119 109 120 MatrixLookup::MatrixLookup(std::istream& is, char sep) 110 : DataLookup2D( true)121 : DataLookup2D() 111 122 { 112 123 data_ = new utility::matrix(is,sep); 124 ref_count_= new u_int(1); 113 125 for(size_t i=0;i<(*data_).rows();i++) 114 126 row_index_.push_back(i); … … 120 132 MatrixLookup::~MatrixLookup(void) 121 133 { 122 if (owner_) 123 delete data_; 134 if (ref_count_) 135 if (!--(*ref_count_)) 136 delete data_; 124 137 } 125 138 … … 179 192 { 180 193 if (this!=&other){ 181 if ( owner_){194 if (ref_count_ && !--(*ref_count_)) 182 195 delete data_; 183 owner_=false;184 }185 196 DataLookup2D::operator=(other); 186 197 data_ = other.data_; 198 ref_count_=other.ref_count_; 199 if (ref_count_) 200 ++(*ref_count_); 187 201 } 188 202 return *this; -
trunk/c++_tools/classifier/MatrixLookupWeighted.cc
r638 r640 16 16 MatrixLookupWeighted::MatrixLookupWeighted(const utility::matrix& data, 17 17 const utility::matrix& weights) 18 : DataLookup2D(), data_(&data), weights_(&weights), weights_owner_(false)18 : DataLookup2D(), data_(&data), weights_(&weights), ref_count_weights_(NULL) 19 19 { 20 20 assert(data.rows()==weights.rows()); … … 27 27 28 28 MatrixLookupWeighted::MatrixLookupWeighted(const utility::matrix& data) 29 : DataLookup2D(), data_(&data) , weights_owner_(true)29 : DataLookup2D(), data_(&data) 30 30 { 31 31 utility::matrix weights; 32 32 data_->nan(weights); 33 33 weights_= new utility::matrix(weights); 34 ref_count_weights_=new u_int(1); 34 35 for(size_t i=0;i<(*data_).rows();i++) 35 36 row_index_.push_back(i); … … 45 46 const std::vector<size_t>& col) 46 47 : DataLookup2D(row,col), data_(&data), weights_(&weights), 47 weights_owner_(false)48 ref_count_weights_(NULL) 48 49 { 49 50 // Checking that each row index is less than data.rows() … … 67 68 const std::vector<size_t>& index, 68 69 const bool row) 69 : DataLookup2D(), data_(&data), weights_(&weights),weights_owner_(false) 70 : DataLookup2D(), data_(&data), weights_(&weights), 71 ref_count_weights_(NULL) 70 72 { 71 73 if (row){ … … 107 109 108 110 109 MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& dv) 110 : DataLookup2D(dv), data_(dv.data_), weights_(dv.weights_), 111 weights_owner_(false) 112 { 113 } 114 115 116 117 MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& ml, 111 MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other) 112 : DataLookup2D(other), data_(other.data_), weights_(other.weights_) 113 { 114 ref_count_ = other.ref_count_; 115 if (ref_count_) 116 ++(*ref_count_); 117 ref_count_weights_ = other.ref_count_weights_; 118 if (ref_count_weights_) 119 ++(*ref_count_weights_); 120 121 } 122 123 124 125 MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other, 118 126 const std::vector<size_t>& row, 119 127 const std::vector<size_t>& col) 120 : DataLookup2D(ml,row,col), data_(ml.data_), weights_(ml.weights_), 121 weights_owner_(false) 122 { 123 } 124 125 126 127 MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& ml, 128 : DataLookup2D(other,row,col), data_(other.data_), weights_(other.weights_) 129 { 130 ref_count_ = other.ref_count_; 131 if (ref_count_) 132 ++(*ref_count_); 133 ref_count_weights_ = other.ref_count_weights_; 134 if (ref_count_weights_) 135 ++(*ref_count_weights_); 136 } 137 138 139 140 MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other, 128 141 const std::vector<size_t>& index, 129 142 bool row) 130 : DataLookup2D(ml,index,row), data_(ml.data_), weights_(ml.weights_), 131 weights_owner_(false) 132 { 143 : DataLookup2D(other,index,row), data_(other.data_), 144 weights_(other.weights_) 145 { 146 ref_count_ = other.ref_count_; 147 if (ref_count_) 148 ++(*ref_count_); 149 ref_count_weights_ = other.ref_count_weights_; 150 if (ref_count_weights_) 151 ++(*ref_count_weights_); 152 133 153 // Checking that no index is out of range 134 154 assert(row_index_.empty() || … … 152 172 const double value, 153 173 const double weight) 154 : DataLookup2D(rows,columns , true),weights_owner_(true)174 : DataLookup2D(rows,columns) 155 175 { 156 176 data_ = new utility::matrix(1,1,value); 177 ref_count_=new u_int(1); 157 178 weights_ = new utility::matrix(1,1,weight); 179 ref_count_weights_=new u_int(1); 158 180 } 159 181 160 182 161 183 MatrixLookupWeighted::MatrixLookupWeighted(std::istream& is, char sep) 162 : DataLookup2D( true), weights_owner_(true)184 : DataLookup2D() 163 185 { 164 186 data_ = new utility::matrix(is,sep); 187 ref_count_=new u_int(1); 165 188 for(size_t i=0;i<(*data_).rows();i++) 166 189 row_index_.push_back(i); … … 170 193 data_->nan(weights); 171 194 weights_= new utility::matrix(weights); 195 ref_count_weights_=new u_int(1); 172 196 } 173 197 … … 175 199 MatrixLookupWeighted::~MatrixLookupWeighted(void) 176 200 { 177 if (owner_) 178 delete data_; 179 if (weights_owner_) 180 delete weights_; 201 if (ref_count_) 202 if (!--(*ref_count_)) 203 delete data_; 204 if (ref_count_weights_) 205 if (!--(*ref_count_weights_)) 206 delete weights_; 181 207 } 182 208 … … 237 263 { 238 264 if (this!=&other){ 239 if ( owner_){265 if (ref_count_ && !--(*ref_count_)) 240 266 delete data_; 267 if (ref_count_weights_ && !--(*ref_count_weights_)) 241 268 delete weights_; 242 owner_=false;243 }244 269 DataLookup2D::operator=(other); 245 270 data_ = other.data_; 271 ref_count_=other.ref_count_; 272 if (ref_count_) 273 ++(*ref_count_); 246 274 weights_ = other.weights_; 275 ref_count_weights_ = other.ref_count_weights_; 276 if (ref_count_weights_) 277 ++(*ref_count_weights_); 247 278 } 248 279 return *this; -
trunk/c++_tools/classifier/MatrixLookupWeighted.h
r638 r640 294 294 const utility::matrix* data_; 295 295 const utility::matrix* weights_; 296 bool weights_owner_;296 u_int* ref_count_weights_; 297 297 }; 298 298 -
trunk/c++_tools/classifier/SVM.cc
r635 r640 36 36 { 37 37 #ifndef NDEBUG 38 assert(kernel.columns()==kernel.rows()); 39 assert(kernel.columns()==alpha_.size()); 38 40 for (size_t i=0; i<alpha_.size(); i++) 39 for (size_t j=0; j<alpha_.size(); j++) 41 for (size_t j=0; j<alpha_.size(); j++) 40 42 assert(kernel(i,j)==kernel(j,i)); 41 43 for (size_t i=0; i<alpha_.size(); i++) … … 48 50 << j << std::endl; 49 51 #endif 50 51 52 } 52 53 … … 72 73 const Target& target) const 73 74 { 74 const KernelLookup& kernel = 75 dynamic_cast<const KernelLookup&>(data); 76 77 assert(data.rows()==data.columns()); 78 assert(data.columns()==target.size()); 79 SVM* sc; 80 sc = new SVM(kernel,target); 81 75 SVM* sc=0; 76 try { 77 const KernelLookup& kernel = dynamic_cast<const KernelLookup&>(data); 78 assert(data.rows()==data.columns()); 79 assert(data.columns()==target.size()); 80 sc = new SVM(kernel,target); 82 81 83 82 //Copy those variables possible to modify from outside 84 83 // Peter, in particular C 84 } 85 catch (std::bad_cast) { 86 std::cerr << "Warning: SVM::make_classifier only takes KernelLookup" 87 << std::endl; 88 } 85 89 return sc; 86 90 } … … 89 93 { 90 94 // Peter, should check success of dynamic_cast 91 const KernelLookup input_kernel = dynamic_cast<const KernelLookup&>(input);95 const KernelLookup& input_kernel = dynamic_cast<const KernelLookup&>(input); 92 96 93 97 const KernelLookup* kernel_pointer; -
trunk/c++_tools/classifier/SubsetGenerator.cc
r637 r640 73 73 for (reset(); more(); next()){ 74 74 75 training_target_.push_back(Target(target(),training_index())); 76 validation_target_.push_back(Target(target(),validation_index())); 75 77 // training data with no feature selection 76 78 const MatrixLookup* train_data_all_feat = 77 79 ml->training_data(training_index()); 78 80 // use these data to create feature selection 81 assert(train_data_all_feat); 79 82 f_selector_->update(*train_data_all_feat, training_target()); 80 83 // get features 81 84 features_.push_back(f_selector_->features()); 85 assert(train_data_all_feat); 82 86 delete train_data_all_feat; 83 87 … … 88 92 training_index(), 89 93 validation_index())); 90 91 training_target_.push_back(Target(target(),training_index()));92 validation_target_.push_back(Target(target(),validation_index()));93 94 } 94 95 } … … 100 101 for (reset(); more(); next()){ 101 102 103 training_target_.push_back(Target(target(),training_index())); 104 validation_target_.push_back(Target(target(),validation_index())); 102 105 // training data with no feature selection 103 106 const MatrixLookupWeighted* train_data_all_feat = … … 116 119 validation_index())); 117 120 118 training_target_.push_back(Target(target(),training_index()));119 validation_target_.push_back(Target(target(),validation_index()));120 121 } 121 122 } … … 125 126 if (kernel){ 126 127 for (reset(); more(); next()){ 128 training_target_.push_back(Target(target(),training_index())); 129 validation_target_.push_back(Target(target(),validation_index())); 127 130 const DataLookup2D* matrix = kernel->data(); 128 131 // dynamically allocated must be deleted 129 132 const DataLookup2D* training_matrix = 130 133 matrix->training_data(training_index()); 131 132 134 if (matrix->weighted()){ 133 135 const MatrixLookupWeighted& ml = 134 136 dynamic_cast<const MatrixLookupWeighted&>(*matrix); 135 f_selector_->update(MatrixLookupWeighted(ml,training_index(), 136 false), 137 f_selector_->update(MatrixLookupWeighted(ml,training_index(),false), 137 138 training_target()); 138 139 } … … 143 144 training_target()); 144 145 } 145 146 features_.push_back(f_selector_->features()); 146 std::vector<size_t> dummie=f_selector_->features(); 147 features_.push_back(dummie); 148 //features_.push_back(f_selector_->features()); 149 assert(kernel); 147 150 const KernelLookup* kl = kernel->selected(features_.back()); 148 151 assert(training_matrix); … … 150 153 151 154 // Dynamically allocated. Must be deleted in destructor. 152 training_data_.push_back(kl->training_data(features_.back(), 153 training_index())); 154 validation_data_.push_back(kl->validation_data(features_.back(), 155 training_index(), 155 training_data_.push_back(kl->training_data(training_index())); 156 validation_data_.push_back(kl->validation_data(training_index(), 156 157 validation_index())); 157 158 training_target_.push_back(Target(target(),training_index()));159 validation_target_.push_back(Target(target(),validation_index()));160 158 assert(kl); 161 159 delete kl; -
trunk/test/Makefile.am
r615 r640 32 32 kernel_test kernel_lookup_test matrix_test matrix_lookup_test \ 33 33 ncc_test nni_test pca_test regression_test rnd_test score_test \ 34 statistics_test stl_utility_test svd_test svm_test target_test \ 34 statistics_test stl_utility_test \ 35 subset_generator_test \ 36 svd_test svm_test target_test \ 35 37 utility_test vector_test 36 38 … … 64 66 statistics_test_SOURCES = statistics_test.cc 65 67 stl_utility_test_SOURCES = stl_utility_test.cc 68 subset_generator_test_SOURCES = subset_generator_test.cc 66 69 svd_test_SOURCES = svd_test.cc 67 70 svm_test_SOURCES = svm_test.cc
Note: See TracChangeset
for help on using the changeset viewer.