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
template <
typename K,
typename stop_strategy_type
typename stop_strategy_type,
typename T
>
distance_function<K> approximate_distance_function (
stop_strategy_type stop_strategy,
const distance_function<K>& target,
const distance_function<K>& starting_point
const T& starting_basis
)
{
// make sure requires clause is not broken
DLIB_ASSERT(target.get_basis_vectors().size() > 0 &&
starting_point.get_basis_vectors().size() > 0 &&
target.get_kernel() == starting_point.get_kernel(),
starting_basis.size() > 0,
"\t distance_function approximate_distance_function()"
<< "\n\t Invalid inputs were given to this function."
<< "\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 target.kernel_function == starting_point.kernel_function: " << (target.get_kernel() == starting_point.get_kernel())
<< "\n\t target.get_basis_vectors().size(): " << target.get_basis_vectors().size()
<< "\n\t starting_basis.size(): " << starting_basis.size()
);
using namespace red_impl;
// 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
// see page 554 of Learning with kernels by Scholkopf and Smola where they talk
// about "Optimal Expansion Coefficients."
......@@ -437,9 +436,9 @@ namespace dlib
matrix<scalar_type,0,1,mem_manager_type> beta;
// Now we compute the fist approximate distance function.
beta = pinv(kernel_matrix(kern,starting_point.get_basis_vectors())) *
(kernel_matrix(kern,starting_point.get_basis_vectors(),target.get_basis_vectors())*target.get_alpha());
matrix<sample_type,0,1,mem_manager_type> out_vectors(starting_point.get_basis_vectors());
beta = pinv(kernel_matrix(kern,starting_basis)) *
(kernel_matrix(kern,starting_basis,target.get_basis_vectors())*target.get_alpha());
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
......@@ -552,10 +551,9 @@ namespace dlib
linearly_independent_subset_finder<kernel_type> lisf(kern, num_bv);
fill_lisf(lisf,x);
distance_function<kernel_type> approx(ones_matrix<scalar_type>(lisf.size(),1), kern, vector_to_matrix(lisf));
const distance_function<kernel_type> target = dec_funct;
approx = approximate_distance_function(objective_delta_stop_strategy(eps), target, approx);
distance_function<kernel_type> approx, target;
target = dec_funct;
approx = approximate_distance_function(objective_delta_stop_strategy(eps), target, lisf);
decision_function<kernel_type> new_df(approx.get_alpha(),
0,
......
......@@ -110,34 +110,40 @@ namespace dlib
template <
typename K,
typename stop_strategy_type
typename stop_strategy_type,
typename T
>
distance_function<K> approximate_distance_function (
stop_strategy_type stop_strategy,
const distance_function<K>& target,
const distance_function<K>& starting_point
const T& starting_basis
);
/*!
requires
- stop_strategy == an object that defines a stop strategy such as one of
the objects from dlib/optimization/optimization_stop_strategies_abstract.h
- target.get_basis_vectors().size() > 0 && starting_point.get_basis_vectors().size() > 0
(i.e. target and starting_point have to have some basis vectors in them)
- target.get_kernel() == starting_point.get_kernel()
(i.e. both distance functions must use the same kernel)
- requirements on starting_basis
- T must be a dlib::matrix type or something convertible to a matrix via vector_to_matrix()
(e.g. a std::vector). Additionally, starting_basis must contain K::sample_type
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
(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
distance_functions must be column vectors.
- K::sample_type must be a dlib::matrix object and the basis_vectors inside target
and starting_basis must be column vectors.
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
minimized. The optimization begins with the initial guess contained in
starting_point and searches for an X which locally minimizes target(X). Since
this problem can have many local minima the quality of the starting point
can significantly influence the results.
- The returned distance_function will contain the same number of basis vectors
as the given starting_point object.
minimized. The optimization begins with an X in the span of the elements
of starting_basis and searches for an X which locally minimizes target(X).
Since this problem can have many local minima, the quality of the starting
basis can significantly influence the results.
- The optimization is over all variables in a distance_function, however,
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
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