-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
LMNN_unittest.cc
160 lines (143 loc) · 4.45 KB
/
LMNN_unittest.cc
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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2013 Fernando J. Iglesias Garcia
*/
#include <shogun/metric/LMNN.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/labels/MulticlassLabels.h>
#include <gtest/gtest.h>
using namespace shogun;
#if defined(HAVE_EIGEN3) && defined(HAVE_LAPACK)
TEST(LMNN,train_identity_init)
{
// create features, each column is a feature vector
SGMatrix<float64_t> feat_mat(2,4);
// 1st feature vector
feat_mat(0,0)=0;
feat_mat(1,0)=0;
// 2nd feature vector
feat_mat(0,1)=0;
feat_mat(1,1)=-1;
// 3rd feature vector
feat_mat(0,2)=1;
feat_mat(1,2)=1;
// 4th feature vector
feat_mat(0,3)=-1;
feat_mat(1,3)=1;
// wrap feat_mat in Shogun features
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(feat_mat);
// create labels
SGVector<float64_t> lab_vec(4);
lab_vec[0]=0;
lab_vec[1]=0;
lab_vec[2]=1;
lab_vec[3]=1;
// two-class data, use MulticlassLabels because it works in general for more than
// two classes
CMulticlassLabels* labels=new CMulticlassLabels(lab_vec);
// create LMNN metric machine
int32_t k=1; // number of target neighbors per example
CLMNN* lmnn=new CLMNN(features,labels,k);
// use the identity matrix as initial transform for LMNN
SGMatrix<float64_t> init_transform=SGMatrix<float64_t>::create_identity_matrix(2,1);
// set number of maximum iterations and train
lmnn->set_maxiter(500);
lmnn->train(init_transform);
// check linear transform solution
SGMatrix<float64_t> L=lmnn->get_linear_transform();
EXPECT_NEAR(L(0,0),0.991577280560543,1e-5);
EXPECT_NEAR(L(0,1),0,1e-5);
EXPECT_NEAR(L(1,0),0,1e-5);
EXPECT_NEAR(L(1,1),1.00000080000000002,1e-5);
SG_UNREF(lmnn)
}
TEST(LMNN,train_pca_init)
{
// create features, each column is a feature vector
SGMatrix<float64_t> feat_mat(2,4);
// 1st feature vector
feat_mat(0,0)=0;
feat_mat(1,0)=0;
// 2nd feature vector
feat_mat(0,1)=0;
feat_mat(1,1)=-1;
// 3rd feature vector
feat_mat(0,2)=1;
feat_mat(1,2)=1;
// 4th feature vector
feat_mat(0,3)=-1;
feat_mat(1,3)=1;
// wrap feat_mat in Shogun features
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(feat_mat);
// create labels
SGVector<float64_t> lab_vec(4);
lab_vec[0]=0;
lab_vec[1]=0;
lab_vec[2]=1;
lab_vec[3]=1;
// two-class data, use MulticlassLabels because it works in general for more than
// two classes
CMulticlassLabels* labels=new CMulticlassLabels(lab_vec);
// create LMNN metric machine
int32_t k=1; // number of target neighbors per example
CLMNN* lmnn=new CLMNN(features,labels,k);
// set number of maximum iterations and train
lmnn->set_maxiter(500);
lmnn->train();
// check linear transform solution
SGMatrix<float64_t> L=lmnn->get_linear_transform();
EXPECT_NEAR(L(1,0),0.991577280560543,1e-5);
EXPECT_NEAR(L(1,1),0,1e-5);
EXPECT_NEAR(L(0,0),0,1e-5);
EXPECT_NEAR(L(0,1),1.00000080000000002,1e-5);
SG_UNREF(lmnn)
}
TEST(LMNN,train_diagonal)
{
// create features, each column is a feature vector
SGMatrix<float64_t> feat_mat(2,4);
// 1st feature vector
feat_mat(0,0)=0;
feat_mat(1,0)=0;
// 2nd feature vector
feat_mat(0,1)=0;
feat_mat(1,1)=-1;
// 3rd feature vector
feat_mat(0,2)=1;
feat_mat(1,2)=1;
// 4th feature vector
feat_mat(0,3)=-1;
feat_mat(1,3)=1;
// wrap feat_mat in Shogun features
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(feat_mat);
// create labels
SGVector<float64_t> lab_vec(4);
lab_vec[0]=0;
lab_vec[1]=0;
lab_vec[2]=1;
lab_vec[3]=1;
// two-class data, use MulticlassLabels because it works in general for more than
// two classes
CMulticlassLabels* labels=new CMulticlassLabels(lab_vec);
// create LMNN metric machine
int32_t k=1; // number of target neighbors per example
CLMNN* lmnn=new CLMNN(features,labels,k);
// use the identity matrix as initial transform for LMNN
SGMatrix<float64_t> init_transform=SGMatrix<float64_t>::create_identity_matrix(2,1);
// set number of maximum iterations and train
lmnn->set_maxiter(1000);
lmnn->set_diagonal(true);
lmnn->train(init_transform);
// check linear transform solution
SGMatrix<float64_t> L=lmnn->get_linear_transform();
EXPECT_NEAR(L(0,0),0.61938,1e-5);
EXPECT_NEAR(L(0,1),0,1e-5);
EXPECT_NEAR(L(1,0),0,1e-5);
EXPECT_NEAR(L(1,1),1.0000,1e-5);
SG_UNREF(lmnn)
}
#endif /* HAVE_LAPACK && HAVE_EIGEN3 */