-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
classifier_larank.cpp
102 lines (82 loc) · 2.86 KB
/
classifier_larank.cpp
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
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Heiko Strathmann, Sergey Lisitsyn
*/
#include <shogun/labels/MulticlassLabels.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/kernel/GaussianKernel.h>
#include <shogun/multiclass/LaRank.h>
#include <shogun/base/init.h>
using namespace shogun;
void test()
{
index_t num_vec=10;
index_t num_feat=3;
index_t num_class=num_feat; // to make data easy
float64_t distance=15;
// create some linearly seperable data
SGMatrix<float64_t> matrix(num_class, num_vec);
SGMatrix<float64_t> matrix_test(num_class, num_vec);
CMulticlassLabels* labels=new CMulticlassLabels(num_vec);
CMulticlassLabels* labels_test=new CMulticlassLabels(num_vec);
auto m_rng = std::unique_ptr<CRandom>(new CRandom());
for (index_t i=0; i<num_vec; ++i)
{
index_t label=i%num_class;
for (index_t j=0; j<num_feat; ++j)
{
matrix(j, i) = m_rng->std_normal_distrib();
matrix_test(j, i) = m_rng->std_normal_distrib();
labels->set_label(i, label);
labels_test->set_label(i, label);
}
/* make sure data is linearly seperable per class */
matrix(label,i)+=distance;
matrix_test(label,i)+=distance;
}
matrix.display_matrix("matrix");
labels->get_int_labels().display_vector("labels");
// shogun will now own the matrix created
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(matrix);
CDenseFeatures<float64_t>* features_test=
new CDenseFeatures<float64_t>(matrix_test);
// create three labels
for (index_t i=0; i<num_vec; ++i)
labels->set_label(i, i%num_class);
// create gaussian kernel with cache 10MB, width 0.5
CGaussianKernel* kernel = new CGaussianKernel(10, 0.5);
kernel->init(features, features);
// create libsvm with C=10 and train
CLaRank* svm = new CLaRank(10, kernel, labels);
svm->train();
svm->train();
// classify on training examples
CMulticlassLabels* output=(CMulticlassLabels*)svm->apply();
output->get_labels().display_vector("batch output");
/* assert that batch apply and apply(index_t) give same result */
SGVector<float64_t> single_outputs(output->get_num_labels());
for (index_t i=0; i<output->get_num_labels(); ++i)
single_outputs[i]=svm->apply_one(i);
single_outputs.display_vector("single_outputs");
for (index_t i=0; i<output->get_num_labels(); ++i)
ASSERT(output->get_label(i)==single_outputs[i]);
CMulticlassLabels* output_test=
(CMulticlassLabels*)svm->apply(features_test);
labels_test->get_labels().display_vector("labels_test");
output_test->get_labels().display_vector("output_test");
for (index_t i=0; i<output->get_num_labels(); ++i)
ASSERT(labels_test->get_label(i)==output_test->get_label(i));
// free up memory
SG_UNREF(output);
SG_UNREF(labels_test);
SG_UNREF(output_test);
SG_UNREF(svm);
}
int main(int argc, char** argv)
{
init_shogun_with_defaults();
test();
exit_shogun();
return 0;
}