Commit 27dfcc36 authored by Davis King's avatar Davis King

Finished the reduced_decision_function_trainer2 object. Also added

the kernel_derivative template.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402421
parent 9773a975
......@@ -14,6 +14,10 @@
namespace dlib
{
// ----------------------------------------------------------------------------------------
template < typename kernel_type > struct kernel_derivative;
// ----------------------------------------------------------------------------------------
template <
......@@ -96,6 +100,28 @@ namespace dlib
}
}
template <
typename T
>
struct kernel_derivative<radial_basis_kernel<T> >
{
typedef typename T::type scalar_type;
typedef T sample_type;
typedef typename T::mem_manager_type mem_manager_type;
kernel_derivative(const radial_basis_kernel<T>& k_) : k(k_){}
const sample_type& operator() (const sample_type& x, const sample_type& y) const
{
// return the derivative of the rbf kernel
temp = 2*k.gamma*(x-y)*k(x,y);
return temp;
}
const radial_basis_kernel<T>& k;
mutable sample_type temp;
};
// ----------------------------------------------------------------------------------------
template <
......
......@@ -313,6 +313,48 @@ namespace dlib
provides deserialization support for linear_kernel
!*/
// ----------------------------------------------------------------------------------------
template <
typename kernel_type
>
struct kernel_derivative
{
/*!
REQUIREMENTS ON kernel_type
kernel_type must be one of the following kernel types:
- radial_basis_kernel
WHAT THIS OBJECT REPRESENTS
This is a function object that computes the derivative of a kernel
function object.
!*/
typedef typename kernel_type::scalar_type scalar_type;
typedef typename kernel_type::sample_type sample_type;
typedef typename kernel_type::mem_manager_type mem_manager_type;
kernel_derivative(
const kernel_type& k_
);
/*!
ensures
- this object will return derivatives of the kernel object k_
- #k == k_
!*/
const sample_type operator() (
const sample_type& x,
const sample_type& y
) const;
/*!
ensures
- returns the derivative of k with respect to y. Or in other words, k(x, y+dy)/dy
!*/
const kernel_type& k;
};
// ----------------------------------------------------------------------------------------
}
......
This diff is collapsed.
......@@ -12,6 +12,8 @@
namespace dlib
{
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <
......@@ -20,6 +22,9 @@ namespace dlib
class reduced_decision_function_trainer
{
/*!
REQUIREMENTS ON trainer_type
- trainer_type == some kind of trainer object (e.g. svm_nu_trainer)
WHAT THIS OBJECT REPRESENTS
This object represents an implementation of a reduced set algorithm
for support vector decision functions. This object acts as a post
......@@ -36,14 +41,13 @@ namespace dlib
typedef typename trainer_type::mem_manager_type mem_manager_type;
typedef typename trainer_type::trained_function_type trained_function_type;
explicit reduced_decision_function_trainer (
reduced_decision_function_trainer (
const trainer_type& trainer,
const unsigned long num_sv
);
/*!
requires
- num_sv > 0
- trainer_type == some kind of trainer object (e.g. svm_nu_trainer)
ensures
- returns a trainer object that applies post processing to the decision_function
objects created by the given trainer object with the goal of creating
......@@ -92,6 +96,102 @@ namespace dlib
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <
typename trainer_type
>
class reduced_decision_function_trainer2
{
/*!
REQUIREMENTS ON trainer_type
- trainer_type == some kind of trainer object (e.g. svm_nu_trainer)
- trainer_type::sample_type must be a dlib::matrix object
- kernel_derivative<trainer_type::kernel_type> must be defined
WHAT THIS OBJECT REPRESENTS
This object represents an implementation of a reduced set algorithm
for support vector decision functions. This object acts as a post
processor for anything that creates decision_function objects. It
wraps another trainer object and performs this reduced set post
processing with the goal of representing the original decision
function in a form that involves fewer support vectors.
This object's implementation is the same as that in the above
reduced_decision_function_trainer object except it also performs
a global gradient based optimization at the end to further
improve the approximation to the original decision function
object.
!*/
public:
typedef typename trainer_type::kernel_type kernel_type;
typedef typename trainer_type::scalar_type scalar_type;
typedef typename trainer_type::sample_type sample_type;
typedef typename trainer_type::mem_manager_type mem_manager_type;
typedef typename trainer_type::trained_function_type trained_function_type;
reduced_decision_function_trainer2 (
const trainer_type& trainer,
const unsigned long num_sv,
double eps = 1e-5
);
/*!
requires
- num_sv > 0
ensures
- returns a trainer object that applies post processing to the decision_function
objects created by the given trainer object with the goal of creating
decision_function objects with fewer support vectors.
- The reduced decision functions that are output will have at most
num_sv support vectors.
!*/
template <
typename in_sample_vector_type,
typename in_scalar_vector_type
>
const decision_function<kernel_type> train (
const in_sample_vector_type& x,
const in_scalar_vector_type& y
) const;
/*!
ensures
- trains a decision_function using the trainer that was supplied to
this object's constructor and then finds a reduced representation
for it and returns the reduced version.
throws
- std::bad_alloc
- any exceptions thrown by the trainer_type object
!*/
};
// ----------------------------------------------------------------------------------------
template <
typename trainer_type
>
const reduced_decision_function_trainer2<trainer_type> reduced2 (
const trainer_type& trainer,
const unsigned long num_sv,
double eps = 1e-5
) { return reduced_decision_function_trainer2<trainer_type>(trainer, num_sv, eps); }
/*!
requires
- num_sv > 0
- trainer_type == some kind of trainer object that creates decision_function
objects (e.g. svm_nu_trainer)
- kernel_derivative<trainer_type::kernel_type> is defined
ensures
- returns a reduced_decision_function_trainer2 object that has been
instantiated with the given arguments.
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
}
......
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