Commit edd2cb4f authored by Davis King's avatar Davis King

Added a unit test for the discriminant_pca object and also fixed a few minor bugs

and clarified a few things.  Also added the ability to add discriminant_pca objects
together.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403332
parent 3f6e1a6d
......@@ -16,7 +16,7 @@ namespace dlib
// ----------------------------------------------------------------------------------------
template <
typename column_matrix
typename column_matrix_type
>
class discriminant_pca
{
......@@ -56,6 +56,7 @@ namespace dlib
discriminant_pca_error(const std::string& message): error(message) {}
};
typedef column_matrix_type column_matrix;
typedef typename column_matrix::mem_manager_type mem_manager_type;
typedef typename column_matrix::type scalar_type;
typedef typename column_matrix::layout_type layout_type;
......@@ -98,6 +99,14 @@ namespace dlib
scalar_type weight
)
{
// make sure requires clause is not broken
DLIB_ASSERT(weight >= 0,
"\t void discriminant_pca::set_within_class_weight()"
<< "\n\t You can't use negative weight values"
<< "\n\t weight: " << weight
<< "\n\t this: " << this
);
within_weight = weight;
}
......@@ -111,6 +120,14 @@ namespace dlib
scalar_type weight
)
{
// make sure requires clause is not broken
DLIB_ASSERT(weight >= 0,
"\t void discriminant_pca::set_between_class_weight()"
<< "\n\t You can't use negative weight values"
<< "\n\t weight: " << weight
<< "\n\t this: " << this
);
between_weight = weight;
}
......@@ -125,6 +142,20 @@ namespace dlib
const column_matrix& y
)
{
// make sure requires clause is not broken
DLIB_ASSERT(is_col_vector(x) && is_col_vector(y) &&
x.size() == y.size() &&
(in_vector_size() == 0 || x.size() == in_vector_size()),
"\t void discriminant_pca::add_to_within_class_variance()"
<< "\n\t Invalid inputs were given to this function"
<< "\n\t is_col_vector(x): " << is_col_vector(x)
<< "\n\t is_col_vector(y): " << is_col_vector(y)
<< "\n\t x.size(): " << x.size()
<< "\n\t y.size(): " << y.size()
<< "\n\t in_vector_size(): " << in_vector_size()
<< "\n\t this: " << this
);
vect_size = x.size();
if (within_count == 0)
{
......@@ -142,6 +173,20 @@ namespace dlib
const column_matrix& y
)
{
// make sure requires clause is not broken
DLIB_ASSERT(is_col_vector(x) && is_col_vector(y) &&
x.size() == y.size() &&
(in_vector_size() == 0 || x.size() == in_vector_size()),
"\t void discriminant_pca::add_to_between_class_variance()"
<< "\n\t Invalid inputs were given to this function"
<< "\n\t is_col_vector(x): " << is_col_vector(x)
<< "\n\t is_col_vector(y): " << is_col_vector(y)
<< "\n\t x.size(): " << x.size()
<< "\n\t y.size(): " << y.size()
<< "\n\t in_vector_size(): " << in_vector_size()
<< "\n\t this: " << this
);
vect_size = x.size();
if (between_count == 0)
{
......@@ -158,6 +203,16 @@ namespace dlib
const column_matrix& x
)
{
// make sure requires clause is not broken
DLIB_ASSERT(is_col_vector(x) && (in_vector_size() == 0 || x.size() == in_vector_size()),
"\t void discriminant_pca::add_to_total_variance()"
<< "\n\t Invalid inputs were given to this function"
<< "\n\t is_col_vector(x): " << is_col_vector(x)
<< "\n\t in_vector_size(): " << in_vector_size()
<< "\n\t x.size(): " << x.size()
<< "\n\t this: " << this
);
vect_size = x.size();
if (total_count == 0)
{
......@@ -188,6 +243,15 @@ namespace dlib
const double eps = 0.99
) const
{
// make sure requires clause is not broken
DLIB_ASSERT(0 < eps && eps <= 1 && in_vector_size() != 0,
"\t void discriminant_pca::dpca_matrix()"
<< "\n\t Invalid inputs were given to this function"
<< "\n\t eps: " << eps
<< "\n\t in_vector_size(): " << in_vector_size()
<< "\n\t this: " << this
);
general_matrix cov;
// now combine the three measures of variance into a single matrix by using the
......@@ -252,7 +316,7 @@ namespace dlib
swap(between_weight, item.between_weight);
swap(within_cov, item.within_cov);
swap(within_count, item.within_count);
swap(between_weight, item.between_weight);
swap(within_weight, item.within_weight);
}
friend void deserialize (
......@@ -269,7 +333,7 @@ namespace dlib
deserialize( item.between_weight, in);
deserialize( item.within_cov, in);
deserialize( item.within_count, in);
deserialize( item.between_weight, in);
deserialize( item.within_weight, in);
}
friend void serialize (
......@@ -286,7 +350,69 @@ namespace dlib
serialize( item.between_weight, out);
serialize( item.within_cov, out);
serialize( item.within_count, out);
serialize( item.between_weight, out);
serialize( item.within_weight, out);
}
const discriminant_pca operator+ (
const discriminant_pca& item
) const
{
// make sure requires clause is not broken
DLIB_ASSERT((in_vector_size() == 0 || item.in_vector_size() == 0 || in_vector_size() == item.in_vector_size()) &&
between_class_weight() == item.between_class_weight() &&
within_class_weight() == item.within_class_weight(),
"\t discriminant_pca discriminant_pca::operator+()"
<< "\n\t The two discriminant_pca objects being added must have compatible parameters"
<< "\n\t in_vector_size(): " << in_vector_size()
<< "\n\t item.in_vector_size(): " << item.in_vector_size()
<< "\n\t between_class_weight(): " << between_class_weight()
<< "\n\t item.between_class_weight(): " << item.between_class_weight()
<< "\n\t within_class_weight(): " << within_class_weight()
<< "\n\t item.within_class_weight(): " << item.within_class_weight()
<< "\n\t this: " << this
);
discriminant_pca temp(item);
// We need to make sure to ignore empty matrices. That's what these if statements
// are for.
if (total_count != 0 && temp.total_count != 0)
{
temp.total_cov += total_cov;
temp.total_sum += total_sum;
temp.total_count += total_count;
}
else if (total_count != 0)
{
temp.total_cov = total_cov;
temp.total_sum = total_sum;
temp.total_count = total_count;
}
if (between_count != 0 && temp.between_count != 0)
{
temp.between_cov += between_cov;
temp.between_count += between_count;
}
else if (between_count != 0)
{
temp.between_cov = between_cov;
temp.between_count = between_count;
}
if (within_count != 0 && temp.within_count != 0)
{
temp.within_cov += within_cov;
temp.within_count += within_count;
}
else if (within_count != 0)
{
temp.within_cov = within_cov;
temp.within_count = within_count;
}
return temp;
}
private:
......@@ -320,16 +446,16 @@ namespace dlib
general_matrix total_cov;
general_matrix total_sum;
long total_count;
scalar_type total_count;
long vect_size;
general_matrix between_cov;
long between_count;
scalar_type between_count;
scalar_type between_weight;
general_matrix within_cov;
long within_count;
scalar_type within_count;
scalar_type within_weight;
};
......
......@@ -13,12 +13,12 @@ namespace dlib
// ----------------------------------------------------------------------------------------
template <
typename column_matrix
typename column_matrix_type
>
class discriminant_pca
{
/*!
REQUIREMENTS ON column_matrix
REQUIREMENTS ON column_matrix_type
Must be some type of dlib::matrix capable of representing a column vector.
INITIAL VALUE
......@@ -45,8 +45,8 @@ namespace dlib
S = St + a*Sb - b*Sw
Where a and b are user supplied weights. Then the largest eigenvalues of the S matrix are
computed and their associated eigenvectors are returned as the output of this algorithm.
That is, the desired linear dimensionality reduction is given by the transformation matrix
with these eigenvectors stored in its rows.
That is, the desired linear dimensionality reduction is given by the matrix with these
eigenvectors stored in its rows.
Note that if a and b are set to 0 (or no labeled data is provided) then the output transformation
matrix is the same as the one produced by the classical PCA algorithm.
......@@ -60,6 +60,7 @@ namespace dlib
a DPCA matrix.
!*/
typedef column_matrix_type column_matrix;
typedef typename column_matrix::mem_manager_type mem_manager_type;
typedef typename column_matrix::type scalar_type;
typedef typename column_matrix::layout_type layout_type;
......@@ -214,17 +215,20 @@ namespace dlib
- in_vector_size() != 0
(i.e. you have to have given this object some data)
ensures
- #is_col_vector(eigenvalues) == true
- is_col_vector(#eigenvalues) == true
- #dpca_mat.nr() == eigenvalues.size()
- #dpca_mat.nc() == in_vector_size()
- rowm(#dpca_mat,i) represents the ith eigenvector of the S matrix described
in the class description and its eigenvalue is given by eigenvalues(i).
- all values in #eigenvalues are > 0. Moreover, the eigenvalues are in
sorted order with the largest eigenvalue stored at eigenvalues(0).
- (#dpca_mat)*trans(#dpca_mat) == identity_matrix.
(i.e. the rows of the dpca_matrix are all unit length vectors and are mutually
orthogonal)
- Note that #dpca_mat is the desired linear transformation matrix. That is,
multiplying a vector by #dpca_mat performs the desired linear dimensionality
reduction.
- sum(eigenvalues) will be equal to about eps times the total sum of all
- sum(#eigenvalues) will be equal to about eps times the total sum of all
positive eigenvalues in the S matrix described in this class's description.
This means that eps is a number that controls how "lossy" the dimensionality
reduction will be. Large values of eps result in more output dimensions
......@@ -237,6 +241,23 @@ namespace dlib
that prevents this algorithm from working properly.
!*/
const discriminant_pca operator+ (
const discriminant_pca& item
) const;
/*!
requires
- in_vector_size() == 0 || item.in_vector_size() == 0 || in_vector_size() == item.in_vector_size()
(i.e. the in_vector_size() of *this and item must match or one must be zero)
- between_class_weight() == item.between_class_weight()
- within_class_weight() == item.within_class_weight()
ensures
- returns a new discriminant_pca object that represents the combination of all
the measurements given to *this and item. That is, this function returns a
discriminant_pca object, R, that is equivalent to what you would obtain if all
modifying calls (e.g. the add_to_*() functions) to *this and item had instead
been done to R.
!*/
void swap (
discriminant_pca& item
);
......@@ -247,6 +268,8 @@ namespace dlib
};
// ----------------------------------------------------------------------------------------
template <
typename column_matrix
>
......
......@@ -31,6 +31,7 @@ set (tests
conditioning_class.cpp
config_reader.cpp
directed_graph.cpp
discriminant_pca.cpp
empirical_kernel_map.cpp
entropy_coder.cpp
entropy_encoder_model.cpp
......
// Copyright (C) 2009 Davis E. King (davisking@users.sourceforge.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include "tester.h"
#include <dlib/svm.h>
#include <dlib/rand.h>
#include <dlib/string.h>
#include <vector>
#include <sstream>
namespace
{
using namespace test;
using namespace dlib;
using namespace std;
dlib::logger dlog("test.discriminant_pca");
class discriminant_pca_tester : public tester
{
/*!
WHAT THIS OBJECT REPRESENTS
This object represents a unit test. When it is constructed
it adds itself into the testing framework.
!*/
public:
discriminant_pca_tester (
) :
tester (
"test_discriminant_pca", // the command line argument name for this test
"Run tests on the discriminant_pca object.", // the command line argument description
0 // the number of command line arguments for this test
)
{
thetime = time(0);
}
time_t thetime;
dlib::rand::float_1a rnd;
template <typename dpca_type>
void test1()
{
dpca_type dpca, dpca2, dpca3;
DLIB_TEST(dpca.in_vector_size() == 0);
DLIB_TEST(dpca.between_class_weight() == 1);
DLIB_TEST(dpca.within_class_weight() == 1);
// generate a bunch of 4 dimensional vectors and compute the normal PCA transformation matrix
// and just make sure it is a unitary matrix as it should be.
for (int i = 0; i < 5000; ++i)
{
dpca.add_to_total_variance(randm(4,1,rnd));
DLIB_TEST(dpca.in_vector_size() == 4);
}
matrix<double> mat = dpca.dpca_matrix(1);
DLIB_TEST(equal(mat*trans(mat), identity_matrix<double>(4)));
mat = dpca.dpca_matrix(0.9);
DLIB_TEST(equal(mat*trans(mat), identity_matrix<double>(mat.nr())));
matrix<double> eig;
dpca.dpca_matrix(mat, eig, 1);
DLIB_TEST(equal(mat*trans(mat), identity_matrix<double>(4)));
// check that all eigen values are grater than 0
DLIB_TEST(min(eig > 0) == 1);
DLIB_TEST(eig.size() == mat.nr());
DLIB_TEST(is_col_vector(eig));
// check that the eigenvalues are sorted
double last = eig(0);
for (long i = 1; i < eig.size(); ++i)
{
DLIB_TEST(last >= eig(i));
}
dpca.set_within_class_weight(5);
dpca.set_between_class_weight(6);
DLIB_TEST(dpca.in_vector_size() == 4);
DLIB_TEST(dpca.within_class_weight() == 5);
DLIB_TEST(dpca.between_class_weight() == 6);
ostringstream sout;
serialize(dpca, sout);
istringstream sin(sout.str());
deserialize(dpca2, sin);
// now make sure the serialization worked
DLIB_TEST(dpca.in_vector_size() == 4);
DLIB_TEST(dpca.within_class_weight() == 5);
DLIB_TEST(dpca.between_class_weight() == 6);
DLIB_TEST(dpca2.in_vector_size() == 4);
DLIB_TEST(dpca2.within_class_weight() == 5);
DLIB_TEST(dpca2.between_class_weight() == 6);
DLIB_TEST(equal(dpca.dpca_matrix(), dpca2.dpca_matrix()));
DLIB_TEST(equal(mat, dpca2.dpca_matrix(1)));
DLIB_TEST(equal(dpca.dpca_matrix(1), mat));
// now test swap
dpca2.swap(dpca3);
DLIB_TEST(dpca2.in_vector_size() == 0);
DLIB_TEST(dpca2.between_class_weight() == 1);
DLIB_TEST(dpca2.within_class_weight() == 1);
DLIB_TEST(dpca3.in_vector_size() == 4);
DLIB_TEST(dpca3.within_class_weight() == 5);
DLIB_TEST(dpca3.between_class_weight() == 6);
DLIB_TEST(equal(mat, dpca3.dpca_matrix(1)));
DLIB_TEST((dpca3 + dpca3).in_vector_size() == 4);
DLIB_TEST((dpca3 + dpca3).within_class_weight() == 5);
DLIB_TEST((dpca3 + dpca3).between_class_weight() == 6);
dpca.clear();
DLIB_TEST(dpca.in_vector_size() == 0);
DLIB_TEST(dpca.between_class_weight() == 1);
DLIB_TEST(dpca.within_class_weight() == 1);
}
template <typename dpca_type>
void test2()
{
dpca_type dpca, dpca2, dpca3;
typename dpca_type::column_matrix samp1(4), samp2(4);
for (int i = 0; i < 5000; ++i)
{
dpca.add_to_total_variance(randm(4,1,rnd));
DLIB_TEST(dpca.in_vector_size() == 4);
// do this to subtract out the variance along the 3rd axis
samp1 = 0,0,0,0;
samp2 = 0,0,1,0;
dpca.add_to_within_class_variance(samp1, samp2);
}
matrix<double> mat;
dpca.set_within_class_weight(0);
mat = dpca.dpca_matrix(1);
DLIB_TEST(equal(mat*trans(mat), identity_matrix<double>(4)));
DLIB_TEST(dpca.dpca_matrix(1).nr() == 4);
dpca.set_within_class_weight(1000);
DLIB_TEST(dpca.dpca_matrix(1).nr() == 3);
// the 3rd column of the transformation matrix should be all zero since
// we killed all the variation long the 3rd axis
DLIB_TEST(sum(abs(colm(dpca.dpca_matrix(1),2))) < 1e-5);
mat = dpca.dpca_matrix(1);
DLIB_TEST(equal(mat*trans(mat), identity_matrix<double>(3)));
}
template <typename dpca_type>
void test3()
{
dpca_type dpca, dpca2, dpca3;
typename dpca_type::column_matrix samp1(4), samp2(4);
for (int i = 0; i < 5000; ++i)
{
dpca.add_to_total_variance(randm(4,1,rnd));
DLIB_TEST(dpca.in_vector_size() == 4);
// do this to subtract out the variance along the 3rd axis
samp1 = 0,0,0,0;
samp2 = 0,0,1,0;
dpca.add_to_within_class_variance(samp1, samp2);
// do this to subtract out the variance along the 1st axis
samp1 = 0,0,0,0;
samp2 = 1,0,0,0;
dpca.add_to_within_class_variance(samp1, samp2);
}
matrix<double> mat;
dpca.set_within_class_weight(0);
mat = dpca.dpca_matrix(1);
DLIB_TEST(equal(mat*trans(mat), identity_matrix<double>(4)));
DLIB_TEST(dpca.dpca_matrix(1).nr() == 4);
dpca.set_within_class_weight(10000);
DLIB_TEST(dpca.dpca_matrix(1).nr() == 2);
// the 1st and 3rd columns of the transformation matrix should be all zero since
// we killed all the variation long the 1st and 3rd axes
DLIB_TEST(sum(abs(colm(dpca.dpca_matrix(1),2))) < 1e-5);
DLIB_TEST(sum(abs(colm(dpca.dpca_matrix(1),0))) < 1e-5);
mat = dpca.dpca_matrix(1);
DLIB_TEST(equal(mat*trans(mat), identity_matrix<double>(2)));
}
template <typename dpca_type>
void test4()
{
dpca_type dpca, dpca2, dpca3;
dpca_type add_dpca1, add_dpca2, add_dpca3, add_dpca4, sum_dpca;
typename dpca_type::column_matrix samp1(4), samp2(4), samp;
for (int i = 0; i < 5000; ++i)
{
samp = randm(4,1,rnd);
dpca.add_to_total_variance(samp);
add_dpca4.add_to_total_variance(samp);
DLIB_TEST(dpca.in_vector_size() == 4);
// do this to subtract out the variance along the 3rd axis
samp1 = 0,0,0,0;
samp2 = 0,0,1,0;
dpca.add_to_within_class_variance(samp1, samp2);
add_dpca1.add_to_within_class_variance(samp1, samp2);
// do this to subtract out the variance along the 1st axis
samp1 = 0,0,0,0;
samp2 = 1,0,0,0;
dpca.add_to_within_class_variance(samp1, samp2);
add_dpca2.add_to_within_class_variance(samp1, samp2);
// do this to add the variance along the 3rd axis back in
samp1 = 0,0,0,0;
samp2 = 0,0,1,0;
dpca.add_to_between_class_variance(samp1, samp2);
add_dpca3.add_to_between_class_variance(samp1, samp2);
}
matrix<double> mat;
sum_dpca = dpca_type() + dpca_type() + add_dpca1 + dpca_type() + add_dpca2 + add_dpca3 + add_dpca4;
dpca.set_within_class_weight(0);
dpca.set_between_class_weight(0);
sum_dpca.set_within_class_weight(0);
sum_dpca.set_between_class_weight(0);
mat = dpca.dpca_matrix(1);
DLIB_TEST(equal(mat, sum_dpca.dpca_matrix(1)));
DLIB_TEST(equal(mat*trans(mat), identity_matrix<double>(4)));
DLIB_TEST(dpca.dpca_matrix(1).nr() == 4);
dpca.set_within_class_weight(10000);
sum_dpca.set_within_class_weight(10000);
DLIB_TEST(dpca.dpca_matrix(1).nr() == 2);
// the 1st and 3rd columns of the transformation matrix should be all zero since
// we killed all the variation long the 1st and 3rd axes
DLIB_TEST(sum(abs(colm(dpca.dpca_matrix(1),2))) < 1e-4);
DLIB_TEST(sum(abs(colm(dpca.dpca_matrix(1),0))) < 1e-4);
mat = dpca.dpca_matrix(1);
DLIB_TEST(equal(mat*trans(mat), identity_matrix<double>(2)));
DLIB_TEST(equal(mat, sum_dpca.dpca_matrix(1)));
// now add the variance back in using the between class weight
dpca.set_within_class_weight(0);
dpca.set_between_class_weight(1);
mat = dpca.dpca_matrix(1);
DLIB_TEST(equal(mat*trans(mat), identity_matrix<double>(4)));
DLIB_TEST(dpca.dpca_matrix(1).nr() == 4);
dpca.set_within_class_weight (10000);
dpca.set_between_class_weight(100000);
sum_dpca.set_within_class_weight (10000);
sum_dpca.set_between_class_weight(100000);
DLIB_TEST(dpca.dpca_matrix(1).nr() == 3);
// the first column should be all zeros
DLIB_TEST(sum(abs(colm(dpca.dpca_matrix(1),0))) < 1e-5);
mat = dpca.dpca_matrix(1);
DLIB_TEST(equal(mat*trans(mat), identity_matrix<double>(3)));
DLIB_TEST(equal(mat, sum_dpca.dpca_matrix(1)));
}
template <typename dpca_type>
void test5()
{
dpca_type dpca, dpca2;
typename dpca_type::column_matrix samp1(4), samp2(4);
samp1 = 0,0,0,0;
samp2 = 0,0,1,0;
for (int i = 0; i < 5000; ++i)
{
dpca.add_to_between_class_variance(samp1, samp2);
dpca2.add_to_total_variance(samp1);
dpca2.add_to_total_variance(samp2);
}
matrix<double> mat, eig;
dpca.dpca_matrix(mat, eig, 1);
// make sure the eigenvalues come out the way they should for this simple data set
DLIB_TEST(eig.size() == 1);
DLIB_TEST_MSG(abs(eig(0) - 1) < 1e-10, abs(eig(0) - 1));
dpca2.dpca_matrix(mat, eig, 1);
// make sure the eigenvalues come out the way they should for this simple data set
DLIB_TEST(eig.size() == 1);
DLIB_TEST(abs(eig(0) - 0.25) < 1e-10);
}
void perform_test (
)
{
++thetime;
typedef matrix<double,0,1> sample_type;
typedef discriminant_pca<sample_type> dpca_type;
dlog << LINFO << "time seed: " << thetime;
rnd.set_seed(cast_to_string(thetime));
test5<dpca_type>();
for (int i = 0; i < 10; ++i)
{
print_spinner();
test1<dpca_type>();
print_spinner();
test2<dpca_type>();
print_spinner();
test3<dpca_type>();
print_spinner();
test4<dpca_type>();
}
}
};
// Create an instance of this object. Doing this causes this test
// to be automatically inserted into the testing framework whenever this cpp file
// is linked into the project. Note that since we are inside an unnamed-namespace
// we won't get any linker errors about the symbol a being defined multple times.
discriminant_pca_tester a;
}
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