Commit 3846bc63 authored by Davis King's avatar Davis King

Added the offset_kernel

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402884
parent 20008940
......@@ -359,6 +359,89 @@ namespace dlib
std::istream& in
){}
// ----------------------------------------------------------------------------------------
template <typename T>
struct offset_kernel
{
typedef typename T::scalar_type scalar_type;
typedef typename T::sample_type sample_type;
typedef typename T::mem_manager_type mem_manager_type;
offset_kernel(const T& k, const scalar_type& offset_
) : kernel(k), offset(offset_) {}
offset_kernel() : offset(0.01) {}
offset_kernel(
const offset_kernel& k
) : kernel(k.kernel), offset(k.offset) {}
const T kernel;
const scalar_type offset;
scalar_type operator() (
const sample_type& a,
const sample_type& b
) const
{
return kernel(a,b) + offset;
}
offset_kernel& operator= (
const offset_kernel& k
)
{
const_cast<T&>(kernel) = k.kernel;
const_cast<scalar_type&>(offset) = k.offset;
return *this;
}
bool operator== (
const offset_kernel& k
) const
{
return k.kernel == kernel && offset == k.offset;
}
};
template <
typename T
>
void serialize (
const offset_kernel<T>& item,
std::ostream& out
)
{
try
{
serialize(item.offset, out);
serialize(item.kernel, out);
}
catch (serialization_error& e)
{
throw serialization_error(e.info + "\n while serializing object of type offset_kernel");
}
}
template <
typename T
>
void deserialize (
offset_kernel<T>& item,
std::istream& in
)
{
typedef typename offset_kernel<T>::scalar_type scalar_type;
try
{
deserialize(const_cast<scalar_type&>(item.offset), in);
deserialize(const_cast<T&>(item.kernel), in);
}
catch (serialization_error& e)
{
throw serialization_error(e.info + "\n while deserializing object of type offset_kernel");
}
}
// ----------------------------------------------------------------------------------------
}
......
......@@ -130,7 +130,7 @@ namespace dlib
!*/
template <
typename K
typename T
>
void deserialize (
radial_basis_kernel<T>& item,
......@@ -171,7 +171,7 @@ namespace dlib
!*/
sigmoid_kernel(
const radial_basis_kernel& k
const sigmoid_kernel& k
);
/*!
ensures
......@@ -236,7 +236,7 @@ namespace dlib
!*/
template <
typename K
typename T
>
void deserialize (
sigmoid_kernel<T>& item,
......@@ -280,7 +280,7 @@ namespace dlib
!*/
polynomial_kernel(
const radial_basis_kernel& k
const polynomial_kernel& k
);
/*!
ensures
......@@ -349,7 +349,7 @@ namespace dlib
!*/
template <
typename K
typename T
>
void deserialize (
polynomial_kernel<T>& item,
......@@ -412,7 +412,7 @@ namespace dlib
!*/
template <
typename K
typename T
>
void deserialize (
linear_kernel<T>& item,
......@@ -422,6 +422,109 @@ namespace dlib
provides deserialization support for linear_kernel
!*/
// ----------------------------------------------------------------------------------------
template <
typename T
>
struct offset_kernel
{
/*!
REQUIREMENTS ON T
T must be a kernel object (e.g. radial_basis_kernel, polynomial_kernel, etc...)
WHAT THIS OBJECT REPRESENTS
This object represents a kernel with a fixed value offset
added to it.
!*/
typedef typename T::scalar_type scalar_type;
typedef typename T::sample_type sample_type;
typedef typename T::mem_manager_type mem_manager_type;
const T kernel;
const scalar_type offset;
offset_kernel(
);
/*!
ensures
- #offset == 0.01
!*/
offset_kernel(
const offset_kernel& k
);
/*!
ensures
- #offset == k.offset
- #kernel == k.kernel
!*/
offset_kernel(
const T& k,
const scalar_type& off
);
/*!
ensures
- #kernel == k
- #offset == off
!*/
scalar_type operator() (
const sample_type& a,
const sample_type& b
) const;
/*!
ensures
- returns kernel(a,b) + offset
!*/
offset_kernel& operator= (
const offset_kernel& k
);
/*!
ensures
- #offset == k.offset
- #kernel == k.kernel
!*/
bool operator== (
const offset_kernel& k
) const;
/*!
ensures
- if (k and *this are identical) then
- returns true
- else
- returns false
!*/
};
template <
typename T
>
void serialize (
const offset_kernel<T>& item,
std::ostream& out
);
/*!
provides serialization support for offset_kernel
!*/
template <
typename T
>
void deserialize (
offset_kernel<T>& item,
std::istream& in
);
/*!
provides deserialization support for offset_kernel
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <
......
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