Commit 8837d626 authored by Davis King's avatar Davis King

Added tests for the new features of the distance_function object.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%404129
parent f5801c15
......@@ -79,15 +79,81 @@ namespace
const sample_type samp1 = samples[rnd.get_random_32bit_number()%samples.size()];
const sample_type samp2 = samples[rnd.get_random_32bit_number()%samples.size()];
matrix<double,0,1> proj1 = ekm.project(samp1);
matrix<double,0,1> proj2 = ekm.project(samp2);
const matrix<double,0,1> proj1 = ekm.project(samp1);
const matrix<double,0,1> proj2 = ekm.project(samp2);
distance_function<kernel_type> df1 = ekm.convert_to_distance_function(proj1);
distance_function<kernel_type> df2 = ekm.convert_to_distance_function(proj2);
DLIB_TEST(df1.get_kernel() == kern);
DLIB_TEST(df2.get_kernel() == kern);
// make sure the norms are correct
DLIB_TEST(std::abs(df1.get_squared_norm() -
trans(df1.get_alpha())*kernel_matrix(df1.get_kernel(),df1.get_basis_vectors())*df1.get_alpha()) < 1e-10);
DLIB_TEST(std::abs(df2.get_squared_norm() -
trans(df2.get_alpha())*kernel_matrix(df2.get_kernel(),df2.get_basis_vectors())*df2.get_alpha()) < 1e-10);
const double true_dist = std::sqrt(kern(samp1,samp1) + kern(samp2,samp2) - 2*kern(samp1,samp2));
DLIB_TEST_MSG(abs(df1(df2) - true_dist) < 1e-7, abs(df1(df2) - true_dist));
DLIB_TEST_MSG(abs(length(proj1-proj2) - true_dist) < 1e-7, abs(length(proj1-proj2) - true_dist));
// test distance function operators
const decision_function<kernel_type> dec1 = ekm.convert_to_decision_function(proj1);
const decision_function<kernel_type> dec2 = ekm.convert_to_decision_function(proj2);
DLIB_TEST(dec1.kernel_function == kern);
DLIB_TEST(dec2.kernel_function == kern);
distance_function<kernel_type> temp;
temp = dec1;
DLIB_TEST(std::abs(temp.get_squared_norm() - df1.get_squared_norm()) < 1e-10);
temp = dec2;
DLIB_TEST(std::abs(temp.get_squared_norm() - df2.get_squared_norm()) < 1e-10);
temp = distance_function<kernel_type>(dec1.alpha, dec1.kernel_function, dec1.basis_vectors);
DLIB_TEST(std::abs(temp.get_squared_norm() - df1.get_squared_norm()) < 1e-10);
df1 = dec1;
temp = df1 + df2;
decision_function<kernel_type> dec3(temp.get_alpha(), 0, temp.get_kernel(), temp.get_basis_vectors());
DLIB_TEST(std::abs(temp.get_squared_norm() -
trans(temp.get_alpha())*kernel_matrix(temp.get_kernel(),temp.get_basis_vectors())*temp.get_alpha()) < 1e-10);
for (unsigned long j = 0; j < samples.size(); ++j)
{
DLIB_TEST(std::abs(dec3(samples[j]) - (dec1(samples[j]) + dec2(samples[j]))) < 1e-10);
}
temp = df1 - df2;
dec3 = decision_function<kernel_type>(temp.get_alpha(), 0, temp.get_kernel(), temp.get_basis_vectors());
DLIB_TEST(std::abs(temp.get_squared_norm() -
trans(temp.get_alpha())*kernel_matrix(temp.get_kernel(),temp.get_basis_vectors())*temp.get_alpha()) < 1e-10);
for (unsigned long j = 0; j < samples.size(); ++j)
{
DLIB_TEST(std::abs(dec3(samples[j]) - (dec1(samples[j]) - dec2(samples[j]))) < 1e-10);
}
temp = 3*(df1 - df2)*2;
dec3 = decision_function<kernel_type>(temp.get_alpha(), 0, temp.get_kernel(), temp.get_basis_vectors());
DLIB_TEST(std::abs(temp.get_squared_norm() -
trans(temp.get_alpha())*kernel_matrix(temp.get_kernel(),temp.get_basis_vectors())*temp.get_alpha()) < 1e-10);
for (unsigned long j = 0; j < samples.size(); ++j)
{
DLIB_TEST(std::abs(dec3(samples[j]) - 6*(dec1(samples[j]) - dec2(samples[j]))) < 1e-10);
}
distance_function<kernel_type> df_empty(kern);
temp = df_empty + (df1 + df2)/2 + df_empty - df_empty + (df_empty + df_empty) - (df_empty - df_empty);
dec3 = decision_function<kernel_type>(temp.get_alpha(), 0, temp.get_kernel(), temp.get_basis_vectors());
DLIB_TEST(std::abs(temp.get_squared_norm() -
trans(temp.get_alpha())*kernel_matrix(temp.get_kernel(),temp.get_basis_vectors())*temp.get_alpha()) < 1e-10);
for (unsigned long j = 0; j < samples.size(); ++j)
{
DLIB_TEST(std::abs(dec3(samples[j]) - 0.5*(dec1(samples[j]) + dec2(samples[j]))) < 1e-10);
}
}
// Do some sanity tests on the conversion to distance functions while we are at it. This
// time multiply one of the projections by 30 and see that it still all works out right.
......
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