/
MulticlassLabels.cpp
268 lines (226 loc) · 7.08 KB
/
MulticlassLabels.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <set>
#include <shogun/labels/BinaryLabels.h>
#include <shogun/labels/DenseLabels.h>
#include <shogun/labels/MulticlassLabels.h>
#include <utility>
using namespace shogun;
MulticlassLabels::MulticlassLabels() : DenseLabels()
{
init();
}
MulticlassLabels::MulticlassLabels(int32_t num_labels) : DenseLabels(num_labels)
{
init();
}
MulticlassLabels::MulticlassLabels(SGVector<float64_t> src) : DenseLabels()
{
init();
set_labels(src);
}
MulticlassLabels::MulticlassLabels(std::shared_ptr<File> loader) : DenseLabels(std::move(loader))
{
init();
}
MulticlassLabels::MulticlassLabels(const std::shared_ptr<BinaryLabels>& labels)
: DenseLabels(labels->get_num_labels())
{
init();
for (index_t i = 0; i < labels->get_num_labels(); ++i)
m_labels[i] = (labels->get_label(i) == 1 ? 1 : 0);
}
MulticlassLabels::MulticlassLabels(const MulticlassLabels& orig)
: DenseLabels(orig)
{
init();
m_multiclass_confidences = orig.m_multiclass_confidences;
}
MulticlassLabels::~MulticlassLabels()
{
}
void MulticlassLabels::init()
{
m_multiclass_confidences=SGMatrix<float64_t>();
watch_method("num_classes", &MulticlassLabels::get_num_classes);
}
void MulticlassLabels::set_multiclass_confidences(int32_t i,
SGVector<float64_t> confidences)
{
require(confidences.size()==m_multiclass_confidences.num_rows,
"{}::set_multiclass_confidences(): Length of confidences should "
"match size of the matrix", get_name());
m_multiclass_confidences.set_column(i, confidences);
}
SGVector<float64_t> MulticlassLabels::get_multiclass_confidences(int32_t i)
{
SGVector<float64_t> confs(m_multiclass_confidences.num_rows);
for (index_t j=0; j<confs.size(); j++)
confs[j] = m_multiclass_confidences(j,i);
return confs;
}
void MulticlassLabels::allocate_confidences_for(int32_t n_classes)
{
int32_t n_labels = m_labels.size();
require(n_labels!=0,"{}::allocate_confidences_for(): There should be "
"labels to store confidences", get_name());
m_multiclass_confidences = SGMatrix<float64_t>(n_classes,n_labels);
}
SGVector<float64_t> MulticlassLabels::get_confidences_for_class(int32_t i)
{
require(
(m_multiclass_confidences.num_rows != 0) &&
(m_multiclass_confidences.num_cols != 0),
"Empty confidences, which need to be allocated before fetching.");
SGVector<float64_t> confs(m_multiclass_confidences.num_cols);
for (index_t j = 0; j < confs.size(); j++)
confs[j] = m_multiclass_confidences(i, j);
return confs;
}
bool MulticlassLabels::is_valid() const
{
if (!DenseLabels::is_valid())
return false;
int32_t subset_size=get_num_labels();
for (int32_t i=0; i<subset_size; i++)
{
int32_t real_i = m_subset_stack->subset_idx_conversion(i);
int32_t label = int64_t(m_labels[real_i]);
if (label<0 || float64_t(label)!=m_labels[real_i])
{
return false;
}
}
return true;
}
void MulticlassLabels::ensure_valid(const char* context)
{
require(is_valid(), "Multiclass Labels must be in range "
"[0,...,num_classes] and integers!");
}
ELabelType MulticlassLabels::get_label_type() const
{
return LT_MULTICLASS;
}
std::shared_ptr<BinaryLabels> MulticlassLabels::get_binary_for_class(int32_t i)
{
SGVector<float64_t> binary_labels(get_num_labels());
bool use_confidences = false;
if ((m_multiclass_confidences.num_rows != 0) && (m_multiclass_confidences.num_cols != 0))
{
use_confidences = true;
}
if (use_confidences)
{
for (int32_t k=0; k<binary_labels.vlen; k++)
{
int32_t label = get_int_label(k);
float64_t confidence = m_multiclass_confidences(label,k);
binary_labels[k] = label == i ? confidence : -confidence;
}
}
else
{
for (int32_t k=0; k<binary_labels.vlen; k++)
{
int32_t label = get_int_label(k);
binary_labels[k] = label == i ? +1.0 : -1.0;
}
}
return std::make_shared<BinaryLabels>(binary_labels);
}
SGVector<float64_t> MulticlassLabels::get_unique_labels()
{
/* extract all labels (copy because of possible subset) */
SGVector<float64_t> unique_labels=get_labels_copy();
unique_labels.vlen=SGVector<float64_t>::unique(unique_labels.vector, unique_labels.vlen);
SGVector<float64_t> result(unique_labels.vlen);
sg_memcpy(result.vector, unique_labels.vector,
sizeof(float64_t)*unique_labels.vlen);
return result;
}
int32_t MulticlassLabels::get_num_classes()
{
SGVector<float64_t> unique=get_unique_labels();
return unique.vlen;
}
std::shared_ptr<Labels> MulticlassLabels::shallow_subset_copy()
{
SGVector<float64_t> shallow_copy_vector(m_labels);
auto shallow_copy_labels=std::make_shared<MulticlassLabels>(m_labels.size());
shallow_copy_labels->set_labels(shallow_copy_vector);
if (m_subset_stack->has_subsets())
shallow_copy_labels->add_subset(m_subset_stack->get_last_subset()->get_subset_idx());
return shallow_copy_labels;
}
std::shared_ptr<MulticlassLabels> MulticlassLabels::obtain_from_generic(const std::shared_ptr<Labels>& labels)
{
if (labels == NULL)
return NULL;
if (labels->get_label_type() != LT_MULTICLASS)
{
error("The Labels passed cannot be casted to MulticlassLabels!");
return NULL;
}
return std::dynamic_pointer_cast<MulticlassLabels>(labels);
}
std::shared_ptr<Labels> MulticlassLabels::duplicate() const
{
return std::make_shared<MulticlassLabels>(*this);
}
namespace shogun
{
SG_FORCED_INLINE std::shared_ptr<MulticlassLabels> to_multiclass(const std::shared_ptr<DenseLabels>& orig)
{
auto result_vector = orig->get_labels();
std::set<int32_t> unique(result_vector.begin(), result_vector.end());
// potentially convert to [0,1, ..., num_classes-1] if not in that form
// TODO: remove this once multiclass labels can be any discrete set
auto min = (*std::min_element(unique.begin(), unique.end()));
auto max = (*std::max_element(unique.begin(), unique.end()));
if (!(min == 0 && max == (index_t)unique.size() - 1))
{
// print conversion table for users
io::warn(
"Converting non-contiguous multiclass labels to "
"contiguous version:",
unique.size() - 1);
std::for_each(
unique.begin(), unique.end(), [&unique](int32_t old_label) {
auto new_label =
std::distance(unique.begin(), unique.find(old_label));
io::warn("Converting {} to {}.", old_label, new_label);
});
SGVector<float64_t> converted(result_vector.vlen);
std::transform(
result_vector.begin(), result_vector.end(), converted.begin(),
[&unique](int32_t old_label) {
return std::distance(
unique.begin(), unique.find(old_label));
});
result_vector = converted;
}
return std::make_shared<MulticlassLabels>(result_vector);
}
std::shared_ptr<MulticlassLabels> multiclass_labels(const std::shared_ptr<Labels>& orig)
{
require(orig, "No labels provided.");
try
{
switch (orig->get_label_type())
{
case LT_MULTICLASS:
return std::static_pointer_cast<MulticlassLabels>(orig);
case LT_BINARY:
return to_multiclass(std::static_pointer_cast<BinaryLabels>(orig));
default:
not_implemented(SOURCE_LOCATION);
}
}
catch (const ShogunException& e)
{
error(
"Cannot convert {} to multiclass labels: {}",
orig->get_name(), e.what());
}
return nullptr;
}
} // namespace shogun