00001 // -*- C++ -*- 00002 #ifndef __LEMGA_SVM_H__ 00003 #define __LEMGA_SVM_H__ 00004 00011 #include "learnmodel.h" 00012 #include "kernel.h" 00013 00014 namespace lemga { 00015 00017 struct SVM_detail; 00018 00019 class SVM : public LearnModel { 00020 private: 00021 SVM_detail *detail; 00022 kernel::Kernel *ker; 00023 // Why not var_shared_ptr? because kernel is not meant to be shared 00024 REAL regC; // regularization parameter C 00025 std::vector<Input> sv; // support vectors 00026 std::vector<REAL> coef; // @f$y_i\alpha_i@f$ 00027 REAL coef0; // the bias 00028 00029 public: 00030 explicit SVM (UINT n_in = 0); 00031 explicit SVM (const kernel::Kernel&, UINT n_in = 0); 00032 SVM (const SVM&); 00033 explicit SVM (std::istream&); 00034 virtual ~SVM (); 00035 const SVM& operator= (const SVM&); 00036 00037 virtual const id_t& id () const; 00038 virtual SVM* create () const { return new SVM(); } 00039 virtual SVM* clone () const { return new SVM(*this); } 00040 00041 REAL C () const { return regC; } 00042 void set_C (REAL c) { regC = c; } 00043 UINT n_support_vectors () const { return sv.size(); } 00044 const Input& support_vector (UINT i) const { return sv[i]; } 00046 REAL support_vector_coef (UINT i) const { return coef[i]; } 00047 REAL bias () const { return coef0; } 00048 const kernel::Kernel& kernel () const { return *ker; } 00049 REAL kernel (const Input&, const Input&) const; 00050 void set_kernel (const kernel::Kernel&); 00051 00052 virtual bool support_weighted_data () const { return true; } 00053 virtual void initialize (); 00054 virtual void train (); 00055 00056 virtual Output operator() (const Input&) const; 00057 virtual REAL margin_norm () const { return w_norm(); } 00058 virtual REAL margin_of (const Input&, const Output&) const; 00059 REAL w_norm () const; 00060 00061 protected: 00064 REAL signed_margin (const Input&) const; 00065 void reset_model (); 00066 virtual bool serialize (std::ostream&, ver_list&) const; 00067 virtual bool unserialize (std::istream&, ver_list&, const id_t& = NIL_ID); 00068 }; 00069 00070 } // namespace lemga 00071 00072 #ifdef __SVM_H__ 00073 #warning "This header file may conflict with another `svm.h' file." 00074 #endif 00075 #define __SVM_H__ 00076 #endif