- Timestamp:
- Sep 27, 2006, 10:53:56 AM (17 years ago)
- Location:
- trunk/c++_tools/classifier
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/c++_tools/classifier/Makefile.am
r642 r660 48 48 SubsetGenerator.cc \ 49 49 SupervisedClassifier.cc \ 50 SVindex.cc \ 50 51 SVM.cc \ 51 52 Target.cc \ … … 80 81 SubsetGenerator.h \ 81 82 SupervisedClassifier.h \ 83 SVindex.h \ 82 84 SVM.h \ 83 85 Target.h \ -
trunk/c++_tools/classifier/SVM.cc
r659 r660 352 352 } 353 353 354 Index::Index(void)355 : nof_sv_(0), vec_(std::vector<size_t>(0))356 {357 }358 359 Index::Index(const size_t n)360 : nof_sv_(0), vec_(std::vector<size_t>(n))361 {362 for (size_t i=0; i<vec_.size(); i++)363 vec_[i]=i;364 }365 366 void Index::init(const utility::vector& alpha, const double tol)367 {368 nof_sv_=0;369 size_t nof_nsv=0;370 for (size_t i=0; i<alpha.size(); i++)371 if (alpha(i)<tol){372 nof_nsv++;373 vec_[vec_.size()-nof_nsv]=i;374 }375 else{376 vec_[nof_sv_]=i;377 nof_sv_++;378 }379 assert(nof_sv_+nof_nsv==vec_.size());380 381 }382 383 void Index::sv_first(void)384 {385 // if already sv, do nothing386 if (index_first_<nof_sv())387 return;388 389 // swap elements390 if(index_second_==nof_sv_){391 index_second_=index_first_;392 }393 vec_[index_first_]=vec_[nof_sv_];394 vec_[nof_sv_]=value_first_;395 index_first_ = nof_sv_;396 397 nof_sv_++;398 399 }400 401 void Index::sv_second(void)402 {403 // if already sv, do nothing404 if (index_second_<nof_sv())405 return;406 407 // swap elements408 if(index_first_==nof_sv_){409 index_first_=index_second_;410 }411 412 vec_[index_second_]=vec_[nof_sv_];413 vec_[nof_sv_]=value_second_;414 index_second_=nof_sv_;415 416 nof_sv_++;417 }418 419 void Index::nsv_first(void)420 {421 // if already nsv, do nothing422 if ( !(index_first_<nof_sv()) )423 return;424 425 if(index_second_==nof_sv_-1)426 index_second_=index_first_;427 vec_[index_first_]=vec_[nof_sv_-1];428 vec_[nof_sv_-1]=value_first_;429 index_first_=nof_sv_-1;430 431 nof_sv_--;432 }433 434 void Index::nsv_second(void)435 {436 // if already nsv, do nothing437 if ( !(index_second_<nof_sv()) )438 return;439 440 if(index_first_==nof_sv_-1)441 index_first_=index_second_;442 vec_[index_second_]=vec_[nof_sv_-1];443 vec_[nof_sv_-1]=value_second_;444 index_second_ = nof_sv_-1;445 446 nof_sv_--;447 }448 449 450 void Index::shuffle(void)451 {452 random::DiscreteUniform a;453 random_shuffle(vec_.begin()+nof_sv_, vec_.end(), a);454 }455 456 void Index::update_first(const size_t i)457 {458 assert(i<size());459 index_first_=i;460 value_first_=vec_[i];461 }462 463 void Index::update_second(const size_t i)464 {465 assert(i<size());466 index_second_=i;467 value_second_=vec_[i];468 }469 354 470 355 }} // of namespace classifier and namespace theplu -
trunk/c++_tools/classifier/SVM.h
r648 r660 6 6 #include <c++_tools/classifier/KernelLookup.h> 7 7 #include <c++_tools/classifier/SupervisedClassifier.h> 8 #include <c++_tools/classifier/SVindex.h> 8 9 #include <c++_tools/classifier/Target.h> 9 10 #include <c++_tools/utility/vector.h> 10 11 11 #include <cassert>12 12 #include <utility> 13 13 #include <vector> … … 18 18 19 19 class DataLookup2D; 20 #ifndef DOXYGEN_SHOULD_SKIP_THIS21 /// @internal Class keeping track of which samples are support vectors and22 /// not. The first nof_sv elements in the vector are indices of the23 /// support vectors24 ///25 class Index26 {27 28 public:29 //Default Contructor30 Index();31 32 //33 Index(const size_t);34 35 // @return index_first36 inline size_t index_first(void) const37 { assert(index_first_<size()); return index_first_; }38 39 // @return index_second40 inline size_t index_second(void) const41 { assert(index_second_<size()); return index_second_; }42 43 // synch the object against alpha44 void init(const utility::vector& alpha, const double);45 46 // @return nof samples47 inline size_t size(void) const { return vec_.size(); }48 49 // @return nof support vectors50 inline size_t nof_sv(void) const { return nof_sv_; }51 52 // making first to an nsv. If already sv, nothing happens.53 void nsv_first(void);54 55 // making second to an nsv. If already sv, nothing happens.56 void nsv_second(void);57 58 // randomizes the nsv part of vector and sets index_first to59 // nof_sv_ (the first nsv)60 void shuffle(void);61 62 // making first to a sv. If already sv, nothing happens.63 void sv_first(void);64 65 // making second to a sv. If already sv, nothing happens.66 void sv_second(void);67 68 //69 void update_first(const size_t);70 71 //72 void update_second(const size_t);73 74 // @return value_first75 inline size_t value_first(void) const76 { assert(value_first_<size()); return value_first_; }77 78 // @return const ref value_second79 inline size_t value_second(void) const80 { assert(value_first_<size()); return value_second_; }81 82 inline size_t operator()(size_t i) const {83 assert(i<size()); assert(vec_[i]<size()); return vec_[i]; }84 85 private:86 size_t index_first_;87 size_t index_second_;88 size_t nof_sv_;89 std::vector<size_t> vec_;90 size_t value_first_; // vec_[index_first_] exists for fast access91 size_t value_second_; // vec_[index_second_] exists for fast access92 93 };94 #endif /* DOXYGEN_SHOULD_SKIP_THIS */95 96 20 /// 97 21 /// @brief Support Vector Machine … … 266 190 utility::vector output_; 267 191 bool owner_; 268 Index sample_;192 SVindex sample_; 269 193 bool trained_; 270 194 double tolerance_;
Note: See TracChangeset
for help on using the changeset viewer.