Changeset 49


Ignore:
Timestamp:
Mar 5, 2004, 11:36:38 AM (18 years ago)
Author:
Peter
Message:

C-parameter added

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/SVM.cc

    r47 r49  
    2626}
    2727
    28 void SVM::train(void) //Peter, this should be done so one can choose to not train on all the samples
     28void SVM::train(const double C) //Peter, this should be done so one can choose to not train on all the samples
    2929{
    3030  using namespace std;
     31 
     32  double D;
     33  if (C>0)
     34    D = 1/C;
     35  else if (C==0)
     36    D=0;
     37  else {
     38    cerr << "SVM): " << "C-parameter cannot be a negative number" << endl;
     39    exit(1);
     40  }
     41
     42  // Modify the diagonal of the kernel matrix
     43  gslapi::matrix kernel_modified = kernel_;
     44  for (unsigned int i=0; i<kernel_.columns(); i++)
     45    kernel_modified(i,i) += D;
     46
    3147  gslapi::vector E = gslapi::vector(-target_);
    32  
    33   double upper_bound = pow(10., 32);
     48 
     49  double upper_bound = pow(10., 32.); //Peter, should be static or is there need for an upperbound?
    3450  u_int count = 0;
    3551  double alpha_new;
     
    4359    count++; 
    4460    dalpha = alpha_.mul_elements(target_);
    45     E = kernel_*dalpha-target_; //Peter, could be done faster!!
     61    E = kernel_modified*dalpha-target_; //Peter, could be done faster!!
    4662   
    4763    // Choosing a pair of variables to modify
     
    4965    index = choose(target_.size());
    5066   
    51     cout << count << endl;
    5267    //Updating the two variables
    5368    if (target_[index.first]!=target_[index.second]) {       
    5469      if (alpha_[index.second] > alpha_[index.first]) {
    55   v = upper_bound;
    56   u = alpha_[index.second] - alpha_[index.first];
     70        v = upper_bound;
     71        u = alpha_[index.second] - alpha_[index.first];
    5772      }
    5873      else {
    59   v = upper_bound - alpha_[index.first] + alpha_[index.second];
    60   u = 0;
     74        v = upper_bound - alpha_[index.first] + alpha_[index.second];
     75        u = 0;
    6176      }
    6277    }
    6378    else {       
    6479      if (alpha_[index.second] + alpha_[index.first] > upper_bound) {
    65   u = alpha_[index.second] + alpha_[index.first] - upper_bound;
    66   v = upper_bound;   
     80        u = alpha_[index.second] + alpha_[index.first] - upper_bound;
     81        v = upper_bound;   
    6782      }
    6883      else {
    69   u = 0;
    70   v = alpha_[index.first] + alpha_[index.second];
     84        u = 0;
     85        v = alpha_[index.first] + alpha_[index.second];
    7186      }
    7287    }
    7388   
    74     double k = ( kernel_(index.first, index.first) +
    75      kernel_(index.second, index.second) -
    76      2*kernel_(index.first, index.second) );
     89    double k = ( kernel_modified(index.first, index.first) +
     90                 kernel_modified(index.second, index.second) -
     91                 2*kernel_modified(index.first, index.second) );
    7792    alpha_new = (alpha_[index.second] + target_[index.second]*
    78     (E[index.first]-E[index.second])/k) ;
     93                (E[index.first]-E[index.second])/k) ;
    7994    if (alpha_new > v)
    80   alpha_new = v;
     95      alpha_new = v;
    8196    else if (alpha_new<u)
    82   alpha_new = u;
     97      alpha_new = u;
    8398   
    8499    alpha_[index.first] += (target_[index.first]*target_[index.second] *
    85100          (alpha_[index.second]-alpha_new));
    86101    alpha_[index.second]=alpha_new;
    87     stop_condition = stop( target_, kernel_, alpha_);
     102    stop_condition = stop( target_, kernel_modified, alpha_);
    88103    if (count>10000000){
    89104      cerr << "SVM): " << "more than 10,000,000 epochs reached" << endl;
     
    110125}
    111126
    112 //(Peter, do it more clever)
     127//(Peter, do it the Cristianini way)
    113128std::pair<u_int,u_int> SVM::choose(u_int nof_samples)
    114129{
     
    122137
    123138bool SVM::stop( const gslapi::vector& target_,
    124     const gslapi::matrix& kernel_,
     139    const gslapi::matrix& kernel,
    125140    const gslapi::vector& alpha_)
    126141{
     
    128143    double max_output_negative = -10000;
    129144    double epsilon = 0.000001; // Peter, should be static const
    130     gslapi::vector output_unbiased = kernel_ * alpha_.mul_elements(target_);
     145    gslapi::vector output_unbiased = kernel * alpha_.mul_elements(target_);
    131146    for (u_int i=0; i<target_.size(); i++) {
    132147      if (target_[i]==1) {
  • trunk/src/SVM.h

    r47 r49  
    3636           
    3737    ///
    38     /// Training the SVM using the SMO algorithm
     38    /// Training the SVM using the SMO algorithm. Input is the C-parameter,
     39    /// which is converted to \f$D=1/C\f$, since \f$1/C\f$ is what is used in
     40    /// the algorithm. Default should correspond to maximal margin
     41    /// (\f$C=\inf\f$), but since I don't trust double inf I use default
     42    /// \f$C=0\f$ and turn that into \f$D=0\f$.
    3943    ///
    40     void train();
     44    void train(const double = 0);
    4145
    4246    ///
     
    7478    ///
    7579    bool stop(const gslapi::vector& target_,
    76         const gslapi::matrix& kernel_,
     80        const gslapi::matrix& kernel,
    7781        const gslapi::vector& alpha_);
    7882  };
Note: See TracChangeset for help on using the changeset viewer.