Changeset 660 for trunk


Ignore:
Timestamp:
Sep 27, 2006, 10:53:56 AM (17 years ago)
Author:
Peter
Message:

closes #140 moved Index class to its own file and changed name to SVindex

Location:
trunk/c++_tools/classifier
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/c++_tools/classifier/Makefile.am

    r642 r660  
    4848  SubsetGenerator.cc \
    4949  SupervisedClassifier.cc \
     50  SVindex.cc \
    5051  SVM.cc \
    5152  Target.cc \
     
    8081  SubsetGenerator.h \
    8182  SupervisedClassifier.h \
     83  SVindex.h \
    8284  SVM.h \
    8385  Target.h \
  • trunk/c++_tools/classifier/SVM.cc

    r659 r660  
    352352  }
    353353
    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 nothing
    386     if (index_first_<nof_sv())
    387       return;
    388 
    389     // swap elements
    390     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 nothing
    404     if (index_second_<nof_sv())
    405       return;
    406 
    407     // swap elements
    408     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 nothing
    422     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 nothing
    437     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   }
    469354
    470355}} // of namespace classifier and namespace theplu
  • trunk/c++_tools/classifier/SVM.h

    r648 r660  
    66#include <c++_tools/classifier/KernelLookup.h>
    77#include <c++_tools/classifier/SupervisedClassifier.h>
     8#include <c++_tools/classifier/SVindex.h>
    89#include <c++_tools/classifier/Target.h>
    910#include <c++_tools/utility/vector.h>
    1011
    11 #include <cassert>
    1212#include <utility>
    1313#include <vector>
     
    1818
    1919  class DataLookup2D;
    20 #ifndef DOXYGEN_SHOULD_SKIP_THIS
    21   /// @internal Class keeping track of which samples are support vectors and
    22   /// not. The first nof_sv elements in the vector are indices of the
    23   /// support vectors
    24   ///
    25   class Index
    26   {
    27 
    28   public:
    29     //Default Contructor
    30     Index();
    31 
    32     //
    33     Index(const size_t);
    34 
    35     // @return index_first
    36     inline size_t index_first(void) const
    37     { assert(index_first_<size()); return index_first_; }
    38 
    39     // @return index_second
    40     inline size_t index_second(void) const
    41     { assert(index_second_<size()); return index_second_; }
    42 
    43     // synch the object against alpha
    44     void init(const utility::vector& alpha, const double);
    45 
    46     // @return nof samples
    47     inline size_t size(void) const { return vec_.size(); }
    48 
    49     // @return nof support vectors
    50     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 to
    59     // 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_first
    75     inline size_t value_first(void) const
    76     { assert(value_first_<size()); return value_first_; }
    77 
    78     // @return const ref value_second
    79     inline size_t value_second(void) const
    80     { 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 access
    91     size_t value_second_; // vec_[index_second_] exists for fast access
    92    
    93   };
    94 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
    95 
    9620  ///
    9721  /// @brief Support Vector Machine
     
    266190    utility::vector output_;
    267191    bool owner_;
    268     Index sample_;
     192    SVindex sample_;
    269193    bool trained_;
    270194    double tolerance_;
Note: See TracChangeset for help on using the changeset viewer.