- Added the ability to get the kernel and inverse kernel matrices out of a

linearly_independent_subset_finder.   I also made the add() function return
   a bool that tells you if it added its argument into the dictionary set
   or not.
 - Added a version of empirical_kernel_map::load() that accepts
   linearly_independent_subset_finder objects.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403615
This commit is contained in:
Davis King 2010-05-15 16:12:25 +00:00
parent 853f8459ed
commit e1af953e25
4 changed files with 85 additions and 2 deletions

View File

@ -5,6 +5,7 @@
#include "../matrix.h" #include "../matrix.h"
#include "empirical_kernel_map_abstract.h" #include "empirical_kernel_map_abstract.h"
#include "linearly_independent_subset_finder.h"
#include <vector> #include <vector>
#include "../algs.h" #include "../algs.h"
#include "kernel_matrix.h" #include "kernel_matrix.h"
@ -71,6 +72,25 @@ namespace dlib
load_impl(kernel_, vector_to_matrix(basis_samples)); load_impl(kernel_, vector_to_matrix(basis_samples));
} }
void load(
const linearly_independent_subset_finder<kernel_type>& lisf
)
{
// make sure requires clause is not broken
DLIB_ASSERT(lisf.dictionary_size() > 0,
"\tvoid empirical_kernel_map::load(linearly_independent_subset_finder)"
<< "\n\t You have to give a non-empty set of basis_samples"
<< "\n\t this: " << this
);
kernel = lisf.get_kernel();
weights = trans(chol(lisf.get_inv_kernel_marix()));
basis.resize(lisf.dictionary_size());
for (unsigned long i = 0; i < basis.size(); ++i)
basis[i] = lisf[i];
}
const kernel_type get_kernel ( const kernel_type get_kernel (
) const ) const
{ {

View File

@ -7,6 +7,7 @@
#include "../matrix.h" #include "../matrix.h"
#include "kernel_abstract.h" #include "kernel_abstract.h"
#include "function_abstract.h" #include "function_abstract.h"
#include "linearly_independent_subset_finder_abstract.h"
#include <vector> #include <vector>
namespace dlib namespace dlib
@ -142,6 +143,27 @@ namespace dlib
If this happens then this object will revert back to its initial value. If this happens then this object will revert back to its initial value.
!*/ !*/
void load(
const linearly_independent_subset_finder<kernel_type>& lisf
);
/*!
requires
- lisf.dictionary_size() > 0
ensures
- #out_vector_size() == lisf.dictionary_size()
- #get_kernel() == lisf.get_kernel()
- Uses the dictionary vectors from lisf as a basis set. Thus, this function
constructs a map between normal sample_type objects and the subspace of
the kernel feature space defined by the given kernel and the given set
of basis samples. So after this function has been called you will be
able to project sample_type objects into kernel feature space and obtain
the resulting vector as a regular column matrix.
throws
- empirical_kernel_map_error
This exception is thrown if we are unable to create a kernel map.
If this happens then this object will revert back to its initial value.
!*/
const kernel_type get_kernel ( const kernel_type get_kernel (
) const; ) const;
/*! /*!

View File

@ -108,7 +108,7 @@ namespace dlib
K.set_size(0,0); K.set_size(0,0);
} }
void add ( bool add (
const sample_type& x const sample_type& x
) )
{ {
@ -126,7 +126,9 @@ namespace dlib
K(0,0) = kx; K(0,0) = kx;
dictionary.push_back(x); dictionary.push_back(x);
return true;
} }
return false;
} }
else else
{ {
@ -220,6 +222,11 @@ namespace dlib
dictionary.push_back(x); dictionary.push_back(x);
} }
return true;
}
else
{
return false;
} }
} }
} }
@ -285,6 +292,18 @@ namespace dlib
return dictionary[index]; return dictionary[index];
} }
const matrix<scalar_type,0,0,mem_manager_type>& get_kernel_matrix (
) const
{
return K;
}
const matrix<scalar_type,0,0,mem_manager_type>& get_inv_kernel_marix (
) const
{
return K_inv;
}
private: private:
typedef std_allocator<sample_type, mem_manager_type> alloc_sample_type; typedef std_allocator<sample_type, mem_manager_type> alloc_sample_type;

View File

@ -112,19 +112,22 @@ namespace dlib
- clears out all the data (e.g. #dictionary_size() == 0) - clears out all the data (e.g. #dictionary_size() == 0)
!*/ !*/
void add ( bool add (
const sample_type& x const sample_type& x
); );
/*! /*!
ensures ensures
- if (x is linearly independent of the vectors already in this object) then - if (x is linearly independent of the vectors already in this object) then
- adds x into the dictionary - adds x into the dictionary
- returns true
- if (dictionary_size() < max_dictionary_size()) then - if (dictionary_size() < max_dictionary_size()) then
- #dictionary_size() == dictionary_size() + 1 - #dictionary_size() == dictionary_size() + 1
- else - else
- #dictionary_size() == dictionary_size() - #dictionary_size() == dictionary_size()
(i.e. the number of vectors in this object doesn't change) (i.e. the number of vectors in this object doesn't change)
- the least linearly independent vector in this object is removed - the least linearly independent vector in this object is removed
- else
- returns false
!*/ !*/
void swap ( void swap (
@ -161,6 +164,25 @@ namespace dlib
vectors in this object. vectors in this object.
!*/ !*/
const matrix<scalar_type,0,0,mem_manager_type>& get_kernel_matrix (
) const;
/*!
ensures
- returns a matrix K such that:
- K.nr() == K.nc() == dictionary_size()
- K == kernel_matrix(get_kernel(), get_dictionary())
i.e. K == the kernel matrix for the dictionary vectors
!*/
const matrix<scalar_type,0,0,mem_manager_type>& get_inv_kernel_marix (
) const;
/*!
ensures
- if (dictionary_size() != 0)
- returns inv(get_kernel_matrix())
- else
- returns an empty matrix
!*/
}; };
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------