Commit 00325e75 authored by Davis King's avatar Davis King

Added the svm_one_class_trainer object.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%404013
parent 2d113cd0
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_SVm_ONE_CLASS_TRAINER_H__
#define DLIB_SVm_ONE_CLASS_TRAINER_H__
#include "svm_one_class_trainer_abstract.h"
#include <cmath>
#include <limits>
#include <sstream>
#include "../matrix.h"
#include "../algs.h"
#include "function.h"
#include "kernel.h"
#include "../optimization/optimization_solve_qp3_using_smo.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <
typename K
>
class svm_one_class_trainer
{
public:
typedef K kernel_type;
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;
typedef decision_function<kernel_type> trained_function_type;
svm_one_class_trainer (
) :
nu(0.1),
cache_size(200),
eps(0.001)
{
}
svm_one_class_trainer (
const kernel_type& kernel_,
const scalar_type& nu_
) :
kernel_function(kernel_),
nu(nu_),
cache_size(200),
eps(0.001)
{
// make sure requires clause is not broken
DLIB_ASSERT(0 < nu && nu <= 1,
"\tsvm_one_class_trainer::svm_one_class_trainer(kernel,nu)"
<< "\n\t invalid inputs were given to this function"
<< "\n\t nu: " << nu
);
}
void set_cache_size (
long cache_size_
)
{
// make sure requires clause is not broken
DLIB_ASSERT(cache_size_ > 0,
"\tvoid svm_one_class_trainer::set_cache_size(cache_size_)"
<< "\n\t invalid inputs were given to this function"
<< "\n\t cache_size: " << cache_size_
);
cache_size = cache_size_;
}
long get_cache_size (
) const
{
return cache_size;
}
void set_epsilon (
scalar_type eps_
)
{
// make sure requires clause is not broken
DLIB_ASSERT(eps_ > 0,
"\tvoid svm_one_class_trainer::set_epsilon(eps_)"
<< "\n\t invalid inputs were given to this function"
<< "\n\t eps: " << eps_
);
eps = eps_;
}
const scalar_type get_epsilon (
) const
{
return eps;
}
void set_kernel (
const kernel_type& k
)
{
kernel_function = k;
}
const kernel_type& get_kernel (
) const
{
return kernel_function;
}
void set_nu (
scalar_type nu_
)
{
// make sure requires clause is not broken
DLIB_ASSERT(0 < nu_ && nu_ <= 1,
"\tvoid svm_one_class_trainer::set_nu(nu_)"
<< "\n\t invalid inputs were given to this function"
<< "\n\t nu: " << nu_
);
nu = nu_;
}
const scalar_type get_nu (
) const
{
return nu;
}
template <
typename in_sample_vector_type
>
const decision_function<kernel_type> train (
const in_sample_vector_type& x
) const
{
return do_train(vector_to_matrix(x));
}
void swap (
svm_one_class_trainer& item
)
{
exchange(kernel_function, item.kernel_function);
exchange(nu, item.nu);
exchange(cache_size, item.cache_size);
exchange(eps, item.eps);
}
private:
// ------------------------------------------------------------------------------------
template <
typename in_sample_vector_type
>
const decision_function<kernel_type> do_train (
const in_sample_vector_type& x
) const
{
typedef typename K::scalar_type scalar_type;
typedef typename decision_function<K>::sample_vector_type sample_vector_type;
typedef typename decision_function<K>::scalar_vector_type scalar_vector_type;
// make sure requires clause is not broken
DLIB_ASSERT(is_col_vector(x) && x.size() > 0,
"\tdecision_function svm_one_class_trainer::train(x)"
<< "\n\t invalid inputs were given to this function"
<< "\n\t x.nr(): " << x.nr()
<< "\n\t x.nc(): " << x.nc()
);
scalar_vector_type alpha;
solve_qp3_using_smo<scalar_vector_type> solver;
solver(symmetric_matrix_cache<float>(kernel_matrix(kernel_function,x), cache_size),
zeros_matrix<scalar_type>(x.size(),1),
ones_matrix<scalar_type>(x.size(),1),
nu*x.size(),
1,
1,
alpha,
eps);
scalar_type rho;
calculate_rho(alpha,solver.get_gradient(),rho);
// count the number of support vectors
const long sv_count = (long)sum(alpha != 0);
scalar_vector_type sv_alpha;
sample_vector_type support_vectors;
// size these column vectors so that they have an entry for each support vector
sv_alpha.set_size(sv_count);
support_vectors.set_size(sv_count);
// load the support vectors and their alpha values into these new column matrices
long idx = 0;
for (long i = 0; i < alpha.nr(); ++i)
{
if (alpha(i) != 0)
{
sv_alpha(idx) = alpha(i);
support_vectors(idx) = x(i);
++idx;
}
}
// now return the decision function
return decision_function<K> (sv_alpha, rho, kernel_function, support_vectors);
}
// ------------------------------------------------------------------------------------
template <
typename scalar_vector_type
>
void calculate_rho(
const scalar_vector_type& alpha,
const scalar_vector_type& df,
scalar_type& rho
) const
{
using namespace std;
long num_p_free = 0;
scalar_type sum_p_free = 0;
scalar_type upper_bound_p;
scalar_type lower_bound_p;
find_min_and_max(df, upper_bound_p, lower_bound_p);
for(long i = 0; i < alpha.nr(); ++i)
{
if(alpha(i) == 1)
{
if (df(i) > upper_bound_p)
upper_bound_p = df(i);
}
else if(alpha(i) == 0)
{
if (df(i) < lower_bound_p)
lower_bound_p = df(i);
}
else
{
++num_p_free;
sum_p_free += df(i);
}
}
scalar_type r1;
if(num_p_free > 0)
r1 = sum_p_free/num_p_free;
else
r1 = (upper_bound_p+lower_bound_p)/2;
rho = r1;
}
kernel_type kernel_function;
scalar_type nu;
long cache_size;
scalar_type eps;
}; // end of class svm_one_class_trainer
// ----------------------------------------------------------------------------------------
template <typename K>
void swap (
svm_one_class_trainer<K>& a,
svm_one_class_trainer<K>& b
) { a.swap(b); }
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_SVm_ONE_CLASS_TRAINER_H__
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_SVm_ONE_CLASS_TRAINER_ABSTRACT_
#ifdef DLIB_SVm_ONE_CLASS_TRAINER_ABSTRACT_
#include <cmath>
#include <limits>
#include "../matrix/matrix_abstract.h"
#include "../algs.h"
#include "function_abstract.h"
#include "kernel_abstract.h"
#include "../optimization/optimization_solve_qp3_using_smo_abstract.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <
typename K
>
class svm_one_class_trainer
{
/*!
REQUIREMENTS ON K
is a kernel function object as defined in dlib/svm/kernel_abstract.h
WHAT THIS OBJECT REPRESENTS
This object implements a trainer for a support vector machine for
solving one-class classification problems. It is implemented using the SMO
algorithm.
The implementation of the training algorithm used by this object is based
on the following excellent paper:
- Chih-Chung Chang and Chih-Jen Lin, LIBSVM : a library for support vector
machines, 2001. Software available at http://www.csie.ntu.edu.tw/~cjlin/libsvm
!*/
public:
typedef K kernel_type;
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;
typedef decision_function<kernel_type> trained_function_type;
svm_one_class_trainer (
);
/*!
ensures
- This object is properly initialized and ready to be used
to train a support vector machine.
- #get_nu() == 0.1
- #get_cache_size() == 200
- #get_epsilon() == 0.001
!*/
svm_one_class_trainer (
const kernel_type& kernel,
const scalar_type& nu
);
/*!
requires
- 0 < nu <= 1
ensures
- This object is properly initialized and ready to be used
to train a support vector machine.
- #get_kernel() == kernel
- #get_nu() == nu
- #get_cache_size() == 200
- #get_epsilon() == 0.001
!*/
void set_cache_size (
long cache_size
);
/*!
requires
- cache_size > 0
ensures
- #get_cache_size() == cache_size
!*/
const long get_cache_size (
) const;
/*!
ensures
- returns the number of megabytes of cache this object will use
when it performs training via the this->train() function.
(bigger values of this may make training go faster but won't affect
the result. However, too big a value will cause you to run out of
memory, obviously.)
!*/
void set_epsilon (
scalar_type eps
);
/*!
requires
- eps > 0
ensures
- #get_epsilon() == eps
!*/
const scalar_type get_epsilon (
) const;
/*!
ensures
- returns the error epsilon that determines when training should stop.
Generally a good value for this is 0.001. Smaller values may result
in a more accurate solution but take longer to execute.
!*/
void set_kernel (
const kernel_type& k
);
/*!
ensures
- #get_kernel() == k
!*/
const kernel_type& get_kernel (
) const;
/*!
ensures
- returns a copy of the kernel function in use by this object
!*/
void set_nu (
scalar_type nu
);
/*!
requires
- 0 < nu <= 1
ensures
- #get_nu() == nu
!*/
const scalar_type get_nu (
) const;
/*!
ensures
- returns the nu svm parameter. This is a value between 0 and
1. It is the parameter that determines the trade off between
trying to fit the training data exactly or allowing more errors
but hopefully improving the generalization ability of the
resulting classifier. Smaller values encourage exact fitting
while larger values of nu may encourage better generalization.
For more information you should consult the papers referenced
above.
!*/
template <
typename in_sample_vector_type
>
const decision_function<kernel_type> train (
const in_sample_vector_type& x
) const;
/*!
requires
- x.size() > 0
- is_col_vector(x) == true
- x == a matrix or something convertible to a matrix via vector_to_matrix().
Also, x should contain sample_type objects.
ensures
- trains a one-class support vector classifier given the training samples in x.
Training is done when the error is less than get_epsilon().
- returns a decision function F with the following properties:
- if (new_x is a sample predicted to arise from the distribution
which generated the training samples) then
- F(new_x) >= 0
- else
- F(new_x) < 0
!*/
void swap (
svm_one_class_trainer& item
);
/*!
ensures
- swaps *this and item
!*/
};
template <typename K>
void swap (
svm_one_class_trainer<K>& a,
svm_one_class_trainer<K>& b
) { a.swap(b); }
/*!
provides a global swap
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_SVm_ONE_CLASS_TRAINER_ABSTRACT_
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