-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel_matrix.cpp
More file actions
161 lines (128 loc) · 5.37 KB
/
Copy pathkernel_matrix.cpp
File metadata and controls
161 lines (128 loc) · 5.37 KB
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
// 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 <vector>
#include <sstream>
namespace
{
using namespace test;
using namespace dlib;
using namespace std;
dlib::logger dlog("test.kernel_matrix");
class kernel_matrix_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:
kernel_matrix_tester (
) :
tester (
"test_kernel_matrix", // the command line argument name for this test
"Run tests on the kernel_matrix functions.", // the command line argument description
0 // the number of command line arguments for this test
)
{
}
void perform_test (
)
{
print_spinner();
typedef matrix<double,0,1> sample_type;
typedef radial_basis_kernel<sample_type> kernel_type;
kernel_type kern(0.1);
std::vector<sample_type> vect1;
std::vector<sample_type> vect2;
const sample_type samp = randm(4,1);
sample_type samp2, samp3;
vect1.push_back(randm(4,1));
vect1.push_back(randm(4,1));
vect1.push_back(randm(4,1));
vect1.push_back(randm(4,1));
vect2.push_back(randm(4,1));
vect2.push_back(randm(4,1));
vect2.push_back(randm(4,1));
vect2.push_back(randm(4,1));
vect2.push_back(randm(4,1));
matrix<double> K;
K.set_size(vect1.size(), vect2.size());
for (long r = 0; r < K.nr(); ++r)
{
for (long c = 0; c < K.nc(); ++c)
{
K(r,c) = kern(vect1[r], vect2[c]);
}
}
DLIB_TEST(equal(K, kernel_matrix(kern, vect1, vect2)));
DLIB_TEST(equal(K, kernel_matrix(kern, mat(vect1), mat(vect2))));
K.set_size(vect2.size(), vect1.size());
for (long r = 0; r < K.nr(); ++r)
{
for (long c = 0; c < K.nc(); ++c)
{
K(r,c) = kern(vect2[r], vect1[c]);
}
}
DLIB_TEST(equal(K, kernel_matrix(kern, vect2, vect1)));
DLIB_TEST(equal(K, tmp(kernel_matrix(kern, vect2, vect1))));
DLIB_TEST(equal(K, kernel_matrix(kern, mat(vect2), mat(vect1))));
K.set_size(vect1.size(), vect1.size());
for (long r = 0; r < K.nr(); ++r)
{
for (long c = 0; c < K.nc(); ++c)
{
K(r,c) = kern(vect1[r], vect1[c]);
}
}
DLIB_TEST(equal(K, kernel_matrix(kern, vect1, vect1)));
DLIB_TEST(equal(K, tmp(kernel_matrix(kern, vect1, vect1))));
DLIB_TEST(equal(K, kernel_matrix(kern, vect1)));
DLIB_TEST(equal(K, tmp(kernel_matrix(kern, vect1))));
DLIB_TEST(equal(K, kernel_matrix(kern, mat(vect1), mat(vect1))));
DLIB_TEST(equal(K, tmp(kernel_matrix(kern, mat(vect1), mat(vect1)))));
DLIB_TEST(equal(K, kernel_matrix(kern, mat(vect1))));
DLIB_TEST(equal(K, tmp(kernel_matrix(kern, mat(vect1)))));
K.set_size(vect1.size(),1);
for (long r = 0; r < K.nr(); ++r)
{
for (long c = 0; c < K.nc(); ++c)
{
K(r,c) = kern(vect1[r], samp);
}
}
DLIB_TEST(equal(K, kernel_matrix(kern, vect1, samp)));
DLIB_TEST(equal(K, kernel_matrix(kern, mat(vect1), samp)));
K.set_size(1, vect1.size());
for (long r = 0; r < K.nr(); ++r)
{
for (long c = 0; c < K.nc(); ++c)
{
K(r,c) = kern(samp, vect1[c]);
}
}
DLIB_TEST(equal(K, kernel_matrix(kern, samp, vect1)));
DLIB_TEST(equal(K, kernel_matrix(kern, samp, mat(vect1))));
DLIB_TEST(equal(K, tmp(kernel_matrix(kern, samp, vect1))));
DLIB_TEST(equal(K, tmp(kernel_matrix(kern, samp, mat(vect1)))));
samp2 = samp;
samp3 = samp;
// test the alias detection
samp2 = kernel_matrix(kern, vect1, samp2);
DLIB_TEST(equal(samp2, kernel_matrix(kern, vect1, samp)));
samp3 = trans(kernel_matrix(kern, samp3, vect2));
DLIB_TEST(equal(samp3, trans(kernel_matrix(kern, samp, vect2))));
samp2 += kernel_matrix(kern, vect1, samp);
DLIB_TEST(equal(samp2, 2*kernel_matrix(kern, vect1, samp)));
samp3 += trans(kernel_matrix(kern, samp, vect2));
DLIB_TEST(equal(samp3, 2*trans(kernel_matrix(kern, samp, vect2))));
}
};
// 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.
kernel_matrix_tester a;
}