summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/test/linear_manifold_regularizer.cpp
blob: e73b1c8d323d06294edc7dbe2f22be838e38b8cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright (C) 2010  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.

#include "tester.h"
#include <dlib/manifold_regularization.h>
#include <dlib/svm.h>
#include <dlib/rand.h>
#include <dlib/string.h>
#include <dlib/graph_utils_threaded.h>
#include <vector>
#include <sstream>
#include <ctime>

namespace  
{
    using namespace test;
    using namespace dlib;
    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_MSG(std::abs(edges1[i].distance() - edges2[i].distance()) < 1e-7,
                edges1[i].distance() - edges2[i].distance());
        }
    }

    template <typename scalar_type>
    void test_knn_lsh_sparse()
    {
        dlib::rand rnd;
        std::vector<std::map<unsigned long,scalar_type> > 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);
    }

    template <typename scalar_type>
    void test_knn_lsh_dense()
    {
        dlib::rand rnd;
        std::vector<matrix<scalar_type,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
    {
        /*!
            WHAT THIS OBJECT REPRESENTS
                This object represents a unit test.  When it is constructed
                it adds itself into the testing framework.
        !*/
    public:
        linear_manifold_regularizer_tester (
        ) :
            tester (
                "test_linear_manifold_regularizer",       // the command line argument name for this test
                "Run tests on the linear_manifold_regularizer object.", // the command line argument description
                0                     // the number of command line arguments for this test
            )
        {
            seed = 1;
        }

        dlib::rand rnd;

        unsigned long seed;

        typedef matrix<double, 0, 1> sample_type;
        typedef radial_basis_kernel<sample_type> kernel_type;

        void do_the_test()
        {
            print_spinner();
            std::vector<sample_type> samples;

            // Declare an instance of the kernel we will be using.  
            const kernel_type kern(0.1);

            const unsigned long num_points = 200;

            // create a large dataset with two concentric circles.  
            generate_circle(samples, 1, num_points);  // circle of radius 1
            generate_circle(samples, 5, num_points);  // circle of radius 5

            std::vector<sample_pair> edges;
            find_percent_shortest_edges_randomly(samples, squared_euclidean_distance(0.1, 4), 1, 10000, "random seed", edges);

            dlog << LTRACE << "number of edges generated: " << edges.size();

            empirical_kernel_map<kernel_type> ekm;

            ekm.load(kern, randomly_subsample(samples, 100));

            // Project all the samples into the span of our 50 basis samples
            for (unsigned long i = 0; i < samples.size(); ++i)
                samples[i] = ekm.project(samples[i]);


            // Now create the manifold regularizer.   The result is a transformation matrix that
            // embodies the manifold assumption discussed above. 
            linear_manifold_regularizer<sample_type> lmr;
            lmr.build(samples, edges, use_gaussian_weights(0.1));
            matrix<double> T = lmr.get_transformation_matrix(10000);

            print_spinner();

            // generate the T matrix manually and make sure it matches.  The point of this test
            // is to make sure that the more complex version of this that happens inside the linear_manifold_regularizer
            // is correct.  It uses a tedious block of loops to do it in a way that is a lot faster for sparse
            // W matrices but isn't super straight forward.  
            matrix<double> X(samples[0].size(), samples.size());
            for (unsigned long i = 0; i < samples.size(); ++i)
                set_colm(X,i) = samples[i];

            matrix<double> W(samples.size(), samples.size());
            W = 0;
            for (unsigned long i = 0; i < edges.size(); ++i)
            {
                W(edges[i].index1(), edges[i].index2()) = use_gaussian_weights(0.1)(edges[i]);
                W(edges[i].index2(), edges[i].index1()) = use_gaussian_weights(0.1)(edges[i]);
            }
            matrix<double> L = diagm(sum_rows(W)) - W;
            matrix<double> trueT = inv_lower_triangular(chol(identity_matrix<double>(X.nr()) + (10000.0/sum(lowerm(W)))*X*L*trans(X)));

            dlog << LTRACE << "T error: "<< max(abs(T - trueT));
            DLIB_TEST(max(abs(T - trueT)) < 1e-7);


            print_spinner();
            // Apply the transformation generated by the linear_manifold_regularizer to 
            // all our samples.
            for (unsigned long i = 0; i < samples.size(); ++i)
                samples[i] = T*samples[i];


            // For convenience, generate a projection_function and merge the transformation
            // matrix T into it.  
            projection_function<kernel_type> proj = ekm.get_projection_function();
            proj.weights = T*proj.weights;


            // Pick 2 different labeled points.  One on the inner circle and another on the outer.  
            // For each of these test points we will see if using the single plane that separates
            // them is a good way to separate the concentric circles.  Also do this a bunch 
            // of times with different randomly chosen points so we can see how robust the result is.
            for (int itr = 0; itr < 10; ++itr)
            {
                print_spinner();
                std::vector<sample_type> test_points;
                // generate a random point from the radius 1 circle
                generate_circle(test_points, 1, 1);
                // generate a random point from the radius 5 circle
                generate_circle(test_points, 5, 1);

                // project the two test points into kernel space.  Recall that this projection_function
                // has the manifold regularizer incorporated into it.  
                const sample_type class1_point = proj(test_points[0]);
                const sample_type class2_point = proj(test_points[1]);

                double num_wrong = 0;

                // Now attempt to classify all the data samples according to which point
                // they are closest to.  The output of this program shows that without manifold 
                // regularization this test will fail but with it it will perfectly classify
                // all the points.
                for (unsigned long i = 0; i < samples.size(); ++i)
                {
                    double distance_to_class1 = length(samples[i] - class1_point);
                    double distance_to_class2 = length(samples[i] - class2_point);

                    bool predicted_as_class_1 = (distance_to_class1 < distance_to_class2);

                    bool really_is_class_1 = (i < num_points);

                    // now count how many times we make a mistake
                    if (predicted_as_class_1 != really_is_class_1)
                        ++num_wrong;
                }

                DLIB_TEST_MSG(num_wrong == 0, num_wrong);
            }

        }

        void generate_circle (
            std::vector<sample_type>& samples,
            double radius,
            const long num
        )
        {
            sample_type m(2,1);

            for (long i = 0; i < num; ++i)
            {
                double sign = 1;
                if (rnd.get_random_double() < 0.5)
                    sign = -1;
                m(0) = 2*radius*rnd.get_random_double()-radius;
                m(1) = sign*sqrt(radius*radius - m(0)*m(0));

                samples.push_back(m);
            }
        }


        void test_knn1()
        {
            std::vector<matrix<double,2,1> > samples;

            matrix<double,2,1> test;
            
            test = 0,0;  samples.push_back(test);
            test = 1,1;  samples.push_back(test);
            test = 1,-1;  samples.push_back(test);
            test = -1,1;  samples.push_back(test);
            test = -1,-1;  samples.push_back(test);

            std::vector<sample_pair> edges;
            find_k_nearest_neighbors(samples, squared_euclidean_distance(), 1, edges);
            DLIB_TEST(edges.size() == 4);

            std::sort(edges.begin(), edges.end(), &order_by_index<sample_pair>);

            DLIB_TEST(edges[0] == sample_pair(0,1,0));
            DLIB_TEST(edges[1] == sample_pair(0,2,0));
            DLIB_TEST(edges[2] == sample_pair(0,3,0));
            DLIB_TEST(edges[3] == sample_pair(0,4,0));

            find_k_nearest_neighbors(samples, squared_euclidean_distance(), 3, edges);
            DLIB_TEST(edges.size() == 8);

            find_k_nearest_neighbors(samples, squared_euclidean_distance(3.9, 4.1), 3, edges);
            DLIB_TEST(edges.size() == 4);

            std::sort(edges.begin(), edges.end(), &order_by_index<sample_pair>);

            DLIB_TEST(edges[0] == sample_pair(1,2,0));
            DLIB_TEST(edges[1] == sample_pair(1,3,0));
            DLIB_TEST(edges[2] == sample_pair(2,4,0));
            DLIB_TEST(edges[3] == sample_pair(3,4,0));

            find_k_nearest_neighbors(samples, squared_euclidean_distance(30000, 4.1), 3, edges);
            DLIB_TEST(edges.size() == 0);
        }

        void test_knn1_approx()
        {
            std::vector<matrix<double,2,1> > samples;

            matrix<double,2,1> test;
            
            test = 0,0;  samples.push_back(test);
            test = 1,1;  samples.push_back(test);
            test = 1,-1;  samples.push_back(test);
            test = -1,1;  samples.push_back(test);
            test = -1,-1;  samples.push_back(test);

            std::vector<sample_pair> edges;
            find_approximate_k_nearest_neighbors(samples, squared_euclidean_distance(), 1, 10000, seed, edges);
            DLIB_TEST(edges.size() == 4);

            std::sort(edges.begin(), edges.end(), &order_by_index<sample_pair>);

            DLIB_TEST(edges[0] == sample_pair(0,1,0));
            DLIB_TEST(edges[1] == sample_pair(0,2,0));
            DLIB_TEST(edges[2] == sample_pair(0,3,0));
            DLIB_TEST(edges[3] == sample_pair(0,4,0));

            find_approximate_k_nearest_neighbors(samples, squared_euclidean_distance(), 3, 10000, seed, edges);
            DLIB_TEST(edges.size() == 8);

            find_approximate_k_nearest_neighbors(samples, squared_euclidean_distance(3.9, 4.1), 3, 10000, seed, edges);
            DLIB_TEST(edges.size() == 4);

            std::sort(edges.begin(), edges.end(), &order_by_index<sample_pair>);

            DLIB_TEST(edges[0] == sample_pair(1,2,0));
            DLIB_TEST(edges[1] == sample_pair(1,3,0));
            DLIB_TEST(edges[2] == sample_pair(2,4,0));
            DLIB_TEST(edges[3] == sample_pair(3,4,0));

            find_approximate_k_nearest_neighbors(samples, squared_euclidean_distance(30000, 4.1), 3, 10000, seed, edges);
            DLIB_TEST(edges.size() == 0);
        }

        void test_knn2()
        {
            std::vector<matrix<double,2,1> > samples;

            matrix<double,2,1> test;
            
            test = 1,1;  samples.push_back(test);
            test = 1,-1;  samples.push_back(test);
            test = -1,1;  samples.push_back(test);
            test = -1,-1;  samples.push_back(test);

            std::vector<sample_pair> edges;
            find_k_nearest_neighbors(samples, squared_euclidean_distance(), 2, edges);
            DLIB_TEST(edges.size() == 4);

            std::sort(edges.begin(), edges.end(), &order_by_index<sample_pair>);

            DLIB_TEST(edges[0] == sample_pair(0,1,0));
            DLIB_TEST(edges[1] == sample_pair(0,2,0));
            DLIB_TEST(edges[2] == sample_pair(1,3,0));
            DLIB_TEST(edges[3] == sample_pair(2,3,0));

            find_k_nearest_neighbors(samples, squared_euclidean_distance(), 200, edges);
            DLIB_TEST(edges.size() == 4*3/2);
        }

        void test_knn2_approx()
        {
            std::vector<matrix<double,2,1> > samples;

            matrix<double,2,1> test;
            
            test = 1,1;  samples.push_back(test);
            test = 1,-1;  samples.push_back(test);
            test = -1,1;  samples.push_back(test);
            test = -1,-1;  samples.push_back(test);

            std::vector<sample_pair> edges;
            // For this simple graph and high number of samples we will do we should obtain the exact 
            // knn solution.
            find_approximate_k_nearest_neighbors(samples, squared_euclidean_distance(), 2, 10000, seed,  edges);
            DLIB_TEST(edges.size() == 4);

            std::sort(edges.begin(), edges.end(), &order_by_index<sample_pair>);

            DLIB_TEST(edges[0] == sample_pair(0,1,0));
            DLIB_TEST(edges[1] == sample_pair(0,2,0));
            DLIB_TEST(edges[2] == sample_pair(1,3,0));
            DLIB_TEST(edges[3] == sample_pair(2,3,0));


            find_approximate_k_nearest_neighbors(samples, squared_euclidean_distance(), 200, 10000, seed,  edges);
            DLIB_TEST(edges.size() == 4*3/2);
        }

        void perform_test (
        )
        {
            for (int i = 0; i < 5; ++i)
            {
                do_the_test();

                ++seed;
                test_knn1_approx();
                test_knn2_approx();
            }
            test_knn1();
            test_knn2();
            test_knn_lsh_sparse<double>();
            test_knn_lsh_sparse<float>();
            test_knn_lsh_dense<double>();
            test_knn_lsh_dense<float>();

        }
    };

    // 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 multiple times. 
    linear_manifold_regularizer_tester a;

}