File: ekm_and_lisf.cpp

package info (click to toggle)
mldemos 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,224 kB
  • ctags: 46,525
  • sloc: cpp: 306,887; ansic: 167,718; ml: 126; sh: 109; makefile: 2
file content (306 lines) | stat: -rw-r--r-- 10,732 bytes parent folder | download | duplicates (7)
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
// Copyright (C) 2009  Davis E. King (davis@dlib.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>
#include <ctime>

namespace  
{
    using namespace test;
    using namespace dlib;
    using namespace std;
    dlib::logger dlog("test.ekm_and_lisf");


    class empirical_kernel_map_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:
        empirical_kernel_map_tester (
        ) :
            tester (
                "test_ekm_and_lisf",       // the command line argument name for this test
                "Run tests on the empirical_kernel_map and linearly_independent_subset_finder objects.", // the command line argument description
                0                     // the number of command line arguments for this test
            )
        {
            thetime = time(0);
        }

        time_t thetime;
        dlib::rand rnd;

        template <typename T>
        void validate (
            const T& ekm_small,
            const T& ekm_big
        )
        {
            matrix<double> tmat;
            projection_function<typename T::kernel_type> proj;

            ekm_small.get_transformation_to(ekm_big, tmat, proj);
            DLIB_TEST(tmat.nr() == ekm_big.out_vector_size());
            DLIB_TEST(tmat.nc() == ekm_small.out_vector_size());
            DLIB_TEST((unsigned long)proj.basis_vectors.size() == ekm_big.basis_size() - ekm_small.basis_size());
            for (unsigned long i = 0; i < 6; ++i)
            {
                const typename T::sample_type temp = randm(4,1,rnd);
                DLIB_TEST(length(ekm_big.project(temp) - (tmat*ekm_small.project(temp) + proj(temp))) < 1e-10);
            }
        }


        void test_transformation_stuff()
        {
            typedef matrix<double,0,1> sample_type;
            typedef radial_basis_kernel<sample_type> kernel_type;
            const kernel_type kern(1);


            for (unsigned long n = 1; n < 6; ++n)
            {
                print_spinner();
                for (unsigned long extra = 1; extra < 10; ++extra)
                {
                    std::vector<sample_type> samps_small, samps_big;
                    linearly_independent_subset_finder<kernel_type> lisf_small(kern, 1000);
                    linearly_independent_subset_finder<kernel_type> lisf_big(kern, 1000);
                    for (unsigned long i = 0; i < n; ++i)
                    {
                        samps_small.push_back(randm(4,1,rnd));
                        samps_big.push_back(samps_small.back());
                        lisf_big.add(samps_small.back());
                        lisf_small.add(samps_small.back());
                    }
                    for (unsigned long i = 0; i < extra; ++i)
                    {
                        samps_big.push_back(randm(4,1,rnd));
                        lisf_big.add(samps_big.back());
                    }


                    // test no lisf
                    {
                        empirical_kernel_map<kernel_type> ekm_small, ekm_big;
                        ekm_small.load(kern, samps_small);
                        ekm_big.load(kern, samps_big);

                        validate(ekm_small, ekm_big);
                    }

                    // test with lisf
                    {
                        empirical_kernel_map<kernel_type> ekm_small, ekm_big;
                        ekm_small.load(lisf_small);
                        ekm_big.load(lisf_big);

                        validate(ekm_small, ekm_big);
                    }

                    // test with partly lisf
                    {
                        empirical_kernel_map<kernel_type> ekm_small, ekm_big;
                        ekm_small.load(kern, samps_small);
                        ekm_big.load(lisf_big);

                        validate(ekm_small, ekm_big);
                    }

                    // test with partly lisf
                    {
                        empirical_kernel_map<kernel_type> ekm_small, ekm_big;
                        ekm_small.load(lisf_small);
                        ekm_big.load(kern, samps_big);

                        validate(ekm_small, ekm_big);
                    }

                }
            }


            // test what happens if the bigger ekm only has repeated basis vectors
            {
                empirical_kernel_map<kernel_type> ekm_big, ekm_small;
                std::vector<sample_type> samps_big, samps_small;

                sample_type temp = randm(4,1,rnd);

                samps_small.push_back(temp);
                samps_big.push_back(temp);
                samps_big.push_back(temp);

                ekm_big.load(kern, samps_big);
                ekm_small.load(kern, samps_small);

                validate(ekm_small, ekm_big);

            }
            {
                empirical_kernel_map<kernel_type> ekm_big, ekm_small;
                linearly_independent_subset_finder<kernel_type> lisf_small(kern, 1000);
                std::vector<sample_type> samps_big;

                sample_type temp = randm(4,1,rnd);

                lisf_small.add(temp);
                samps_big.push_back(temp);
                samps_big.push_back(temp);

                ekm_big.load(kern, samps_big);
                ekm_small.load(lisf_small);

                validate(ekm_small, ekm_big);

            }
            {
                empirical_kernel_map<kernel_type> ekm_big, ekm_small;
                std::vector<sample_type> samps_big, samps_small;

                sample_type temp = randm(4,1,rnd);
                sample_type temp2 = randm(4,1,rnd);

                samps_small.push_back(temp);
                samps_small.push_back(temp2);
                samps_big.push_back(temp);
                samps_big.push_back(temp2);
                samps_big.push_back(randm(4,1,rnd));

                ekm_big.load(kern, samps_big);
                ekm_small.load(kern, samps_small);

                validate(ekm_small, ekm_big);

            }
            {
                empirical_kernel_map<kernel_type> ekm_big, ekm_small;
                linearly_independent_subset_finder<kernel_type> lisf_small(kern, 1000);
                std::vector<sample_type> samps_big;

                sample_type temp = randm(4,1,rnd);
                sample_type temp2 = randm(4,1,rnd);

                lisf_small.add(temp);
                lisf_small.add(temp2);
                samps_big.push_back(temp);
                samps_big.push_back(temp2);
                samps_big.push_back(temp);

                ekm_big.load(kern, samps_big);
                ekm_small.load(lisf_small);

                validate(ekm_small, ekm_big);

            }


        }



