-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
MulticlassOneVsOneStrategy.cpp
322 lines (270 loc) · 8.14 KB
/
MulticlassOneVsOneStrategy.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* 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) 2012 Chiyuan Zhang
* Written (W) 2013 Shell Hu and Heiko Strathmann
* Copyright (C) 2012 Chiyuan Zhang
*/
#include <shogun/multiclass/MulticlassOneVsOneStrategy.h>
#include <shogun/labels/BinaryLabels.h>
#include <shogun/labels/MulticlassLabels.h>
using namespace shogun;
CMulticlassOneVsOneStrategy::CMulticlassOneVsOneStrategy()
:CMulticlassStrategy(), m_num_machines(0), m_num_samples(SGVector<int32_t>())
{
register_parameters();
}
CMulticlassOneVsOneStrategy::CMulticlassOneVsOneStrategy(EProbHeuristicType prob_heuris)
:CMulticlassStrategy(prob_heuris), m_num_machines(0), m_num_samples(SGVector<int32_t>())
{
register_parameters();
}
void CMulticlassOneVsOneStrategy::register_parameters()
{
//SG_ADD(&m_num_samples, "num_samples", "Number of samples in each training machine", MS_NOT_AVAILABLE);
SG_WARNING("%s::CMulticlassOneVsOneStrategy(): register parameters!\n", get_name());
}
void CMulticlassOneVsOneStrategy::train_start(CMulticlassLabels *orig_labels, CBinaryLabels *train_labels)
{
CMulticlassStrategy::train_start(orig_labels, train_labels);
m_num_machines=m_num_classes*(m_num_classes-1)/2;
m_train_pair_idx_1 = 0;
m_train_pair_idx_2 = 1;
m_num_samples.resize_vector(m_num_machines);
}
bool CMulticlassOneVsOneStrategy::train_has_more()
{
return m_train_iter < m_num_machines;
}
SGVector<int32_t> CMulticlassOneVsOneStrategy::train_prepare_next()
{
CMulticlassStrategy::train_prepare_next();
SGVector<int32_t> subset(m_orig_labels->get_num_labels());
int32_t tot=0;
for (int32_t k=0; k < m_orig_labels->get_num_labels(); ++k)
{
if (((CMulticlassLabels*) m_orig_labels)->get_int_label(k)==m_train_pair_idx_1)
{
((CBinaryLabels*) m_train_labels)->set_label(k, +1.0);
subset[tot]=k;
tot++;
}
else if (((CMulticlassLabels*) m_orig_labels)->get_int_label(k)==m_train_pair_idx_2)
{
((CBinaryLabels*) m_train_labels)->set_label(k, -1.0);
subset[tot]=k;
tot++;
}
}
m_train_pair_idx_2++;
if (m_train_pair_idx_2 >= m_num_classes)
{
m_train_pair_idx_1++;
m_train_pair_idx_2=m_train_pair_idx_1+1;
}
// collect num samples each machine
m_num_samples[m_train_iter-1] = tot;
subset.resize_vector(tot);
return subset;
}
int32_t CMulticlassOneVsOneStrategy::decide_label(SGVector<float64_t> outputs)
{
// if OVO with prob outputs, find max posterior
if (outputs.vlen==m_num_classes)
return SGVector<float64_t>::arg_max(outputs.vector, 1, outputs.vlen);
int32_t s=0;
SGVector<int32_t> votes(m_num_classes);
SGVector<int32_t> dec_vals(m_num_classes);
votes.zero();
dec_vals.zero();
for (int32_t i=0; i<m_num_classes; i++)
{
for (int32_t j=i+1; j<m_num_classes; j++)
{
if (outputs[s]>0)
{
votes[i]++;
dec_vals[i] += CMath::abs(outputs[s]);
}
else
{
votes[j]++;
dec_vals[j] += CMath::abs(outputs[s]);
}
s++;
}
}
int32_t i_max=0;
int32_t vote_max=-1;
float64_t dec_val_max=-1;
for (int32_t i=0; i < m_num_classes; ++i)
{
if (votes[i] > vote_max)
{
i_max = i;
vote_max = votes[i];
dec_val_max = dec_vals[i];
}
else if (votes[i] == vote_max)
{
if (dec_vals[i] > dec_val_max)
{
i_max = i;
dec_val_max = dec_vals[i];
}
}
}
return i_max;
}
void CMulticlassOneVsOneStrategy::rescale_outputs(SGVector<float64_t> outputs)
{
if (m_num_machines < 1)
return;
SGVector<int32_t> indx1(m_num_machines);
SGVector<int32_t> indx2(m_num_machines);
int32_t tot = 0;
for (int32_t j=0; j<m_num_classes; j++)
{
for (int32_t k=j+1; k<m_num_classes; k++)
{
indx1[tot] = j;
indx2[tot] = k;
tot++;
}
}
if(tot!=m_num_machines)
SG_ERROR("%s::rescale_output(): size(outputs) is not num_machines.\n", get_name());
switch(get_prob_heuris_type())
{
case OVO_PRICE:
rescale_heuris_price(outputs,indx1,indx2);
break;
case OVO_HASTIE:
rescale_heuris_hastie(outputs,indx1,indx2);
break;
case OVO_HAMAMURA:
rescale_heuris_hamamura(outputs,indx1,indx2);
break;
case PROB_HEURIS_NONE:
break;
default:
SG_ERROR("%s::rescale_outputs(): Unknown OVO probability heuristic type!\n", get_name());
break;
}
}
void CMulticlassOneVsOneStrategy::rescale_heuris_price(SGVector<float64_t> outputs,
const SGVector<int32_t> indx1, const SGVector<int32_t> indx2)
{
if (m_num_machines != outputs.vlen)
{
SG_ERROR("%s::rescale_heuris_price(): size(outputs) = %d != m_num_machines = %d\n",
get_name(), outputs.vlen, m_num_machines);
}
SGVector<float64_t> new_outputs(m_num_classes);
new_outputs.zero();
for (int32_t j=0; j<m_num_classes; j++)
{
for (int32_t m=0; m<m_num_machines; m++)
{
if (indx1[m]==j)
new_outputs[j] += 1.0 / (outputs[m]+1E-12);
if (indx2[m]==j)
new_outputs[j] += 1.0 / (1.0-outputs[m]+1E-12);
}
new_outputs[j] = 1.0 / (new_outputs[j] - m_num_classes + 2);
}
//outputs.resize_vector(m_num_classes);
float64_t norm = SGVector<float64_t>::sum(new_outputs);
for (int32_t i=0; i<new_outputs.vlen; i++)
outputs[i] = new_outputs[i] / norm;
}
void CMulticlassOneVsOneStrategy::rescale_heuris_hastie(SGVector<float64_t> outputs,
const SGVector<int32_t> indx1, const SGVector<int32_t> indx2)
{
if (m_num_machines != outputs.vlen)
{
SG_ERROR("%s::rescale_heuris_hastie(): size(outputs) = %d != m_num_machines = %d\n",
get_name(), outputs.vlen, m_num_machines);
}
SGVector<float64_t> new_outputs(m_num_classes);
new_outputs.zero();
for (int32_t j=0; j<m_num_classes; j++)
{
for (int32_t m=0; m<m_num_machines; m++)
{
if (indx1[m]==j)
new_outputs[j] += outputs[m];
if (indx2[m]==j)
new_outputs[j] += 1.0-outputs[m];
}
new_outputs[j] *= 2.0 / (m_num_classes * (m_num_classes - 1));
new_outputs[j] += 1E-10;
}
SGVector<float64_t> mu(m_num_machines);
SGVector<float64_t> prev_outputs(m_num_classes);
float64_t gap = 1.0;
while (gap > 1E-12)
{
prev_outputs = new_outputs.clone();
for (int32_t m=0; m<m_num_machines; m++)
mu[m] = new_outputs[indx1[m]] / (new_outputs[indx1[m]] + new_outputs[indx2[m]]);
for (int32_t j=0; j<m_num_classes; j++)
{
float64_t numerator = 0.0;
float64_t denominator = 0.0;
for (int32_t m=0; m<m_num_machines; m++)
{
if (indx1[m]==j)
{
numerator += m_num_samples[m] * outputs[m];
denominator += m_num_samples[m] * mu[m];
}
if (indx2[m]==j)
{
numerator += m_num_samples[m] * (1.0-outputs[m]);
denominator += m_num_samples[m] * (1.0-mu[m]);
}
}
// update posterior
new_outputs[j] *= numerator / denominator;
}
float64_t norm = SGVector<float64_t>::sum(new_outputs);
for (int32_t i=0; i<new_outputs.vlen; i++)
new_outputs[i] /= norm;
// gap is Euclidean distance
for (int32_t i=0; i<new_outputs.vlen; i++)
prev_outputs[i] -= new_outputs[i];
gap = SGVector<float64_t>::qsq(prev_outputs.vector, prev_outputs.vlen, 2);
SG_DEBUG("[Hastie's heuristic] gap = %.12f\n", gap);
}
for (int32_t i=0; i<new_outputs.vlen; i++)
outputs[i] = new_outputs[i];
}
void CMulticlassOneVsOneStrategy::rescale_heuris_hamamura(SGVector<float64_t> outputs,
const SGVector<int32_t> indx1, const SGVector<int32_t> indx2)
{
if (m_num_machines != outputs.vlen)
{
SG_ERROR("%s::rescale_heuris_hamamura(): size(outputs) = %d != m_num_machines = %d\n",
get_name(), outputs.vlen, m_num_machines);
}
SGVector<float64_t> new_outputs(m_num_classes);
SGVector<float64_t>::fill_vector(new_outputs.vector, new_outputs.vlen, 1.0);
for (int32_t j=0; j<m_num_classes; j++)
{
for (int32_t m=0; m<m_num_machines; m++)
{
if (indx1[m]==j)
new_outputs[j] *= outputs[m];
if (indx2[m]==j)
new_outputs[j] *= 1-outputs[m];
}
new_outputs[j] += 1E-10;
}
float64_t norm = SGVector<float64_t>::sum(new_outputs);
for (int32_t i=0; i<new_outputs.vlen; i++)
outputs[i] = new_outputs[i] / norm;
}