source: trunk/src/SVM.h @ 80

Last change on this file since 80 was 80, checked in by Peter, 19 years ago

to match SVM.cc

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.8 KB
Line 
1// $Id: SVM.h 80 2004-05-05 10:26:53Z peter $
2
3#ifndef _theplu_cpptools_svm_
4#define _theplu_cpptools_svm_
5
6// C++ tools include
7/////////////////////
8#include "vector.h"
9#include "matrix.h"
10
11// Standard C++ includes
12////////////////////////
13#include <utility>
14#include <vector>
15
16namespace theplu {
17namespace cpptools { 
18  ///
19  /// Class for SVM using Keerthi's second modification of Platt's SMO. Also
20  /// the elements of the kernel is not computed sequentially, but the
21  /// complete kernel matrix is taken as input and stored in memory. This
22  /// means that the training is faster, but also that it is not possible to
23  /// train a large number of samples N, since the memory cost for the kernel
24  /// matrix is N^2. The SVM object does not contain any data, hence any true
25  /// prediction is not possible.
26  ///   
27  class SVM
28  {
29 
30  public:
31    ///
32    /// Constructor taking the kernel matrix and the target vector as input
33    ///
34    SVM(const gslapi::matrix&, const gslapi::vector&, 
35        const std::vector<size_t> = std::vector<size_t>());
36         
37    ///
38    /// Function returns \f$\alpha\f$
39    ///
40    inline gslapi::vector get_alpha(void) const { return alpha_; }
41
42    ///
43    /// Function returns the C-parameter
44    ///
45    inline double get_c(void) const { return c_; }
46
47    ///
48    /// Function returns the output from SVM
49    ///
50    inline gslapi::vector get_output(void) const { return (kernel_ * alpha_.mul_elements(target_) + gslapi::vector(target_.size(),bias_) );}
51
52    ///
53    /// Changing the C-parameter
54    ///
55    inline void set_c(const double c) {c_ = c;}
56
57    ///
58    /// Training the SVM following Platt's SMO, with Keerti's
59    /// modifacation. However the complete kernel is stored in memory. The
60    /// reason for this is speed. When number of samples N is large this is
61    /// not possible since the memory cost for the kernel scales N^2. In that
62    /// case one should follow the SMO and calculate the kernel elements
63    /// sequentially
64    ///
65
66    void train(void);
67   
68     
69  private:
70    gslapi::vector alpha_;
71    double bias_;
72    double c_;
73    gslapi::matrix kernel_;
74    gslapi::vector target_;
75    bool trained_;
76    std::vector<size_t> train_set_;
77    double tolerance_;
78    ///
79    ///   Private function choosing which two elements that should be
80    ///   updated. First checking for the biggest violation (output - target =
81    ///   0) among support vectors (alpha!=0). If no violation was found check
82    ///   for sequentially among the other samples. If no violation there as
83    ///   well, stop_condition is fullfilled.
84    ///
85   
86    std::pair<size_t, size_t> choose(const theplu::gslapi::vector&, 
87                                  const std::vector<size_t>&, 
88                                  const std::vector<size_t>&, 
89                                  const theplu::gslapi::vector&,
90                                  bool&);
91   
92   
93  };
94
95
96
97
98}} // of namespace cpptools and namespace theplu
99
100#endif
101
Note: See TracBrowser for help on using the repository browser.