        void perform_test (
        )
        {
            ++thetime;
            typedef matrix<double,0,1> sample_type;
            //dlog << LINFO << "time seed: " << thetime;
            //rnd.set_seed(cast_to_string(thetime));


            typedef radial_basis_kernel<sample_type> kernel_type;


            for (int n = 1; n < 10; ++n)
            {
                print_spinner();
                dlog << LINFO << "matrix size " << n;

                std::vector<sample_type> samples;
                // make some samples
                for (int i = 0; i < n; ++i)
                {
                    samples.push_back(randm(4,1,rnd));
                    // double up the samples just to mess with the lisf
                    if (n > 5)
                        samples.push_back(samples.back());
                }

                dlog << LINFO << "samples.size(): "<< samples.size();

                const kernel_type kern(1);

                linearly_independent_subset_finder<kernel_type> lisf(kern, 100, 1e-4);
                unsigned long count = 0;
                for (unsigned long i = 0; i < samples.size(); ++i)
                {
                    if (lisf.add(samples[i]))
                    {
                        DLIB_TEST(equal(lisf[lisf.size()-1], samples[i]));
                        ++count;
                    }
                }
                DLIB_TEST(count == lisf.size());

                DLIB_TEST(lisf.size() == (unsigned int)n);


                dlog << LINFO << "lisf.size(): "<< lisf.size();

                // make sure the kernel matrices coming out of the lisf are correct
                DLIB_TEST(dlib::equal(lisf.get_kernel_matrix(), kernel_matrix(kern, lisf), 1e-8));
                DLIB_TEST(dlib::equal(lisf.get_inv_kernel_marix(), inv(kernel_matrix(kern, lisf.get_dictionary())), 1e-8));

                empirical_kernel_map<kernel_type> ekm;
                ekm.load(lisf);
                DLIB_TEST(ekm.basis_size() == lisf.size());

                std::vector<sample_type> proj_samples;
                for (unsigned long i = 0; i < samples.size(); ++i)
                {
                    double err;
                    proj_samples.push_back(ekm.project(samples[i], err));
                    DLIB_TEST(err <= 1e-4);
                    const double error_agreement = std::abs(err - lisf.projection_error(samples[i]));
                    dlog << LTRACE << "err: " << err << "    error_agreement: "<< error_agreement;
                    DLIB_TEST(error_agreement < 1e-11);
                }

                for (int i = 0; i < 5; ++i)
                {
                    sample_type temp = randm(4,1,rnd);
                    double err;
                    ekm.project(temp, err);
                    const double error_agreement = std::abs(err - lisf.projection_error(temp));
                    dlog << LTRACE << "err: " << err << "    error_agreement: "<< error_agreement;
                    DLIB_TEST(error_agreement < 1e-11);
                }

                // make sure the EKM did the projection correctly
                DLIB_TEST(dlib::equal(kernel_matrix(kern, samples), kernel_matrix(linear_kernel<sample_type>(), proj_samples), 1e-5));
            }


            test_transformation_stuff();

        }
    };

    // 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. 
    empirical_kernel_map_tester a;

}