diff --git a/dlib/svm/function.h b/dlib/svm/function.h index 2b19a3fd6..d3ba92705 100644 --- a/dlib/svm/function.h +++ b/dlib/svm/function.h @@ -11,6 +11,7 @@ #include "../algs.h" #include "../serialize.h" #include "../rand.h" +#include "../statistics.h" namespace dlib { @@ -113,12 +114,6 @@ namespace dlib std::istream& in ) { - typedef typename K::scalar_type scalar_type; - typedef typename K::sample_type sample_type; - typedef typename K::mem_manager_type mem_manager_type; - - typedef matrix scalar_vector_type; - typedef matrix sample_vector_type; try { deserialize(item.alpha, in); @@ -349,12 +344,6 @@ namespace dlib std::istream& in ) { - typedef typename K::scalar_type scalar_type; - typedef typename K::sample_type sample_type; - typedef typename K::mem_manager_type mem_manager_type; - - typedef matrix scalar_vector_type; - typedef matrix sample_vector_type; try { deserialize(item.alpha, in); @@ -368,6 +357,77 @@ namespace dlib } } +// ---------------------------------------------------------------------------------------- + + template < + typename function_type + > + struct normalized_function + { + typedef typename function_type::scalar_type scalar_type; + typedef typename function_type::sample_type sample_type; + typedef typename function_type::mem_manager_type mem_manager_type; + + vector_normalizer normalizer; + function_type function; + + normalized_function ( + ){} + + normalized_function ( + const normalized_function& f + ) : + normalizer(f.normalizer), + function(f.function) + {} + + normalized_function ( + const vector_normalizer& normalizer_, + const function_type& funct + ) : normalizer(normalizer_), function(funct) {} + + scalar_type operator() ( + const sample_type& x + ) const { return function(normalizer(x)); } + }; + + template < + typename function_type + > + void serialize ( + const normalized_function& item, + std::ostream& out + ) + { + try + { + serialize(item.normalizer, out); + serialize(item.function, out); + } + catch (serialization_error e) + { + throw serialization_error(e.info + "\n while serializing object of type normalized_function"); + } + } + + template < + typename function_type + > + void deserialize ( + normalized_function& item, + std::istream& in + ) + { + try + { + deserialize(item.normalizer, in); + deserialize(item.function, in); + } + catch (serialization_error e) + { + throw serialization_error(e.info + "\n while deserializing object of type normalized_function"); + } + } // ---------------------------------------------------------------------------------------- diff --git a/dlib/svm/function_abstract.h b/dlib/svm/function_abstract.h index b0296ec2b..95b1c8493 100644 --- a/dlib/svm/function_abstract.h +++ b/dlib/svm/function_abstract.h @@ -9,6 +9,7 @@ #include "../matrix/matrix_abstract.h" #include "../algs.h" #include "../serialize.h" +#include "../statistics/statistics_abstract.h" namespace dlib { @@ -85,7 +86,7 @@ namespace dlib ) const /*! ensures - - evalutes this sample according to the decision + - evaluates this sample according to the decision function contained in this object. !*/ { @@ -359,6 +360,97 @@ namespace dlib // ---------------------------------------------------------------------------------------- + template < + typename function_type + > + struct normalized_function + { + /*! + REQUIREMENTS ON function_type + - function_type must be a function object with an overloaded + operator() similar to the other function objects defined in + this file. + - function_type::sample_type must be a dlib::matrix column + matrix type + + WHAT THIS OBJECT REPRESENTS + This object represents some sort of function object that + automatically normalizes its inputs using a vector_normalizer + object. + !*/ + + typedef typename function_type::scalar_type scalar_type; + typedef typename function_type::sample_type sample_type; + typedef typename function_type::mem_manager_type mem_manager_type; + + vector_normalizer normalizer; + function_type function; + + normalized_function ( + ); + /*! + ensures + - the members of this object have their default values + !*/ + + normalized_function ( + const normalized_function& f + ); + /*! + ensures + - #*this is a copy of f + !*/ + + normalized_function ( + const vector_normalizer& normalizer_, + const function_type& funct + ) : normalizer(normalizer_), function(funct) {} + /*! + ensures + - populates this object with the vector_normalizer and function object + !*/ + + normalized_function& operator= ( + const normalized_function& d + ); + /*! + ensures + - #*this is identical to d + - returns *this + !*/ + + scalar_type operator() ( + const sample_type& x + ) const + /*! + ensures + - returns function(normalizer(x)) + !*/ + }; + + template < + typename K + > + void serialize ( + const normalized_function& item, + std::ostream& out + ); + /*! + provides serialization support for normalized_function + !*/ + + template < + typename K + > + void deserialize ( + normalized_function& item, + std::istream& in + ); + /*! + provides serialization support for normalized_function + !*/ + +// ---------------------------------------------------------------------------------------- }