Added unit tests for the find_k_nearest_neighbors_lsh() method

and related tools.
This commit is contained in:
Davis King 2013-03-14 21:10:08 -04:00
parent 5cee2563a3
commit 3401325107

View File

@ -6,6 +6,7 @@
#include <dlib/svm.h>
#include <dlib/rand.h>
#include <dlib/string.h>
#include <dlib/graph_utils_threaded.h>
#include <vector>
#include <sstream>
#include <ctime>
@ -17,6 +18,63 @@ namespace
using namespace std;
dlib::logger dlog("test.linear_manifold_regularizer");
template <typename hash_type, typename samples_type>
void test_find_k_nearest_neighbors_lsh(
const samples_type& samples
)
{
std::vector<sample_pair> edges1, edges2;
find_k_nearest_neighbors(samples, cosine_distance(), 2, edges1);
find_k_nearest_neighbors_lsh(samples, cosine_distance(), hash_type(), 2, 6, edges2, 2);
std::sort(edges1.begin(), edges1.end(), order_by_index<sample_pair>);
std::sort(edges2.begin(), edges2.end(), order_by_index<sample_pair>);
DLIB_TEST_MSG(edges1.size() == edges2.size(), edges1.size() << " " << edges2.size());
for (unsigned long i = 0; i < edges1.size(); ++i)
{
DLIB_TEST(edges1[i] == edges2[i]);
DLIB_TEST(edges1[i].distance() == edges2[i].distance());
}
}
void test_knn_lsh_sparse()
{
dlib::rand rnd;
std::vector<std::map<unsigned long,double> > samples;
samples.resize(20);
for (unsigned int i = 0; i < samples.size(); ++i)
{
samples[i][0] = rnd.get_random_gaussian();
samples[i][2] = rnd.get_random_gaussian();
}
test_find_k_nearest_neighbors_lsh<hash_similar_angles_64>(samples);
test_find_k_nearest_neighbors_lsh<hash_similar_angles_128>(samples);
test_find_k_nearest_neighbors_lsh<hash_similar_angles_256>(samples);
test_find_k_nearest_neighbors_lsh<hash_similar_angles_512>(samples);
}
void test_knn_lsh_dense()
{
dlib::rand rnd;
std::vector<matrix<double,0,1> > samples;
samples.resize(20);
for (unsigned int i = 0; i < samples.size(); ++i)
{
samples[i].set_size(2);
samples[i](0) = rnd.get_random_gaussian();
samples[i](1) = rnd.get_random_gaussian();
}
test_find_k_nearest_neighbors_lsh<hash_similar_angles_64>(samples);
test_find_k_nearest_neighbors_lsh<hash_similar_angles_128>(samples);
test_find_k_nearest_neighbors_lsh<hash_similar_angles_256>(samples);
test_find_k_nearest_neighbors_lsh<hash_similar_angles_512>(samples);
}
class linear_manifold_regularizer_tester : public tester
{
@ -327,6 +385,8 @@ namespace
}
test_knn1();
test_knn2();
test_knn_lsh_sparse();
test_knn_lsh_dense();
}
};