-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Serialization_unittest.cc
71 lines (56 loc) · 1.77 KB
/
Serialization_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
/*
* 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 Heiko Strathmann
*/
#include <shogun/base/init.h>
#include <shogun/labels/MulticlassLabels.h>
#include <shogun/io/SerializableAsciiFile.h>
#include <gtest/gtest.h>
using namespace shogun;
TEST(Serialization,multiclass_labels)
{
index_t n=10;
index_t n_class=3;
CMulticlassLabels* labels=new CMulticlassLabels();
SGVector<float64_t> lab(n);
for (index_t i=0; i<n; ++i)
lab[i]=i%n_class;
labels->set_labels(lab);
labels->allocate_confidences_for(n_class);
SGVector<float64_t> conf(n_class);
for (index_t i=0; i<n_class; ++i)
conf[i]=CMath::randn_double();
for (index_t i=0; i<n; ++i)
labels->set_multiclass_confidences(i, conf);
/* create serialized copy */
const char* filename="multiclass_labels.txt";
CSerializableAsciiFile* file=new CSerializableAsciiFile(filename, 'w');
labels->save_serializable(file);
file->close();
SG_UNREF(file);
file=new CSerializableAsciiFile(filename, 'r');
CMulticlassLabels* labels_loaded=new CMulticlassLabels();
labels_loaded->load_serializable(file);
file->close();
SG_UNREF(file);
/* compare */
for (index_t i=0; i<n; ++i)
ASSERT(labels_loaded->get_labels()[i]==labels->get_labels()[i]);
for (index_t i=0; i<n; ++i)
{
for (index_t j=0; j<n_class; ++j)
{
//float64_t a=labels->get_multiclass_confidences(i)[j];
//float64_t b=labels_loaded->get_multiclass_confidences(i)[j];
// Add one multiclass serialization works
//float64_t diff=CMath::abs(a-b);
//EXPECT_LE(diff, 10E-15);
}
}
SG_UNREF(labels_loaded);
SG_UNREF(labels);
}