Changeset 47


Ignore:
Timestamp:
Mar 2, 2004, 12:10:51 PM (18 years ago)
Author:
Peter
Message:

* empty log message *

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/SVM.cc

    r45 r47  
    66// System includes
    77#include <math.h>
     8#include <utility>
    89
    910// Thep C++ Tools
     
    3637  double v;
    3738  gslapi::vector dalpha; 
    38  
     39
    3940  // Stop criteria 
    4041  bool stop_condition = false;
     
    4243    count++; 
    4344    dalpha = alpha_.mul_elements(target_);
    44     E = kernel_*dalpha-target_; //Peter, should be done in another way!!
     45    E = kernel_*dalpha-target_; //Peter, could be done faster!!
    4546   
    4647    // Choosing a pair of variables to modify
    4748    pair<u_int, u_int> index(0,0);     
    48     index = SVM::choose(target_.size());
     49    index = choose(target_.size());
    4950   
     51    cout << count << endl;
    5052    //Updating the two variables
    5153    if (target_[index.first]!=target_[index.second]) {       
     
    9597  gslapi::vector output_unbiased = kernel_ * alpha_.mul_elements(target_);
    9698  for (u_int i=0; i<target_.size(); i++){
    97     if (target_[i]==1){
     99    if (target_[i]==1) {
    98100      if (output_unbiased[i] < min_output_positive) {
    99101  min_output_positive = output_unbiased[i];
     
    103105      max_output_negative = output_unbiased[i];
    104106    }
    105    
    106107    bias_ = ( -min_output_positive - max_output_negative )/2;
    107    
    108108    trained_= true;
    109109  }
     110}
     111
     112//(Peter, do it more clever)
     113std::pair<u_int,u_int> SVM::choose(u_int nof_samples)
     114{
     115  random_singleton* rnd=random_singleton::get_instance();
     116  u_int index1 = rnd->get_uniform_int(nof_samples);
     117  u_int index2 = rnd->get_uniform_int(nof_samples-1);
     118  if (index2 >= index1)
     119    index2++;
     120  return std::pair<u_int,u_int>(index1,index2);
    110121}
    111122
     
    132143      return false;
    133144    }
    134     else{
     145    else {
    135146      double primal = alpha_.mul_elements(target_)
    136147    *  ( alpha_.mul_elements(target_) );
     
    140151      else
    141152    return false;
    142   }
     153    }
    143154
    144155
  • trunk/src/SVM.h

    r45 r47  
    4343    /// Function will return \f$\alpha\f$
    4444    ///
    45     inline gslapi::vector get_alpha() const;
     45    inline gslapi::vector get_alpha() const { return alpha_; }
    4646
    4747    ///
     
    7979 
    8080    // class SVM
    81   gslapi::vector SVM::get_alpha() const 
    82   {
    83     return alpha_;
    84   }
    8581
    8682  gslapi::vector SVM::get_output() const 
     
    8985    bias.set_all(bias_);
    9086    return kernel_ * alpha_.mul_elements(target_) + bias;
    91    
    9287  }
    9388
Note: See TracChangeset for help on using the changeset viewer.