Added the normalized_function object.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402556
This commit is contained in:
Davis King 2008-10-12 22:14:53 +00:00
parent c9b5d5dc5a
commit c7524e8e85
2 changed files with 165 additions and 13 deletions

View File

@ -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_type,0,1,mem_manager_type> scalar_vector_type;
typedef matrix<sample_type,0,1,mem_manager_type> 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_type,0,1,mem_manager_type> scalar_vector_type;
typedef matrix<sample_type,0,1,mem_manager_type> 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<sample_type> normalizer;
function_type function;
normalized_function (
){}
normalized_function (
const normalized_function& f
) :
normalizer(f.normalizer),
function(f.function)
{}
normalized_function (
const vector_normalizer<sample_type>& 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<function_type>& 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<function_type>& 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");
}
}
// ----------------------------------------------------------------------------------------

View File

@ -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<sample_type> 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<sample_type>& 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<K>& item,
std::ostream& out
);
/*!
provides serialization support for normalized_function
!*/
template <
typename K
>
void deserialize (
normalized_function<K>& item,
std::istream& in
);
/*!
provides serialization support for normalized_function
!*/
// ----------------------------------------------------------------------------------------
}