1 // Copyright (C) 2010 Davis E. King (davis@dlib.net) 2 // License: Boost Software License See LICENSE.txt for the full license. 3 #undef DLIB_SVm_ONE_CLASS_TRAINER_ABSTRACT_ 4 #ifdef DLIB_SVm_ONE_CLASS_TRAINER_ABSTRACT_ 5 6 #include <cmath> 7 #include <limits> 8 #include "../matrix/matrix_abstract.h" 9 #include "../algs.h" 10 #include "function_abstract.h" 11 #include "kernel_abstract.h" 12 #include "../optimization/optimization_solve_qp3_using_smo_abstract.h" 13 14 namespace dlib 15 { 16 17 // ---------------------------------------------------------------------------------------- 18 19 template < 20 typename K 21 > 22 class svm_one_class_trainer 23 { 24 /*! 25 REQUIREMENTS ON K 26 is a kernel function object as defined in dlib/svm/kernel_abstract.h 27 28 WHAT THIS OBJECT REPRESENTS 29 This object implements a trainer for a support vector machine for 30 solving one-class classification problems. It is implemented using the SMO 31 algorithm. 32 33 The implementation of the training algorithm used by this object is based 34 on the following excellent paper: 35 - Chih-Chung Chang and Chih-Jen Lin, LIBSVM : a library for support vector 36 machines, 2001. Software available at http://www.csie.ntu.edu.tw/~cjlin/libsvm 37 38 !*/ 39 40 public: 41 typedef K kernel_type; 42 typedef typename kernel_type::scalar_type scalar_type; 43 typedef typename kernel_type::sample_type sample_type; 44 typedef typename kernel_type::mem_manager_type mem_manager_type; 45 typedef decision_function<kernel_type> trained_function_type; 46 47 svm_one_class_trainer ( 48 ); 49 /*! 50 ensures 51 - This object is properly initialized and ready to be used 52 to train a support vector machine. 53 - #get_nu() == 0.1 54 - #get_cache_size() == 200 55 - #get_epsilon() == 0.001 56 !*/ 57 58 svm_one_class_trainer ( 59 const kernel_type& kernel, 60 const scalar_type& nu 61 ); 62 /*! 63 requires 64 - 0 < nu <= 1 65 ensures 66 - This object is properly initialized and ready to be used 67 to train a support vector machine. 68 - #get_kernel() == kernel 69 - #get_nu() == nu 70 - #get_cache_size() == 200 71 - #get_epsilon() == 0.001 72 !*/ 73 74 void set_cache_size ( 75 long cache_size 76 ); 77 /*! 78 requires 79 - cache_size > 0 80 ensures 81 - #get_cache_size() == cache_size 82 !*/ 83 84 const long get_cache_size ( 85 ) const; 86 /*! 87 ensures 88 - returns the number of megabytes of cache this object will use 89 when it performs training via the this->train() function. 90 (bigger values of this may make training go faster but won't affect 91 the result. However, too big a value will cause you to run out of 92 memory, obviously.) 93 !*/ 94 95 void set_epsilon ( 96 scalar_type eps 97 ); 98 /*! 99 requires 100 - eps > 0 101 ensures 102 - #get_epsilon() == eps 103 !*/ 104 105 const scalar_type get_epsilon ( 106 ) const; 107 /*! 108 ensures 109 - returns the error epsilon that determines when training should stop. 110 Generally a good value for this is 0.001. Smaller values may result 111 in a more accurate solution but take longer to execute. 112 !*/ 113 114 void set_kernel ( 115 const kernel_type& k 116 ); 117 /*! 118 ensures 119 - #get_kernel() == k 120 !*/ 121 122 const kernel_type& get_kernel ( 123 ) const; 124 /*! 125 ensures 126 - returns a copy of the kernel function in use by this object 127 !*/ 128 129 void set_nu ( 130 scalar_type nu 131 ); 132 /*! 133 requires 134 - 0 < nu <= 1 135 ensures 136 - #get_nu() == nu 137 !*/ 138 139 const scalar_type get_nu ( 140 ) const; 141 /*! 142 ensures 143 - returns the nu svm parameter. This is a value between 0 and 144 1. It is the parameter that determines the trade off between 145 trying to fit the training data exactly or allowing more errors 146 but hopefully improving the generalization ability of the 147 resulting classifier. Smaller values encourage exact fitting 148 while larger values of nu may encourage better generalization. 149 For more information you should consult the papers referenced 150 above. 151 !*/ 152 153 template < 154 typename in_sample_vector_type 155 > 156 const decision_function<kernel_type> train ( 157 const in_sample_vector_type& x 158 ) const; 159 /*! 160 requires 161 - x.size() > 0 162 - is_col_vector(x) == true 163 - x == a matrix or something convertible to a matrix via mat(). 164 Also, x should contain sample_type objects. 165 ensures 166 - trains a one-class support vector classifier given the training samples in x. 167 Training is done when the error is less than get_epsilon(). 168 - returns a decision function F with the following properties: 169 - if (new_x is a sample predicted to arise from the distribution 170 which generated the training samples) then 171 - F(new_x) >= 0 172 - else 173 - F(new_x) < 0 174 !*/ 175 176 void swap ( 177 svm_one_class_trainer& item 178 ); 179 /*! 180 ensures 181 - swaps *this and item 182 !*/ 183 }; 184 185 template <typename K> swap(svm_one_class_trainer<K> & a,svm_one_class_trainer<K> & b)186 void swap ( 187 svm_one_class_trainer<K>& a, 188 svm_one_class_trainer<K>& b 189 ) { a.swap(b); } 190 /*! 191 provides a global swap 192 !*/ 193 194 // ---------------------------------------------------------------------------------------- 195 196 } 197 198 #endif // DLIB_SVm_ONE_CLASS_TRAINER_ABSTRACT_ 199 200 201 202