Changeset 37 for trunk


Ignore:
Timestamp:
Feb 13, 2004, 4:46:31 PM (19 years ago)
Author:
Peter
Message:

SVM.get_output added

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/SVM.cc

    r36 r37  
    2020                 kernel_(kernel),
    2121                 target_(target),
    22                  alpha_(target.size(),true,true)
     22                 alpha_(target.size(),true,true),
     23                 bias_(0)
     24 
    2325{
    2426}
     
    3840  // Stop criteria 
    3941  bool stop_condition = false;
    40   while(count<100000 && !stop_condition)
     42  while(!stop_condition)
    4143    {
    4244     count++; 
     
    9092     alpha_[index2]=alpha_new;
    9193     stop_condition = stop( target_, kernel_, alpha_);
     94     if (count>10000000){
     95      cerr << "SVM): " << "more than 10,000,000 epochs reached" << endl;
     96       exit(1);
     97     }
    9298    }
     99
     100  // Calculating the bias
     101  double min_output_positive = 10000;
     102  double max_output_negative = -10000;
     103  thep_gsl_api::vector output_unbiased = kernel_ * alpha_.mul_elements(target_);
     104  for (u_int i=0; i<target_.size(); i++){
     105   if (target_[i]==1){
     106     if (output_unbiased[i] < min_output_positive)
     107       min_output_positive = output_unbiased[i];
     108   }
     109   else if( output_unbiased[i] > max_output_negative )
     110     max_output_negative = output_unbiased[i];
    93111 
     112  }
     113  cout << max_output_negative << endl;
     114  cout << min_output_positive << endl;
     115 
     116  bias_ = ( -min_output_positive - max_output_negative )/2;
     117  cout << "bias:" << bias_ << endl;
     118
    94119  trained_= true;
    95120}
     
    101126  double min_output_positive = 10000;
    102127  double max_output_negative = -10000;
    103   double epsilon = 0.001; // used twice, should perhaps be two different values
     128  double epsilon = 0.000001; // used twice, should perhaps be two different values
    104129  thep_gsl_api::vector output_unbiased = kernel_ * alpha_.mul_elements(target_);
    105130  for (u_int i=0; i<target_.size(); i++){
  • trunk/src/SVM.h

    r36 r37  
    88#include "vector.h"
    99#include "matrix.h"
     10
    1011
    1112// Standard C++ includes
     
    3435    */
    3536    inline thep_gsl_api::vector get_alpha() const;
     37
     38    /**
     39       Function will return the output from SVM
     40    */
     41    inline thep_gsl_api::vector get_output() const;
     42
    3643   
    3744     
     
    4148    thep_gsl_api::vector target_;
    4249    thep_gsl_api::vector alpha_;
     50    double bias_;
    4351
    4452    /**
    45        Private function that determines when to stop the training
     53       Private function that determines when to stop the training.
     54       The test is done in two steps. First, we check that the
     55       functional margin is at least 2 - epsilon. Second, we check
     56       that the gap between the primal and the dual object is less
     57       than epsilon.
     58
    4659    */
    4760    bool SVM::stop(const thep_gsl_api::vector& target_,
     
    5568    return alpha_;
    5669  }
     70
     71  thep_gsl_api::vector SVM::get_output() const 
     72  {
     73    thep_gsl_api::vector bias(target_.size(), false, false);
     74    bias.set_all(bias_);
     75    return kernel_ * alpha_.mul_elements(target_) + bias;
     76   
     77  }
    5778}; // namespace thep_c++_tools
    5879
  • trunk/src/vector.cc

    r36 r37  
    247247{
    248248  double sum = 0;
    249   for (int i=0; i<size(); i++)
     249  for (u_int i=0; i<size(); i++)
    250250   sum += gsl_vector_get( v_, i );
    251251  return( sum );
Note: See TracChangeset for help on using the changeset viewer.