Commit 4cd02ec4 authored by Davis King's avatar Davis King

Added a sigmoid_kernel object and also added a kernel_derivative

for the polynomial_kernel.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402434
parent fa59a5c0
......@@ -211,6 +211,113 @@ namespace dlib
}
}
template <
typename T
>
struct kernel_derivative<polynomial_kernel<T> >
{
typedef typename T::type scalar_type;
typedef T sample_type;
typedef typename T::mem_manager_type mem_manager_type;
kernel_derivative(const polynomial_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 = k.degree*k.gamma*x*std::pow(k.gamma*(trans(x)*y) + k.coef, k.degree-1);
return temp;
}
const polynomial_kernel<T>& k;
mutable sample_type temp;
};
// ----------------------------------------------------------------------------------------
template <
typename T
>
struct sigmoid_kernel
{
typedef typename T::type scalar_type;
typedef T sample_type;
typedef typename T::mem_manager_type mem_manager_type;
sigmoid_kernel(const scalar_type g, const scalar_type c) : gamma(g), coef(c) {}
sigmoid_kernel() : gamma(0.1), coef(-1.0) {}
sigmoid_kernel(
const sigmoid_kernel& k
) : gamma(k.gamma), coef(k.coef) {}
typedef T type;
const scalar_type gamma;
const scalar_type coef;
scalar_type operator() (
const sample_type& a,
const sample_type& b
) const
{
return std::tanh(gamma*(trans(a)*b) + coef);
}
sigmoid_kernel& operator= (
const sigmoid_kernel& k
)
{
const_cast<scalar_type&>(gamma) = k.gamma;
const_cast<scalar_type&>(coef) = k.coef;
return *this;
}
bool operator== (
const sigmoid_kernel& k
) const
{
return (gamma == k.gamma) && (coef == k.coef);
}
};
template <
typename T
>
void serialize (
const sigmoid_kernel<T>& item,
std::ostream& out
)
{
try
{
serialize(item.gamma, out);
serialize(item.coef, out);
}
catch (serialization_error& e)
{
throw serialization_error(e.info + "\n while serializing object of type sigmoid_kernel");
}
}
template <
typename T
>
void deserialize (
sigmoid_kernel<T>& item,
std::istream& in
)
{
typedef typename T::type scalar_type;
try
{
deserialize(const_cast<scalar_type&>(item.gamma), in);
deserialize(const_cast<scalar_type&>(item.coef), in);
}
catch (serialization_error& e)
{
throw serialization_error(e.info + "\n while deserializing object of type sigmoid_kernel");
}
}
// ----------------------------------------------------------------------------------------
template <typename T>
......
......@@ -140,6 +140,113 @@ namespace dlib
provides deserialization support for radial_basis_kernel
!*/
// ----------------------------------------------------------------------------------------
template <
typename T
>
struct sigmoid_kernel
{
/*!
REQUIREMENTS ON T
T must be a dlib::matrix object
WHAT THIS OBJECT REPRESENTS
This object represents a sigmoid kernel
!*/
typedef typename T::type scalar_type;
typedef T sample_type;
typedef typename T::mem_manager_type mem_manager_type;
const scalar_type gamma;
const scalar_type coef;
sigmoid_kernel(
);
/*!
ensures
- #gamma == 0.1
- #coef == -1.0
!*/
sigmoid_kernel(
const radial_basis_kernel& k
);
/*!
ensures
- #gamma == k.gamma
- #coef == k.coef
!*/
sigmoid_kernel(
const scalar_type g,
const scalar_type c
);
/*!
ensures
- #gamma == g
- #coef == c
!*/
scalar_type operator() (
const sample_type& a,
const sample_type& b
) const;
/*!
requires
- a.nc() == 1
- b.nc() == 1
- a.nr() == b.nr()
ensures
- returns tanh(gamma*trans(a)*b + coef)
!*/
sigmoid_kernel& operator= (
const sigmoid_kernel& k
);
/*!
ensures
- #gamma = k.gamma
- #coef = k.coef
- returns *this
!*/
bool operator== (
const sigmoid_kernel& k
) const;
/*!
ensures
- if (k and *this are identical) then
- returns true
- else
- returns false
!*/
};
template <
typename T
>
void serialize (
const sigmoid_kernel<T>& item,
std::ostream& out
);
/*!
provides serialization support for sigmoid_kernel
!*/
template <
typename K
>
void deserialize (
sigmoid_kernel<T>& item,
std::istream& in
);
/*!
provides deserialization support for sigmoid_kernel
!*/
// ----------------------------------------------------------------------------------------
template <
......@@ -178,6 +285,8 @@ namespace dlib
/*!
ensures
- #gamma == k.gamma
- #coef == k.coef
- #degree == k.degree
!*/
polynomial_kernel(
......@@ -324,6 +433,7 @@ namespace dlib
REQUIREMENTS ON kernel_type
kernel_type must be one of the following kernel types:
- radial_basis_kernel
- polynomial_kernel
WHAT THIS OBJECT REPRESENTS
This is a function object that computes the derivative of a kernel
......
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