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_AnY_TRAINER_ABSTRACT_H_ 4 #ifdef DLIB_AnY_TRAINER_ABSTRACT_H_ 5 6 #include "any_abstract.h" 7 #include "../algs.h" 8 #include "any_decision_function_abstract.h" 9 #include <vector> 10 11 namespace dlib 12 { 13 14 // ---------------------------------------------------------------------------------------- 15 16 template < 17 typename sample_type_, 18 typename scalar_type_ = double 19 > 20 class any_trainer 21 { 22 /*! 23 INITIAL VALUE 24 - is_empty() == true 25 - for all T: contains<T>() == false 26 27 WHAT THIS OBJECT REPRESENTS 28 This object is a version of dlib::any that is restricted to containing 29 elements which are some kind of object with a .train() method compatible 30 with the following signature: 31 32 decision_function train( 33 const std::vector<sample_type>& samples, 34 const std::vector<scalar_type>& labels 35 ) const 36 37 Where decision_function is a type capable of being stored in an 38 any_decision_function<sample_type,scalar_type> object. 39 40 any_trainer is intended to be used to contain objects such as the svm_nu_trainer 41 and other similar types which represent supervised machine learning algorithms. 42 It allows you to write code which contains and processes these trainer objects 43 without needing to know the specific types of trainer objects used. 44 !*/ 45 46 public: 47 48 typedef sample_type_ sample_type; 49 typedef scalar_type_ scalar_type; 50 typedef default_memory_manager mem_manager_type; 51 typedef any_decision_function<sample_type, scalar_type> trained_function_type; 52 53 any_trainer( 54 ); 55 /*! 56 ensures 57 - this object is properly initialized 58 !*/ 59 60 any_trainer ( 61 const any_trainer& item 62 ); 63 /*! 64 ensures 65 - copies the state of item into *this. 66 - Note that *this and item will contain independent copies of the 67 contents of item. That is, this function performs a deep 68 copy and therefore does not result in *this containing 69 any kind of reference to item. 70 !*/ 71 72 template < typename T > 73 any_trainer ( 74 const T& item 75 ); 76 /*! 77 ensures 78 - #contains<T>() == true 79 - #cast_to<T>() == item 80 (i.e. a copy of item will be stored in *this) 81 !*/ 82 83 void clear ( 84 ); 85 /*! 86 ensures 87 - #*this will have its default value. I.e. #is_empty() == true 88 !*/ 89 90 template <typename T> 91 bool contains ( 92 ) const; 93 /*! 94 ensures 95 - if (this object currently contains an object of type T) then 96 - returns true 97 - else 98 - returns false 99 !*/ 100 101 bool is_empty( 102 ) const; 103 /*! 104 ensures 105 - if (this object contains any kind of object) then 106 - returns false 107 - else 108 - returns true 109 !*/ 110 111 trained_function_type train ( 112 const std::vector<sample_type>& samples, 113 const std::vector<scalar_type>& labels 114 ) const 115 /*! 116 requires 117 - is_empty() == false 118 ensures 119 - Let TRAINER denote the object contained within *this. Then 120 this function performs: 121 return TRAINER.train(samples, labels) 122 !*/ 123 124 template <typename T> 125 T& cast_to( 126 ); 127 /*! 128 ensures 129 - if (contains<T>() == true) then 130 - returns a non-const reference to the object contained within *this 131 - else 132 - throws bad_any_cast 133 !*/ 134 135 template <typename T> 136 const T& cast_to( 137 ) const; 138 /*! 139 ensures 140 - if (contains<T>() == true) then 141 - returns a const reference to the object contained within *this 142 - else 143 - throws bad_any_cast 144 !*/ 145 146 template <typename T> 147 T& get( 148 ); 149 /*! 150 ensures 151 - #is_empty() == false 152 - #contains<T>() == true 153 - if (contains<T>() == true) 154 - returns a non-const reference to the object contained in *this. 155 - else 156 - Constructs an object of type T inside *this 157 - Any previous object stored in this any_trainer object is destructed and its 158 state is lost. 159 - returns a non-const reference to the newly created T object. 160 !*/ 161 162 any_trainer& operator= ( 163 const any_trainer& item 164 ); 165 /*! 166 ensures 167 - copies the state of item into *this. 168 - Note that *this and item will contain independent copies of the 169 contents of item. That is, this function performs a deep 170 copy and therefore does not result in *this containing 171 any kind of reference to item. 172 !*/ 173 174 void swap ( 175 any_trainer& item 176 ); 177 /*! 178 ensures 179 - swaps *this and item 180 !*/ 181 182 }; 183 184 // ---------------------------------------------------------------------------------------- 185 186 template < 187 typename sample_type, 188 typename scalar_type 189 > swap(any_trainer<sample_type,scalar_type> & a,any_trainer<sample_type,scalar_type> & b)190 inline void swap ( 191 any_trainer<sample_type,scalar_type>& a, 192 any_trainer<sample_type,scalar_type>& b 193 ) { a.swap(b); } 194 /*! 195 provides a global swap function 196 !*/ 197 198 // ---------------------------------------------------------------------------------------- 199 200 template < 201 typename T, 202 typename sample_type, 203 typename scalar_type 204 > any_cast(any_trainer<sample_type,scalar_type> & a)205 T& any_cast( 206 any_trainer<sample_type,scalar_type>& a 207 ) { return a.cast_to<T>(); } 208 /*! 209 ensures 210 - returns a.cast_to<T>() 211 !*/ 212 213 // ---------------------------------------------------------------------------------------- 214 215 template < 216 typename T, 217 typename sample_type, 218 typename scalar_type 219 > any_cast(const any_trainer<sample_type,scalar_type> & a)220 const T& any_cast( 221 const any_trainer<sample_type,scalar_type>& a 222 ) { return a.cast_to<T>(); } 223 /*! 224 ensures 225 - returns a.cast_to<T>() 226 !*/ 227 228 // ---------------------------------------------------------------------------------------- 229 230 } 231 232 #endif // DLIB_AnY_TRAINER_ABSTRACT_H_ 233 234 235