-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
DenseLabels.cpp
210 lines (171 loc) · 4.55 KB
/
DenseLabels.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
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
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soeren Sonnenburg, Heiko Strathmann, Sergey Lisitsyn,
* Fernando Iglesias, Michele Mazzoni, Chiyuan Zhang
*/
#include <shogun/base/Parameter.h>
#include <shogun/io/File.h>
#include <shogun/io/SGIO.h>
#include <shogun/labels/DenseLabels.h>
#include <shogun/labels/Labels.h>
#include <shogun/lib/common.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/linalg/LinalgNamespace.h>
using namespace shogun;
CDenseLabels::CDenseLabels()
: CLabels()
{
init();
}
CDenseLabels::CDenseLabels(const CDenseLabels& orig) : CLabels(orig)
{
init();
m_labels = orig.m_labels;
}
CDenseLabels::CDenseLabels(int32_t num_lab)
: CLabels()
{
init();
m_labels = SGVector<float64_t>(num_lab);
m_current_values=SGVector<float64_t>(num_lab);
linalg::zero(m_labels);
linalg::zero(m_current_values);
}
CDenseLabels::CDenseLabels(CFile* loader)
: CLabels()
{
init();
load(loader);
}
CDenseLabels::~CDenseLabels()
{
}
void CDenseLabels::init()
{
SG_ADD(&m_labels, "labels", "The labels.", MS_NOT_AVAILABLE);
}
void CDenseLabels::set_to_one()
{
set_to_const(1.0);
}
void CDenseLabels::zero()
{
set_to_const(0.0);
}
void CDenseLabels::set_to_const(float64_t c)
{
ASSERT(m_labels.vector)
index_t subset_size=get_num_labels();
for (int32_t i=0; i<subset_size; i++)
{
m_labels.vector[m_subset_stack->subset_idx_conversion(i)]=c;
m_current_values.vector[m_subset_stack->subset_idx_conversion(i)]=c;
}
}
void CDenseLabels::set_labels(SGVector<float64_t> v)
{
if (m_subset_stack->has_subsets())
SG_ERROR("A subset is set, cannot set labels\n")
m_labels = v;
}
SGVector<float64_t> CDenseLabels::get_labels() const
{
if (m_subset_stack->has_subsets())
return get_labels_copy();
return m_labels;
}
SGVector<float64_t> CDenseLabels::get_labels_copy() const
{
if (!m_subset_stack->has_subsets())
return m_labels.clone();
index_t num_labels = get_num_labels();
SGVector<float64_t> result(num_labels);
/* copy element wise because of possible subset */
for (index_t i=0; i<num_labels; i++)
result[i] = get_label(i);
return result;
}
SGVector<int32_t> CDenseLabels::get_int_labels()
{
SGVector<int32_t> intlab(get_num_labels());
for (int32_t i=0; i<get_num_labels(); i++)
intlab.vector[i] = get_int_label(i);
return intlab;
}
void CDenseLabels::set_int_labels(SGVector<int32_t> lab)
{
if (m_subset_stack->has_subsets())
SG_ERROR("set_int_labels() is not possible on subset")
m_labels = SGVector<float64_t>(lab.vlen);
for (int32_t i=0; i<lab.vlen; i++)
set_int_label(i, lab.vector[i]);
}
#if !defined(SWIGJAVA) && !defined(SWIGCSHARP)
void CDenseLabels::set_int_labels(SGVector<int64_t> lab)
{
if (m_subset_stack->has_subsets())
SG_ERROR("set_int_labels() is not possible on subset")
m_labels = SGVector<float64_t>(lab.vlen);
for (int32_t i=0; i<lab.vlen; i++)
set_int_label(i, lab.vector[i]);
}
#endif
void CDenseLabels::ensure_valid(const char* context)
{
if (m_labels.vector == NULL)
SG_ERROR("%s%sempty content (NULL) for labels\n", context?context:"", context?": ":"")
}
void CDenseLabels::load(CFile* loader)
{
remove_subset();
m_labels = SGVector<float64_t>();
m_labels.load(loader);
}
void CDenseLabels::save(CFile* writer)
{
if (m_subset_stack->has_subsets())
SG_ERROR("save() is not possible on subset")
m_labels.save(writer);
}
bool CDenseLabels::set_label(int32_t idx, float64_t label)
{
int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
if (m_labels.vector && real_num<get_num_labels())
{
m_labels.vector[real_num]=label;
return true;
}
else
return false;
}
bool CDenseLabels::set_int_label(int32_t idx, int32_t label)
{
int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
if (m_labels.vector && real_num<get_num_labels())
{
m_labels.vector[real_num] = (float64_t)label;
return true;
}
else
return false;
}
float64_t CDenseLabels::get_label(int32_t idx) const
{
int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
ASSERT(m_labels.vector && idx<get_num_labels())
return m_labels.vector[real_num];
}
int32_t CDenseLabels::get_int_label(int32_t idx)
{
int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
ASSERT(m_labels.vector && idx<get_num_labels())
if (m_labels.vector[real_num] != float64_t((int32_t(m_labels.vector[real_num]))))
SG_ERROR("label[%d]=%g is not an integer\n", m_labels.vector[real_num])
return int32_t(m_labels.vector[real_num]);
}
int32_t CDenseLabels::get_num_labels() const
{
return m_subset_stack->has_subsets()
? m_subset_stack->get_size() : m_labels.vlen;
}