Changeset 162


Ignore:
Timestamp:
Sep 21, 2004, 3:49:27 PM (17 years ago)
Author:
Peter
Message:

choice of maximal epochs added in SVM

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/InputRanker.cc

    r154 r162  
    8585    sort(score.begin(), score.end(),
    8686         pair_value_compare<size_t, double>());
    87    
     87
    8888    for (size_t i=0; i<nof_genes; i++){
    8989      id_[i]=score[nof_genes-i-1].first;
  • trunk/src/SVM.cc

    r115 r162  
    11// $Id$
     2
     3#include "SVM.h"
    24
    35// System includes
    46#include <cmath>
     7#include <limits.h>
    58#include <utility>
    69#include <vector>
     
    1013#include "matrix.h"
    1114#include "random_singleton.h"
    12 #include "SVM.h"
    1315#include "vector.h"
    1416
     
    2426    c_(0),
    2527    kernel_(kernel),
     28    max_epochs_(10000000),
    2629    target_(target),
    2730    trained_(false),
     
    3033       
    3134  {
     35    if (max_epochs_>ULONG_MAX)
     36      max_epochs_=ULONG_MAX;
    3237    if (!train_set_.size())
    3338      for (size_t i=0; i<target_.size(); i++)
     
    9095  }
    9196 
    92   void SVM::train()
     97  bool SVM::train()
    9398  {
    94   using namespace std;
    95 
    96   // initializing variables for optimization
    97   gslapi::matrix kernel_train(train_set_.size(),train_set_.size());
    98   gslapi::vector alpha_train(train_set_.size());
    99   gslapi::vector target_train(train_set_.size());
    100   for (unsigned int i=0; i<train_set_.size(); i++) {
    101     alpha_train(i) = alpha_(train_set_[i]);
    102     target_train(i) = target_(train_set_[i]);
    103     for (unsigned int j=0; j<train_set_.size(); j++)
    104       kernel_train(i,j) = kernel_(train_set_[i],train_set_[j]);
    105   }
    106 
    107   // Modify the diagonal of the kernel matrix (soft margin)
    108   double d;
    109   if (c_>0)
    110     d = 1/c_;
    111   else if (c_==0)
    112     d = 0;
    113   else {
    114     cerr << "SVM): " << "C-parameter cannot be a negative number" << endl;
    115     exit(1);
    116   }
    117   for (unsigned int i=0; i<kernel_train.columns(); i++)
    118     kernel_train(i,i) += d;
     99    using namespace std;
     100
     101    // initializing variables for optimization
     102    gslapi::matrix kernel_train(train_set_.size(),train_set_.size());
     103    gslapi::vector alpha_train(train_set_.size());
     104    gslapi::vector target_train(train_set_.size());
     105    for (unsigned int i=0; i<train_set_.size(); i++) {
     106      alpha_train(i) = alpha_(train_set_[i]);
     107      target_train(i) = target_(train_set_[i]);
     108      for (unsigned int j=0; j<train_set_.size(); j++)
     109        kernel_train(i,j) = kernel_(train_set_[i],train_set_[j]);
     110    }
     111
     112    // Modify the diagonal of the kernel matrix (soft margin)
     113    if (c_>0){
     114      double d = 1/c_;
     115      for (unsigned int i=0; i<kernel_train.columns(); i++)
     116        kernel_train(i,i) += d;
     117    }
    119118 
    120   gslapi::vector  E = ( kernel_train*target_train.mul_elements(alpha_train)
    121                         - target_train ) ;
    122 
    123   double upper_bound = pow(10., 32.); //Peter, should be static...
    124   u_int epochs = 0;
    125   double alpha_new2;
    126   double alpha_new1;
    127   double u;
    128   double v;
    129   bool stop_condition = false;
    130 
    131   // Training loop
    132   while(!stop_condition) {
    133     pair<size_t, size_t> index = choose(E, target_train,
     119    gslapi::vector  E = ( kernel_train*target_train.mul_elements(alpha_train)
     120                          - target_train ) ;
     121
     122    double upper_bound = pow(10., 32); //Peter, should be static...
     123    unsigned long int epochs = 0;
     124    double alpha_new2;
     125    double alpha_new1;
     126    double u;
     127    double v;
     128    bool stop_condition = false;
     129    std::cout << "target:\n" << target_train << "\n";
     130    std::cout << "kernel:\n" << kernel_train << "\n";
     131    // Training loop
     132    while(!stop_condition) {
     133      pair<size_t, size_t> index = choose(E, target_train,
    134134                                        alpha_train,stop_condition);
    135     if (target_train[index.first]!=target_train[index.second]) {       
    136       if (alpha_train[index.second] > alpha_train[index.first]) {
    137         v = upper_bound;
    138         u = alpha_train[index.second] - alpha_train[index.first];
    139       }
    140       else {
    141         v = upper_bound - alpha_train[index.first] + alpha_train[index.second];
    142         u = 0;
    143       }
    144     }
    145     else {       
    146       if (alpha_train[index.second] + alpha_train[index.first] > upper_bound) {
    147         u = alpha_train[index.second] + alpha_train[index.first] - upper_bound;
    148         v = upper_bound;   
    149       }
    150       else {
    151         u = 0;
    152         v = alpha_train[index.first] + alpha_train[index.second];
    153       }
    154     }
    155    
    156     double k = ( kernel_train(index.first, index.first) +
    157                  kernel_train(index.second, index.second) -
    158                  2*kernel_train(index.first, index.second));
    159     alpha_new2 = (alpha_train[index.second] + target_train[index.second]*
    160                  (E[index.first]-E[index.second])/k );
    161    
    162     if (alpha_new2 > v)
    163       alpha_new2 = v;
    164     else if (alpha_new2<u)
    165       alpha_new2 = u;
     135      // Defining upper and lower bounds for alpha
     136      if (target_train[index.first]!=target_train[index.second]) {       
     137        if (alpha_train[index.second] > alpha_train[index.first]) {
     138          v = upper_bound;
     139          u = alpha_train[index.second] - alpha_train[index.first];
     140        }
     141        else {
     142          v = (upper_bound - alpha_train[index.first] +
     143               alpha_train[index.second]);
     144          u = 0;
     145        }
     146      }
     147      else {       
     148        if (alpha_train[index.second] + alpha_train[index.first] >
     149            upper_bound) {
     150          u = (alpha_train[index.second] + alpha_train[index.first] -
     151               upper_bound);
     152          v = upper_bound;   
     153        }
     154        else {
     155          u = 0;
     156          v = alpha_train[index.first] + alpha_train[index.second];
     157        }
     158      }
     159       
     160      double k = ( kernel_train(index.first, index.first) +
     161                   kernel_train(index.second, index.second) -
     162                   2*kernel_train(index.first, index.second));
     163 
     164      alpha_new2 = (alpha_train[index.second] + target_train[index.second]*
     165                    (E[index.first]-E[index.second])/k );
     166   
     167      if (alpha_new2 > v)
     168        alpha_new2 = v;
     169      else if (alpha_new2<u)
     170        alpha_new2 = u;
    166171   
    167     // Updating the alphas and some more
    168     if (alpha_new2 < tolerance_)
    169       alpha_new2=0;
    170    
    171     alpha_new1 = (alpha_train[index.first] +
    172                   (target_train[index.first] * target_train[index.second] *
    173                   (alpha_train[index.second] - alpha_new2)) );
    174     if (alpha_new1 < tolerance_){
    175       alpha_new1=0;
    176     }
    177 
    178     alpha_train[index.first] = alpha_new1;
    179     alpha_train[index.second] = alpha_new2;
    180     E = kernel_train*target_train.mul_elements(alpha_train) - target_train;
    181     gslapi::vector ones(alpha_train.size(),1);
    182    
    183     epochs++; 
    184     if (epochs>1000000){
    185       cerr << "WARNING: SVM: " << "1,000,000 epochs reached" << endl;
    186       exit(1);
    187     }
    188   }
    189 
    190   // Calculating the bias from support vectors. E = output - bias - target
    191   Averager bias;
    192   for (size_t i=0; i<E.size(); i++)
    193     if (alpha_train(i))
    194       bias.add(-E(i));
    195   bias_ = bias.mean();
     172      // Updating the alphas and some more
     173      if (alpha_new2 < tolerance_)
     174        alpha_new2=0;
     175   
     176      alpha_new1 = (alpha_train[index.first] +
     177                    (target_train[index.first] * target_train[index.second] *
     178                    (alpha_train[index.second] - alpha_new2)) );
     179      if (alpha_new1 < tolerance_){
     180        alpha_new1=0;
     181      }
     182
     183      alpha_train[index.first] = alpha_new1;
     184      alpha_train[index.second] = alpha_new2;
     185   
     186      E = kernel_train*target_train.mul_elements(alpha_train) - target_train;
     187      gslapi::vector ones(alpha_train.size(),1);
     188   
     189      epochs++; 
     190      if (epochs>max_epochs_){
     191        return false;
     192      }
     193    }
     194
     195    // Calculating the bias from support vectors. E = output - bias - target
     196    Averager bias;
     197    for (size_t i=0; i<E.size(); i++)
     198      if (alpha_train(i))
     199        bias.add(-E(i));
     200    bias_ = bias.mean();
    196201 
    197   trained_= true;
    198   alpha_.set_all(0);
    199   for (unsigned int i=0; i<train_set_.size(); i++)
    200     alpha_(train_set_[i]) = alpha_train(i);
     202    trained_= true;
     203    alpha_.set_all(0);
     204    for (unsigned int i=0; i<train_set_.size(); i++)
     205      alpha_(train_set_[i]) = alpha_train(i);
     206   
     207    // training performed succesfully
     208    return true;
    201209   
    202210  }
  • trunk/src/SVM.h

    r114 r162  
    1313#include <utility>
    1414#include <vector>
     15
    1516
    1617namespace theplu {
     
    4849    /// Function returns the output from SVM
    4950    ///
    50     inline gslapi::vector get_output(void) const { return (kernel_ * alpha_.mul_elements(target_) + gslapi::vector(target_.size(),bias_) );}
     51    inline gslapi::vector get_output(void) const
     52    { return (kernel_ * alpha_.mul_elements(target_) +
     53              gslapi::vector(target_.size(),bias_) );}
    5154
    5255    ///
     56    /// @return number of maximal epochs
     57    ///
     58    inline long int max_epochs(void) const {return max_epochs_;}
     59   
     60    ///
     61    /// Changing number of maximal epochs
     62    ///
     63    inline void max_epochs(const unsigned long int d) {max_epochs_=d;}
     64   
     65    ///
    5366    /// Changing the C-parameter
    5467    ///
     
    6477    ///
    6578
    66     void train(void);
     79    bool train(void);
    6780   
    6881     
     
    7285    double c_;
    7386    gslapi::matrix kernel_;
     87    unsigned long int max_epochs_;
    7488    gslapi::vector target_;
    7589    bool trained_;
     
    99113
    100114#endif
    101 
Note: See TracChangeset for help on using the changeset viewer.