Commit c9bfa1e1 authored by Davis King's avatar Davis King

Added two new any object. One for containing just decision function style objects and

another for trainers.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%404039
parent f3aa5779
......@@ -4,6 +4,8 @@
#define DLIB_AnY_
#include "any/any.h"
#include "any/any_trainer.h"
#include "any/any_decision_function.h"
#endif // DLIB_AnY_
......
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_AnY_DECISION_FUNCTION_H__
#define DLIB_AnY_DECISION_FUNCTION_H__
#include "any.h"
#include "../smart_pointers.h"
#include "../memory_manager.h"
#include "any_decision_function_abstract.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <
typename sample_type_,
typename scalar_type_ = double
>
class any_decision_function
{
public:
typedef sample_type_ sample_type;
typedef scalar_type_ scalar_type;
typedef memory_manager<char>::kernel_1a mem_manager_type;
any_decision_function()
{
}
any_decision_function (
const any_decision_function& item
)
{
if (item.data)
{
item.data->copy_to(data);
}
}
template <typename T>
any_decision_function (
const T& item
)
{
typedef typename basic_type<T>::type U;
data.reset(new derived<U>(item));
}
void clear (
)
{
data.reset();
}
template <typename T>
bool contains (
) const
{
typedef typename basic_type<T>::type U;
return dynamic_cast<derived<U>*>(data.get()) != 0;
}
bool is_empty(
) const
{
return data.get() == 0;
}
scalar_type operator() (
const sample_type& item
) const
{
// make sure requires clause is not broken
DLIB_ASSERT(is_empty() == false,
"\t scalar_type any_decision_function::operator()"
<< "\n\t You can't call operator() on an empty any_decision_function"
<< "\n\t this: " << this
);
return data->evaluate(item);
}
template <typename T>
T& cast_to(
)
{
typedef typename basic_type<T>::type U;
derived<U>* d = dynamic_cast<derived<U>*>(data.get());
if (d == 0)
{
throw bad_any_cast();
}
return d->item;
}
template <typename T>
const T& cast_to(
) const
{
typedef typename basic_type<T>::type U;
derived<U>* d = dynamic_cast<derived<U>*>(data.get());
if (d == 0)
{
throw bad_any_cast();
}
return d->item;
}
template <typename T>
T& get(
)
{
typedef typename basic_type<T>::type U;
derived<U>* d = dynamic_cast<derived<U>*>(data.get());
if (d == 0)
{
d = new derived<U>();
data.reset(d);
}
return d->item;
}
any_decision_function& operator= (
const any_decision_function& item
)
{
any_decision_function(item).swap(*this);
return *this;
}
void swap (
any_decision_function& item
)
{
data.swap(item.data);
}
private:
struct base
{
virtual ~base() {}
virtual void copy_to (
scoped_ptr<base>& dest
) const = 0;
virtual scalar_type evaluate (
const sample_type& samp
) const = 0;
};
template <typename T>
struct derived : public base
{
T item;
derived() {}
derived(const T& val) : item(val) {}
virtual void copy_to (
scoped_ptr<base>& dest
) const
{
dest.reset(new derived<T>(item));
}
virtual scalar_type evaluate (
const sample_type& samp
) const
{
return item(samp);
}
};
scoped_ptr<base> data;
};
// ----------------------------------------------------------------------------------------
template <
typename sample_type,
typename scalar_type
>
inline void swap (
any_decision_function<sample_type, scalar_type>& a,
any_decision_function<sample_type, scalar_type>& b
) { a.swap(b); }
// ----------------------------------------------------------------------------------------
template <typename T, typename U, typename V>
T& any_cast(any_decision_function<U,V>& a) { return a.template cast_to<T>(); }
template <typename T, typename U, typename V>
const T& any_cast(const any_decision_function<U,V>& a) { return a.template cast_to<T>(); }
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_AnY_DECISION_FUNCTION_H__
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_AnY_DECISION_FUNCTION_ABSTRACT_H_
#ifdef DLIB_AnY_DECISION_FUNCTION_ABSTRACT_H_
#include "any_abstract.h"
#include "../memory_manager.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <
typename sample_type_,
typename scalar_type_ = double
>
class any_decision_function
{
/*!
INITIAL VALUE
- is_empty() == true
- for all T: contains<T>() == false
WHAT THIS OBJECT REPRESENTS
This object is a version of dlib::any that is restricted to containing
elements which are some kind of function object with an operator() with
the following signature:
scalar_type operator()(const sample_type&) const
It is intended to be used to contain dlib::decision_function objects and
other types which represent learned decision functions. It allows you
to write code which contains and processes these decision functions
without needing to know the specific types of decision functions used.
!*/
public:
typedef sample_type_ sample_type;
typedef scalar_type_ scalar_type;
typedef memory_manager<char>::kernel_1a mem_manager_type;
any_decision_function(
);
/*!
ensures
- this object is properly initialized
!*/
any_decision_function (
const any_decision_function& item
);
/*!
ensures
- copies the state of item into *this.
- Note that *this and item will contain independent copies of the
contents of item. That is, this function performs a deep
copy and therefore does not result in *this containing
any kind of reference to item.
!*/
template < typename T >
any_decision_function (
const T& item
);
/*!
ensures
- #contains<T>() == true
- #cast_to<T>() == item
(i.e. a copy of item will be stored in *this)
!*/
void clear (
);
/*!
ensures
- #*this will have its default value. I.e. #is_empty() == true
!*/
template <typename T>
bool contains (
) const;
/*!
ensures
- if (this object currently contains an object of type T) then
- returns true
- else
- returns false
!*/
bool is_empty(
) const;
/*!
ensures
- if (this object contains any kind of object) then
- returns true
- else
- returns false
!*/
scalar_type operator() (
const sample_type& item
) const;
/*!
requires
- is_empty() == false
ensures
- Let F denote the function object contained within *this. Then
this function performs:
return F(item)
!*/
template <typename T>
T& cast_to(
);
/*!
ensures
- if (contains<T>() == true) then
- returns a non-const reference to the object contained within *this
- else
- throws bad_any_cast
!*/
template <typename T>
const T& cast_to(
) const;
/*!
ensures
- if (contains<T>() == true) then
- returns a const reference to the object contained within *this
- else
- throws bad_any_cast
!*/
template <typename T>
T& get(
);
/*!
ensures
- #is_empty() == false
- #contains<T>() == true
- if (contains<T>() == true)
- returns a non-const reference to the object contained in *this.
- else
- Constructs an object of type T inside *this
- Any previous object stored in this any_decision_function object is destructed and its
state is lost.
- returns a non-const reference to the newly created T object.
!*/
any_decision_function& operator= (
const any_decision_function& item
);
/*!
ensures
- copies the state of item into *this.
- Note that *this and item will contain independent copies of the
contents of item. That is, this function performs a deep
copy and therefore does not result in *this containing
any kind of reference to item.
!*/
void swap (
any_decision_function& item
);
/*!
ensures
- swaps *this and item
!*/
};
// ----------------------------------------------------------------------------------------
template <
typename sample_type,
typename scalar_type
>
inline void swap (
any_decision_function<sample_type,scalar_type>& a,
any_decision_function<sample_type,scalar_type>& b
) { a.swap(b); }
/*!
provides a global swap function
!*/
// ----------------------------------------------------------------------------------------
template <
typename T,
typename sample_type,
typename scalar_type
>
T& any_cast(
any_decision_function<sample_type,scalar_type>& a
) { return a.cast_to<T>(); }
/*!
ensures
- returns a.cast_to<T>()
!*/
// ----------------------------------------------------------------------------------------
template <
typename T,
typename sample_type,
typename scalar_type
>
const T& any_cast(
const any_decision_function<sample_type,scalar_type>& a
) { return a.cast_to<T>(); }
/*!
ensures
- returns a.cast_to<T>()
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_AnY_DECISION_FUNCTION_ABSTRACT_H_
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_AnY_TRAINER_H_
#define DLIB_AnY_TRAINER_H_
#include "any.h"
#include "../smart_pointers.h"
#include "any_decision_function.h"
#include "../memory_manager.h"
#include "any_trainer_abstract.h"
#include <vector>
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <
typename sample_type_,
typename scalar_type_ = double
>
class any_trainer
{
public:
typedef sample_type_ sample_type;
typedef scalar_type_ scalar_type;
typedef memory_manager<char>::kernel_1a mem_manager_type;
typedef any_decision_function<sample_type, scalar_type> trained_function_type;
any_trainer()
{
}
any_trainer (
const any_trainer& item
)
{
if (item.data)
{
item.data->copy_to(data);
}
}
template <typename T>
any_trainer (
const T& item
)
{
typedef typename basic_type<T>::type U;
data.reset(new derived<U>(item));
}
void clear (
)
{
data.reset();
}
template <typename T>
bool contains (
) const
{
typedef typename basic_type<T>::type U;
return dynamic_cast<derived<U>*>(data.get()) != 0;
}
bool is_empty(
) const
{
return data.get() == 0;
}
trained_function_type train (
const std::vector<sample_type>& samples,
const std::vector<scalar_type>& labels
) const
{
// make sure requires clause is not broken
DLIB_ASSERT(is_empty() == false,
"\t trained_function_type any_trainer::train()"
<< "\n\t You can't call train() on an empty any_trainer"
<< "\n\t this: " << this
);
return data->train(samples, labels);
}
template <typename T>
T& cast_to(
)
{
typedef typename basic_type<T>::type U;
derived<U>* d = dynamic_cast<derived<U>*>(data.get());
if (d == 0)
{
throw bad_any_cast();
}
return d->item;
}
template <typename T>
const T& cast_to(
) const
{
typedef typename basic_type<T>::type U;
derived<U>* d = dynamic_cast<derived<U>*>(data.get());
if (d == 0)
{
throw bad_any_cast();
}
return d->item;
}
template <typename T>
T& get(
)
{
typedef typename basic_type<T>::type U;
derived<U>* d = dynamic_cast<derived<U>*>(data.get());
if (d == 0)
{
d = new derived<U>();
data.reset(d);
}
return d->item;
}
any_trainer& operator= (
const any_trainer& item
)
{
any_trainer(item).swap(*this);
return *this;
}
void swap (
any_trainer& item
)
{
data.swap(item.data);
}
private:
struct base
{
virtual ~base() {}
virtual trained_function_type train (
const std::vector<sample_type>& samples,
const std::vector<scalar_type>& labels
) const = 0;
virtual void copy_to (
scoped_ptr<base>& dest
) const = 0;
};
template <typename T>
struct derived : public base
{
T item;
derived() {}
derived(const T& val) : item(val) {}
virtual void copy_to (
scoped_ptr<base>& dest
) const
{
dest.reset(new derived<T>(item));
}
virtual trained_function_type train (
const std::vector<sample_type>& samples,
const std::vector<scalar_type>& labels
) const
{
return item.train(samples, labels);
}
};
scoped_ptr<base> data;
};
// ----------------------------------------------------------------------------------------
template <
typename sample_type,
typename scalar_type
>
inline void swap (
any_trainer<sample_type,scalar_type>& a,
any_trainer<sample_type,scalar_type>& b
) { a.swap(b); }
// ----------------------------------------------------------------------------------------
template <typename T, typename U, typename V>
T& any_cast(any_trainer<U,V>& a) { return a.template cast_to<T>(); }
template <typename T, typename U, typename V>
const T& any_cast(const any_trainer<U,V>& a) { return a.template cast_to<T>(); }
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_AnY_TRAINER_H_
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_AnY_TRAINER_ABSTRACT_H_
#ifdef DLIB_AnY_TRAINER_ABSTRACT_H_
#include "any_abstract.h"
#include "../memory_manager.h"
#include "any_decision_function_abstract.h"
#include <vector>
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <
typename sample_type_,
typename scalar_type_ = double
>
class any_trainer
{
/*!
INITIAL VALUE
- is_empty() == true
- for all T: contains<T>() == false
WHAT THIS OBJECT REPRESENTS
This object is a version of dlib::any that is restricted to containing
elements which are some kind of object with a .train() method compatible
with the following signature:
decision_function train(
const std::vector<sample_type>& samples,
const std::vector<scalar_type>& labels
) const
Where decision_function is a type capable of being stored in an
any_decision_function<sample_type,scalar_type> object.
any_trainer is intended to be used to contain objects such as the svm_nu_trainer
and other similar types which represent supervised machine learning algorithms.
It allows you to write code which contains and processes these trainer objects
without needing to know the specific types of trainer objects used.
!*/
public:
typedef sample_type_ sample_type;
typedef scalar_type_ scalar_type;
typedef memory_manager<char>::kernel_1a mem_manager_type;
typedef any_decision_function<sample_type, scalar_type> trained_function_type;
any_trainer(
);
/*!
ensures
- this object is properly initialized
!*/
any_trainer (
const any_trainer& item
);
/*!
ensures
- copies the state of item into *this.
- Note that *this and item will contain independent copies of the
contents of item. That is, this function performs a deep
copy and therefore does not result in *this containing
any kind of reference to item.
!*/
template < typename T >
any_trainer (
const T& item
);
/*!
ensures
- #contains<T>() == true
- #cast_to<T>() == item
(i.e. a copy of item will be stored in *this)
!*/
void clear (
);
/*!
ensures
- #*this will have its default value. I.e. #is_empty() == true
!*/
template <typename T>
bool contains (
) const;
/*!
ensures
- if (this object currently contains an object of type T) then
- returns true
- else
- returns false
!*/
bool is_empty(
) const;
/*!
ensures
- if (this object contains any kind of object) then
- returns true
- else
- returns false
!*/
trained_function_type train (
const std::vector<sample_type>& samples,
const std::vector<scalar_type>& labels
) const
/*!
requires
- is_empty() == false
ensures
- Let TRAINER denote the object contained within *this. Then
this function performs:
return TRAINER.train(samples, labels)
!*/
template <typename T>
T& cast_to(
);
/*!
ensures
- if (contains<T>() == true) then
- returns a non-const reference to the object contained within *this
- else
- throws bad_any_cast
!*/
template <typename T>
const T& cast_to(
) const;
/*!
ensures
- if (contains<T>() == true) then
- returns a const reference to the object contained within *this
- else
- throws bad_any_cast
!*/
template <typename T>
T& get(
);
/*!
ensures
- #is_empty() == false
- #contains<T>() == true
- if (contains<T>() == true)
- returns a non-const reference to the object contained in *this.
- else
- Constructs an object of type T inside *this
- Any previous object stored in this any_trainer object is destructed and its
state is lost.
- returns a non-const reference to the newly created T object.
!*/
any_trainer& operator= (
const any_trainer& item
);
/*!
ensures
- copies the state of item into *this.
- Note that *this and item will contain independent copies of the
contents of item. That is, this function performs a deep
copy and therefore does not result in *this containing
any kind of reference to item.
!*/
void swap (
any_trainer& item
);
/*!
ensures
- swaps *this and item
!*/
};
// ----------------------------------------------------------------------------------------
template <
typename sample_type,
typename scalar_type
>
inline void swap (
any_trainer<sample_type,scalar_type>& a,
any_trainer<sample_type,scalar_type>& b
) { a.swap(b); }
/*!
provides a global swap function
!*/
// ----------------------------------------------------------------------------------------
template <
typename T,
typename sample_type,
typename scalar_type
>
T& any_cast(
any_trainer<sample_type,scalar_type>& a
) { return a.cast_to<T>(); }
/*!
ensures
- returns a.cast_to<T>()
!*/
// ----------------------------------------------------------------------------------------
template <
typename T,
typename sample_type,
typename scalar_type
>
const T& any_cast(
const any_trainer<sample_type,scalar_type>& a
) { return a.cast_to<T>(); }
/*!
ensures
- returns a.cast_to<T>()
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_AnY_TRAINER_ABSTRACT_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment