Commit e7ac1871 authored by Davis King's avatar Davis King

Made the interface to the approximate_distance_function() a little cleaner and

improved its specification a bit.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%404127
parent 9e5ce968
...@@ -403,28 +403,27 @@ namespace dlib ...@@ -403,28 +403,27 @@ namespace dlib
template < template <
typename K, typename K,
typename stop_strategy_type typename stop_strategy_type,
typename T
> >
distance_function<K> approximate_distance_function ( distance_function<K> approximate_distance_function (
stop_strategy_type stop_strategy, stop_strategy_type stop_strategy,
const distance_function<K>& target, const distance_function<K>& target,
const distance_function<K>& starting_point const T& starting_basis
) )
{ {
// make sure requires clause is not broken // make sure requires clause is not broken
DLIB_ASSERT(target.get_basis_vectors().size() > 0 && DLIB_ASSERT(target.get_basis_vectors().size() > 0 &&
starting_point.get_basis_vectors().size() > 0 && starting_basis.size() > 0,
target.get_kernel() == starting_point.get_kernel(),
"\t distance_function approximate_distance_function()" "\t distance_function approximate_distance_function()"
<< "\n\t Invalid inputs were given to this function." << "\n\t Invalid inputs were given to this function."
<< "\n\t target.get_basis_vectors().size(): " << target.get_basis_vectors().size() << "\n\t target.get_basis_vectors().size(): " << target.get_basis_vectors().size()
<< "\n\t starting_point.get_basis_vectors().size(): " << starting_point.get_basis_vectors().size() << "\n\t starting_basis.size(): " << starting_basis.size()
<< "\n\t target.kernel_function == starting_point.kernel_function: " << (target.get_kernel() == starting_point.get_kernel())
); );
using namespace red_impl; using namespace red_impl;
// The next few statements just find the best weights with which to approximate // The next few statements just find the best weights with which to approximate
// the target object with the set of vectors in the starting_point object. This // the target object with the set of basis vectors in starting_basis. This
// is really just a simple application of some linear algebra. For the details // is really just a simple application of some linear algebra. For the details
// see page 554 of Learning with kernels by Scholkopf and Smola where they talk // see page 554 of Learning with kernels by Scholkopf and Smola where they talk
// about "Optimal Expansion Coefficients." // about "Optimal Expansion Coefficients."
...@@ -437,9 +436,9 @@ namespace dlib ...@@ -437,9 +436,9 @@ namespace dlib
matrix<scalar_type,0,1,mem_manager_type> beta; matrix<scalar_type,0,1,mem_manager_type> beta;
// Now we compute the fist approximate distance function. // Now we compute the fist approximate distance function.
beta = pinv(kernel_matrix(kern,starting_point.get_basis_vectors())) * beta = pinv(kernel_matrix(kern,starting_basis)) *
(kernel_matrix(kern,starting_point.get_basis_vectors(),target.get_basis_vectors())*target.get_alpha()); (kernel_matrix(kern,starting_basis,target.get_basis_vectors())*target.get_alpha());
matrix<sample_type,0,1,mem_manager_type> out_vectors(starting_point.get_basis_vectors()); matrix<sample_type,0,1,mem_manager_type> out_vectors(vector_to_matrix(starting_basis));
// Now setup to do a global optimization of all the parameters in the approximate // Now setup to do a global optimization of all the parameters in the approximate
...@@ -552,10 +551,9 @@ namespace dlib ...@@ -552,10 +551,9 @@ namespace dlib
linearly_independent_subset_finder<kernel_type> lisf(kern, num_bv); linearly_independent_subset_finder<kernel_type> lisf(kern, num_bv);
fill_lisf(lisf,x); fill_lisf(lisf,x);
distance_function<kernel_type> approx(ones_matrix<scalar_type>(lisf.size(),1), kern, vector_to_matrix(lisf)); distance_function<kernel_type> approx, target;
const distance_function<kernel_type> target = dec_funct; target = dec_funct;
approx = approximate_distance_function(objective_delta_stop_strategy(eps), target, lisf);
approx = approximate_distance_function(objective_delta_stop_strategy(eps), target, approx);
decision_function<kernel_type> new_df(approx.get_alpha(), decision_function<kernel_type> new_df(approx.get_alpha(),
0, 0,
......
...@@ -110,34 +110,40 @@ namespace dlib ...@@ -110,34 +110,40 @@ namespace dlib
template < template <
typename K, typename K,
typename stop_strategy_type typename stop_strategy_type,
typename T
> >
distance_function<K> approximate_distance_function ( distance_function<K> approximate_distance_function (
stop_strategy_type stop_strategy, stop_strategy_type stop_strategy,
const distance_function<K>& target, const distance_function<K>& target,
const distance_function<K>& starting_point const T& starting_basis
); );
/*! /*!
requires requires
- stop_strategy == an object that defines a stop strategy such as one of - stop_strategy == an object that defines a stop strategy such as one of
the objects from dlib/optimization/optimization_stop_strategies_abstract.h the objects from dlib/optimization/optimization_stop_strategies_abstract.h
- target.get_basis_vectors().size() > 0 && starting_point.get_basis_vectors().size() > 0 - requirements on starting_basis
(i.e. target and starting_point have to have some basis vectors in them) - T must be a dlib::matrix type or something convertible to a matrix via vector_to_matrix()
- target.get_kernel() == starting_point.get_kernel() (e.g. a std::vector). Additionally, starting_basis must contain K::sample_type
(i.e. both distance functions must use the same kernel) objects which can be supplied to the kernel function used by target.
- is_vector(starting_basis) == true
- starting_basis.size() > 0
- target.get_basis_vectors().size() > 0
- kernel_derivative<K> is defined - kernel_derivative<K> is defined
(i.e. The analytic derivative for the given kernel must be defined) (i.e. The analytic derivative for the given kernel must be defined)
- K::sample_type must be a dlib::matrix object and the basis_vectors inside the - K::sample_type must be a dlib::matrix object and the basis_vectors inside target
distance_functions must be column vectors. and starting_basis must be column vectors.
ensures ensures
- This function attempts to find a distance function object which is close - This routine attempts to find a distance_function object which is close
to the given target. That is, it searches for an X such that target(X) is to the given target. That is, it searches for an X such that target(X) is
minimized. The optimization begins with the initial guess contained in minimized. The optimization begins with an X in the span of the elements
starting_point and searches for an X which locally minimizes target(X). Since of starting_basis and searches for an X which locally minimizes target(X).
this problem can have many local minima the quality of the starting point Since this problem can have many local minima, the quality of the starting
can significantly influence the results. basis can significantly influence the results.
- The returned distance_function will contain the same number of basis vectors - The optimization is over all variables in a distance_function, however,
as the given starting_point object. the size of the basis set is constrained to no more than starting_basis.size().
That is, in the returned distance_function DF, we will have:
- DF.get_basis_vectors().size() <= starting_basis.size()
- The optimization is carried out until the stop_strategy indicates it - The optimization is carried out until the stop_strategy indicates it
should stop. should stop.
!*/ !*/
......
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