Changeset 1206
- Timestamp:
- Mar 5, 2008, 6:56:01 PM (16 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/ensemble_test.cc
r1161 r1206 32 32 #include "yat/classifier/Kernel_MEV.h" 33 33 #include "yat/classifier/MatrixLookup.h" 34 #include "yat/classifier/MatrixLookupWeighted.h" 34 35 #include "yat/classifier/NCC.h" 35 36 #include "yat/classifier/PolynomialKernelFunction.h" … … 87 88 *error << "build ensemble" << std::endl; 88 89 ensemble.build(); 90 std::vector<std::vector<statistics::Averager> > result; 91 ensemble.predict(data, result); 92 } 93 94 { 95 *error << "create ensemble of ncc" << std::endl; 96 classifier::MatrixLookupWeighted data_weighted(data); 97 classifier::NCC<statistics::EuclideanDistance> ncc; 98 classifier::CrossValidationSampler sampler(target,3,3); 99 classifier::SubsetGenerator<classifier::MatrixLookupWeighted> 100 subdata(sampler,data_weighted); 101 classifier::EnsembleBuilder<classifier::SupervisedClassifier, 102 classifier::MatrixLookupWeighted> ensemble(ncc, data_weighted, sampler); 103 *error << "build ensemble" << std::endl; 104 ensemble.build(); 105 std::vector<std::vector<statistics::Averager> > result; 106 ensemble.predict(data_weighted, result); 89 107 } 90 108 … … 103 121 *error << "build ensemble" << std::endl; 104 122 ensemble.build(); 123 std::vector<std::vector<statistics::Averager> > result; 124 ensemble.predict(kernel_lookup, result); 105 125 106 126 utility::Vector out(target.size(),0); -
trunk/yat/classifier/EnsembleBuilder.h
r1157 r1206 115 115 SubsetGenerator<Data>* subset_; 116 116 std::vector<Classifier*> classifier_; 117 KernelLookup test_data(const KernelLookup&, size_t k); 118 MatrixLookup test_data(const MatrixLookup&, size_t k); 119 MatrixLookupWeighted test_data(const MatrixLookupWeighted&, size_t k); 117 120 std::vector<std::vector<statistics::Averager> > validation_result_; 118 121 … … 169 172 170 173 template <class C, class D> 171 u_long EnsembleBuilder<C, D>::size(void) const172 {173 return classifier_.size();174 }175 176 177 template <class C, class D>178 174 void EnsembleBuilder<C, D>::predict 179 175 (const D& data, std::vector<std::vector<statistics::Averager> >& result) … … 187 183 188 184 for(u_long k=0;k<subset_->size();++k) { 189 const D* sub_data = 190 data.selected(subset_->training_features(k)); 191 assert(sub_data); 192 classifier(k).predict(*sub_data,prediction); 193 delete sub_data; 185 D sub_data = test_data(data, k); 186 classifier(k).predict(sub_data,prediction); 194 187 } 195 188 … … 200 193 201 194 195 template <class C, class D> 196 u_long EnsembleBuilder<C, D>::size(void) const 197 { 198 return classifier_.size(); 199 } 200 201 202 template <class C, class D> 203 MatrixLookup EnsembleBuilder<C, D>::test_data(const MatrixLookup& data, 204 size_t k) 205 { 206 return MatrixLookup(data, subset_->training_features(k), true); 207 } 208 209 210 template <class C, class D> 211 MatrixLookupWeighted 212 EnsembleBuilder<C, D>::test_data(const MatrixLookupWeighted& data, size_t k) 213 { 214 return MatrixLookupWeighted(data, subset_->training_features(k), true); 215 } 216 217 218 template <class C, class D> 219 KernelLookup 220 EnsembleBuilder<C, D>::test_data(const KernelLookup& kernel, size_t k) 221 { 222 // weighted case 223 if (kernel.weighted()){ 224 assert(false); 225 // no feature selection 226 if (kernel.data_weighted().rows()==subset_->training_features(k).size()) 227 return KernelLookup(kernel, subset_->training_index(k), true); 228 MatrixLookupWeighted mlw = test_data(kernel.data_weighted(), k); 229 return subset_->training_data(k).test_kernel(mlw); 230 231 } 232 // unweighted case 233 234 // no feature selection 235 if (kernel.data().rows()==subset_->training_features(k).size()) 236 return KernelLookup(kernel, subset_->training_index(k), true); 237 238 // feature selection 239 return subset_->training_data(k).test_kernel(test_data(kernel.data(),k)); 240 } 241 242 202 243 template <class C, class D> 203 244 const std::vector<std::vector<statistics::Averager> >& -
trunk/yat/classifier/KernelLookup.cc
r1201 r1206 131 131 132 132 133 utility::SmartPtr<const MatrixLookup> KernelLookup::data(void) const 134 { 135 return utility::SmartPtr<const MatrixLookup> 136 (new MatrixLookup(kernel_->data(), column_index_, false)); 137 } 138 139 140 utility::SmartPtr<const MatrixLookupWeighted> 141 KernelLookup::data_weighted(void) const 142 { 143 return utility::SmartPtr<const MatrixLookupWeighted> 144 (new MatrixLookupWeighted(kernel_->data_weighted(),column_index_,false)); 133 MatrixLookup KernelLookup::data(void) const 134 { 135 assert(!weighted()); 136 return MatrixLookup(kernel_->data(), column_index_, false); 137 } 138 139 140 MatrixLookupWeighted KernelLookup::data_weighted(void) const 141 { 142 assert(weighted()); 143 return MatrixLookupWeighted(kernel_->data_weighted(),column_index_,false); 145 144 } 146 145 … … 184 183 185 184 186 utility::SmartPtr<const KernelLookup> 187 KernelLookup::selected(const utility::Index& inputs) const 185 KernelLookup KernelLookup::selected(const utility::Index& inputs) const 188 186 { 189 187 const Kernel* kernel; 190 188 if (kernel_->weighted()){ 191 utility::SmartPtr<const MatrixLookupWeighted> ml = data_weighted();192 189 const MatrixLookupWeighted* ms = 193 new MatrixLookupWeighted( *ml,inputs,true);194 kernel = kernel_->make_kernel(*ms, false);190 new MatrixLookupWeighted(data_weighted(),inputs,true); 191 kernel = kernel_->make_kernel(*ms, true); 195 192 } 196 193 else { 197 utility::SmartPtr<const MatrixLookup> m = data();198 194 // matrix with selected features 199 const MatrixLookup* ms = new MatrixLookup( *m,inputs,true);195 const MatrixLookup* ms = new MatrixLookup(data(),inputs,true); 200 196 kernel = kernel_->make_kernel(*ms,true); 201 197 } 202 return utility::SmartPtr<const KernelLookup> 203 (new KernelLookup(*kernel, true)); 204 } 205 206 207 utility::SmartPtr<const KernelLookup> 208 KernelLookup::test_kernel(const MatrixLookup& data) const 198 return KernelLookup(*kernel, true); 199 } 200 201 202 KernelLookup KernelLookup::test_kernel(const MatrixLookup& data) const 209 203 { 210 204 if (!weighted()){ … … 240 234 kernel_->make_kernel(*tmp, true); 241 235 242 return utility::SmartPtr<const KernelLookup> 243 (new KernelLookup(*kernel, utility::Index(row_index), 244 utility::Index(column_index), true)); 236 return KernelLookup(*kernel, utility::Index(row_index), 237 utility::Index(column_index), true); 245 238 } 246 239 … … 282 275 283 276 284 return utility::SmartPtr<const KernelLookup> 285 (new KernelLookup(*kernel, row_index_, 286 utility::Index(column_index), true)); 287 } 288 289 290 291 utility::SmartPtr<const KernelLookup> 292 KernelLookup::test_kernel(const MatrixLookupWeighted& data) const 277 return KernelLookup(*kernel, row_index_, 278 utility::Index(column_index), true); 279 } 280 281 282 283 KernelLookup KernelLookup::test_kernel(const MatrixLookupWeighted& data) const 293 284 { 294 285 utility::Matrix* data_all = … … 331 322 const Kernel* kernel = 332 323 kernel_->make_kernel(MatrixLookupWeighted(*data_all, *weight_all, true)); 333 return utility::SmartPtr<const KernelLookup> 334 (new KernelLookup(*kernel, row_index_, 335 utility::Index(column_index), true)); 324 return KernelLookup(*kernel, row_index_, 325 utility::Index(column_index), true); 336 326 } 337 327 -
trunk/yat/classifier/KernelLookup.h
r1201 r1206 49 49 /// not contain any data or values, but rather is a lookup into a 50 50 /// Kernel object. Each row and each column corresponds to a row and 51 /// a column in the Kernel, respectively. This design allow for fast51 /// a column in the Kernel, respectively. This design allows for fast 52 52 /// creation of sub-kernels, which is a common operation in most 53 53 /// traning/validation procedures. 54 54 /// 55 /// A KernelLookup can be created directly from a Kernel or from an56 /// other KernelLookup. In the latter case, the resulting55 /// A KernelLookup can be created directly from a Kernel or from 56 /// another KernelLookup. In the latter case, the resulting 57 57 /// KernelLookup is looking directly into the underlying Kernel to 58 58 /// avoid multiple lookups. … … 154 154 155 155 /// 156 /// Constructor taking the column (default) or row index vectoras156 /// Constructor taking the column (default) or row index as 157 157 /// input. If @a row is false the created KernelLookup will have 158 158 /// equally many rows as @a kernel. … … 165 165 /// undefined. 166 166 /// 167 KernelLookup(const KernelLookup& k ernel, const utility::Index&,167 KernelLookup(const KernelLookup& kl, const utility::Index&, 168 168 const bool row=false); 169 169 … … 211 211 /// \throw if KernelLookup is weighted 212 212 /// 213 utility::SmartPtr<const MatrixLookup>data(void) const;213 MatrixLookup data(void) const; 214 214 215 215 /// … … 221 221 /// \throw if KernelLookup is unweighted 222 222 /// 223 utility::SmartPtr<const MatrixLookupWeighted>data_weighted(void) const;223 MatrixLookupWeighted data_weighted(void) const; 224 224 225 225 /** … … 268 268 KernelLookup. 269 269 */ 270 utility::SmartPtr<const KernelLookup> 271 selected(const utility::Index& index) const; 270 KernelLookup selected(const utility::Index& index) const; 272 271 273 272 /** … … 281 280 this was built from. 282 281 */ 283 utility::SmartPtr<const KernelLookup> 284 test_kernel(const MatrixLookup& data) const; 282 KernelLookup test_kernel(const MatrixLookup& data) const; 285 283 286 284 /** … … 293 291 between the passed @a data and the internal underlying data @a 294 292 this was built from. 295 296 @note Returns a dynamically allocated KernelLookup, which has 297 to be deleted by the caller to avoid memory leaks. 298 */ 299 utility::SmartPtr<const KernelLookup> 300 test_kernel(const MatrixLookupWeighted& data) const; 293 */ 294 KernelLookup test_kernel(const MatrixLookupWeighted& data) const; 301 295 302 296 /** -
trunk/yat/classifier/SubsetGenerator.h
r1201 r1206 202 202 void SubsetGenerator<T>::build(const MatrixLookup& ml) 203 203 { 204 if (!f_selector_)// no feature selection 205 features_.push_back(utility::Index(ml.rows())); 206 204 207 for (size_t k=0; k<size(); k++){ 205 208 training_target_.push_back(Target(target(),training_index(k))); … … 217 220 delete train_data_all_feat; 218 221 } 219 else // no feature selection220 features_.push_back(utility::Index(ml.rows()));221 222 222 223 223 // Dynamically allocated. Must be deleted in destructor. … … 234 234 void SubsetGenerator<T>::build(const MatrixLookupWeighted& ml) 235 235 { 236 if (!f_selector_)// no feature selection 237 features_.push_back(utility::Index(ml.rows())); 238 236 239 for (u_long k=0; k<size(); k++){ 237 240 training_target_.push_back(Target(target(),training_index(k))); … … 247 250 delete train_data_all_feat; 248 251 } 249 else // no feature selection250 features_.push_back(utility::Index(ml.rows()));251 252 252 253 … … 268 269 if (f_selector_){ 269 270 if (kernel.weighted()){ 270 utility::SmartPtr<const MatrixLookupWeighted> ml= 271 kernel.data_weighted(); 272 f_selector_->update(MatrixLookupWeighted(*ml,training_index(k),false), 271 MatrixLookupWeighted ml = kernel.data_weighted(); 272 f_selector_->update(MatrixLookupWeighted(ml,training_index(k),false), 273 273 training_target(k)); 274 274 } 275 275 else { 276 utility::SmartPtr<const MatrixLookup>ml=kernel.data();277 f_selector_->update(MatrixLookup( *ml,training_index(k), false),276 MatrixLookup ml=kernel.data(); 277 f_selector_->update(MatrixLookup(ml,training_index(k), false), 278 278 training_target(k)); 279 279 } 280 280 features_.push_back(f_selector_->features()); 281 utility::SmartPtr<const KernelLookup> kl = 282 kernel.selected(features_.back()); 281 KernelLookup kl = kernel.selected(features_.back()); 283 282 // Dynamically allocated. Must be deleted in destructor. 284 training_data_.push_back(new KernelLookup( *kl,training_index(k),283 training_data_.push_back(new KernelLookup(kl,training_index(k), 285 284 training_index(k))); 286 validation_data_.push_back(new KernelLookup( *kl, training_index(k),285 validation_data_.push_back(new KernelLookup(kl, training_index(k), 287 286 validation_index(k))); 288 287 } … … 296 295 297 296 } 297 if (!f_selector_){ 298 if (kernel.weighted()) 299 features_.push_back(utility::Index(kernel.data_weighted().rows())); 300 else 301 features_.push_back(utility::Index(kernel.data().rows())); 302 } 298 303 } 299 304 … … 325 330 SubsetGenerator<T>::training_features(size_t i) const 326 331 { 332 utility::yat_assert<std::runtime_error>(features_.size(), 333 "SubsetGenerator::training_features"); 327 334 return f_selector_ ? features_[i] : features_[0]; 328 335 }
Note: See TracChangeset
for help on using the changeset viewer.