-
Notifications
You must be signed in to change notification settings - Fork 342
/
linear.cpp
3750 lines (3319 loc) · 78.4 KB
/
linear.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <locale.h>
#include "linear.h"
#include "newton.h"
int liblinear_version = LIBLINEAR_VERSION;
typedef signed char schar;
template <class T> static inline void swap(T& x, T& y) { T t=x; x=y; y=t; }
#ifndef min
template <class T> static inline T min(T x,T y) { return (x<y)?x:y; }
#endif
#ifndef max
template <class T> static inline T max(T x,T y) { return (x>y)?x:y; }
#endif
template <class S, class T> static inline void clone(T*& dst, S* src, int n)
{
dst = new T[n];
memcpy((void *)dst,(void *)src,sizeof(T)*n);
}
#define INF HUGE_VAL
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
static void print_string_stdout(const char *s)
{
fputs(s,stdout);
fflush(stdout);
}
static void print_null(const char *s) {}
static void (*liblinear_print_string) (const char *) = &print_string_stdout;
#if 1
static void info(const char *fmt,...)
{
char buf[BUFSIZ];
va_list ap;
va_start(ap,fmt);
vsprintf(buf,fmt,ap);
va_end(ap);
(*liblinear_print_string)(buf);
}
#else
static void info(const char *fmt,...) {}
#endif
class sparse_operator
{
public:
static double nrm2_sq(const feature_node *x)
{
double ret = 0;
while(x->index != -1)
{
ret += x->value*x->value;
x++;
}
return ret;
}
static double dot(const double *s, const feature_node *x)
{
double ret = 0;
while(x->index != -1)
{
ret += s[x->index-1]*x->value;
x++;
}
return ret;
}
static double sparse_dot(const feature_node *x1, const feature_node *x2)
{
double ret = 0;
while(x1->index != -1 && x2->index != -1)
{
if(x1->index == x2->index)
{
ret += x1->value * x2->value;
++x1;
++x2;
}
else
{
if(x1->index > x2->index)
++x2;
else
++x1;
}
}
return ret;
}
static void axpy(const double a, const feature_node *x, double *y)
{
while(x->index != -1)
{
y[x->index-1] += a*x->value;
x++;
}
}
};
// L2-regularized empirical risk minimization
// min_w w^Tw/2 + \sum C_i \xi(w^Tx_i), where \xi() is the loss
class l2r_erm_fun: public function
{
public:
l2r_erm_fun(const problem *prob, const parameter *param, double *C);
~l2r_erm_fun();
double fun(double *w);
double linesearch_and_update(double *w, double *d, double *f, double *g, double alpha);
int get_nr_variable(void);
protected:
virtual double C_times_loss(int i, double wx_i) = 0;
void Xv(double *v, double *Xv);
void XTv(double *v, double *XTv);
double *C;
const problem *prob;
double *wx;
double *tmp; // a working array
double wTw;
int regularize_bias;
};
l2r_erm_fun::l2r_erm_fun(const problem *prob, const parameter *param, double *C)
{
int l=prob->l;
this->prob = prob;
wx = new double[l];
tmp = new double[l];
this->C = C;
this->regularize_bias = param->regularize_bias;
}
l2r_erm_fun::~l2r_erm_fun()
{
delete[] wx;
delete[] tmp;
}
double l2r_erm_fun::fun(double *w)
{
int i;
double f=0;
int l=prob->l;
int w_size=get_nr_variable();
wTw = 0;
Xv(w, wx);
for(i=0;i<w_size;i++)
wTw += w[i]*w[i];
if(regularize_bias == 0)
wTw -= w[w_size-1]*w[w_size-1];
for(i=0;i<l;i++)
f += C_times_loss(i, wx[i]);
f = f + 0.5 * wTw;
return f;
}
int l2r_erm_fun::get_nr_variable(void)
{
return prob->n;
}
// On entry *f must be the function value of w
// On exit w is updated and *f is the new function value
double l2r_erm_fun::linesearch_and_update(double *w, double *s, double *f, double *g, double alpha)
{
int i;
int l = prob->l;
double sTs = 0;
double wTs = 0;
double gTs = 0;
double eta = 0.01;
int w_size = get_nr_variable();
int max_num_linesearch = 20;
double fold = *f;
Xv(s, tmp);
for (i=0;i<w_size;i++)
{
sTs += s[i] * s[i];
wTs += s[i] * w[i];
gTs += s[i] * g[i];
}
if(regularize_bias == 0)
{
// bias not used in calculating (w + \alpha s)^T (w + \alpha s)
sTs -= s[w_size-1] * s[w_size-1];
wTs -= s[w_size-1] * w[w_size-1];
}
int num_linesearch = 0;
for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++)
{
double loss = 0;
for(i=0;i<l;i++)
{
double inner_product = tmp[i] * alpha + wx[i];
loss += C_times_loss(i, inner_product);
}
*f = loss + (alpha * alpha * sTs + wTw) / 2.0 + alpha * wTs;
if (*f - fold <= eta * alpha * gTs)
{
for (i=0;i<l;i++)
wx[i] += alpha * tmp[i];
break;
}
else
alpha *= 0.5;
}
if (num_linesearch >= max_num_linesearch)
{
*f = fold;
return 0;
}
else
for (i=0;i<w_size;i++)
w[i] += alpha * s[i];
wTw += alpha * alpha * sTs + 2* alpha * wTs;
return alpha;
}
void l2r_erm_fun::Xv(double *v, double *Xv)
{
int i;
int l=prob->l;
feature_node **x=prob->x;
for(i=0;i<l;i++)
Xv[i]=sparse_operator::dot(v, x[i]);
}
void l2r_erm_fun::XTv(double *v, double *XTv)
{
int i;
int l=prob->l;
int w_size=get_nr_variable();
feature_node **x=prob->x;
for(i=0;i<w_size;i++)
XTv[i]=0;
for(i=0;i<l;i++)
sparse_operator::axpy(v[i], x[i], XTv);
}
class l2r_lr_fun: public l2r_erm_fun
{
public:
l2r_lr_fun(const problem *prob, const parameter *param, double *C);
~l2r_lr_fun();
void grad(double *w, double *g);
void Hv(double *s, double *Hs);
void get_diag_preconditioner(double *M);
private:
double *D;
double C_times_loss(int i, double wx_i);
};
l2r_lr_fun::l2r_lr_fun(const problem *prob, const parameter *param, double *C):
l2r_erm_fun(prob, param, C)
{
int l=prob->l;
D = new double[l];
}
l2r_lr_fun::~l2r_lr_fun()
{
delete[] D;
}
double l2r_lr_fun::C_times_loss(int i, double wx_i)
{
double ywx_i = wx_i * prob->y[i];
if (ywx_i >= 0)
return C[i]*log(1 + exp(-ywx_i));
else
return C[i]*(-ywx_i + log(1 + exp(ywx_i)));
}
void l2r_lr_fun::grad(double *w, double *g)
{
int i;
double *y=prob->y;
int l=prob->l;
int w_size=get_nr_variable();
for(i=0;i<l;i++)
{
tmp[i] = 1/(1 + exp(-y[i]*wx[i]));
D[i] = tmp[i]*(1-tmp[i]);
tmp[i] = C[i]*(tmp[i]-1)*y[i];
}
XTv(tmp, g);
for(i=0;i<w_size;i++)
g[i] = w[i] + g[i];
if(regularize_bias == 0)
g[w_size-1] -= w[w_size-1];
}
void l2r_lr_fun::get_diag_preconditioner(double *M)
{
int i;
int l = prob->l;
int w_size=get_nr_variable();
feature_node **x = prob->x;
for (i=0; i<w_size; i++)
M[i] = 1;
if(regularize_bias == 0)
M[w_size-1] = 0;
for (i=0; i<l; i++)
{
feature_node *xi = x[i];
while (xi->index!=-1)
{
M[xi->index-1] += xi->value*xi->value*C[i]*D[i];
xi++;
}
}
}
void l2r_lr_fun::Hv(double *s, double *Hs)
{
int i;
int l=prob->l;
int w_size=get_nr_variable();
feature_node **x=prob->x;
for(i=0;i<w_size;i++)
Hs[i] = 0;
for(i=0;i<l;i++)
{
feature_node * const xi=x[i];
double xTs = sparse_operator::dot(s, xi);
xTs = C[i]*D[i]*xTs;
sparse_operator::axpy(xTs, xi, Hs);
}
for(i=0;i<w_size;i++)
Hs[i] = s[i] + Hs[i];
if(regularize_bias == 0)
Hs[w_size-1] -= s[w_size-1];
}
class l2r_l2_svc_fun: public l2r_erm_fun
{
public:
l2r_l2_svc_fun(const problem *prob, const parameter *param, double *C);
~l2r_l2_svc_fun();
void grad(double *w, double *g);
void Hv(double *s, double *Hs);
void get_diag_preconditioner(double *M);
protected:
void subXTv(double *v, double *XTv);
int *I;
int sizeI;
private:
double C_times_loss(int i, double wx_i);
};
l2r_l2_svc_fun::l2r_l2_svc_fun(const problem *prob, const parameter *param, double *C):
l2r_erm_fun(prob, param, C)
{
I = new int[prob->l];
}
l2r_l2_svc_fun::~l2r_l2_svc_fun()
{
delete[] I;
}
double l2r_l2_svc_fun::C_times_loss(int i, double wx_i)
{
double d = 1 - prob->y[i] * wx_i;
if (d > 0)
return C[i] * d * d;
else
return 0;
}
void l2r_l2_svc_fun::grad(double *w, double *g)
{
int i;
double *y=prob->y;
int l=prob->l;
int w_size=get_nr_variable();
sizeI = 0;
for (i=0;i<l;i++)
{
tmp[i] = wx[i] * y[i];
if (tmp[i] < 1)
{
tmp[sizeI] = C[i]*y[i]*(tmp[i]-1);
I[sizeI] = i;
sizeI++;
}
}
subXTv(tmp, g);
for(i=0;i<w_size;i++)
g[i] = w[i] + 2*g[i];
if(regularize_bias == 0)
g[w_size-1] -= w[w_size-1];
}
void l2r_l2_svc_fun::get_diag_preconditioner(double *M)
{
int i;
int w_size=get_nr_variable();
feature_node **x = prob->x;
for (i=0; i<w_size; i++)
M[i] = 1;
if(regularize_bias == 0)
M[w_size-1] = 0;
for (i=0; i<sizeI; i++)
{
int idx = I[i];
feature_node *xi = x[idx];
while (xi->index!=-1)
{
M[xi->index-1] += xi->value*xi->value*C[idx]*2;
xi++;
}
}
}
void l2r_l2_svc_fun::Hv(double *s, double *Hs)
{
int i;
int w_size=get_nr_variable();
feature_node **x=prob->x;
for(i=0;i<w_size;i++)
Hs[i]=0;
for(i=0;i<sizeI;i++)
{
feature_node * const xi=x[I[i]];
double xTs = sparse_operator::dot(s, xi);
xTs = C[I[i]]*xTs;
sparse_operator::axpy(xTs, xi, Hs);
}
for(i=0;i<w_size;i++)
Hs[i] = s[i] + 2*Hs[i];
if(regularize_bias == 0)
Hs[w_size-1] -= s[w_size-1];
}
void l2r_l2_svc_fun::subXTv(double *v, double *XTv)
{
int i;
int w_size=get_nr_variable();
feature_node **x=prob->x;
for(i=0;i<w_size;i++)
XTv[i]=0;
for(i=0;i<sizeI;i++)
sparse_operator::axpy(v[i], x[I[i]], XTv);
}
class l2r_l2_svr_fun: public l2r_l2_svc_fun
{
public:
l2r_l2_svr_fun(const problem *prob, const parameter *param, double *C);
void grad(double *w, double *g);
private:
double C_times_loss(int i, double wx_i);
double p;
};
l2r_l2_svr_fun::l2r_l2_svr_fun(const problem *prob, const parameter *param, double *C):
l2r_l2_svc_fun(prob, param, C)
{
this->p = param->p;
this->regularize_bias = param->regularize_bias;
}
double l2r_l2_svr_fun::C_times_loss(int i, double wx_i)
{
double d = wx_i - prob->y[i];
if(d < -p)
return C[i]*(d+p)*(d+p);
else if(d > p)
return C[i]*(d-p)*(d-p);
return 0;
}
void l2r_l2_svr_fun::grad(double *w, double *g)
{
int i;
double *y=prob->y;
int l=prob->l;
int w_size=get_nr_variable();
double d;
sizeI = 0;
for(i=0;i<l;i++)
{
d = wx[i] - y[i];
// generate index set I
if(d < -p)
{
tmp[sizeI] = C[i]*(d+p);
I[sizeI] = i;
sizeI++;
}
else if(d > p)
{
tmp[sizeI] = C[i]*(d-p);
I[sizeI] = i;
sizeI++;
}
}
subXTv(tmp, g);
for(i=0;i<w_size;i++)
g[i] = w[i] + 2*g[i];
if(regularize_bias == 0)
g[w_size-1] -= w[w_size-1];
}
// A coordinate descent algorithm for
// multi-class support vector machines by Crammer and Singer
//
// min_{\alpha} 0.5 \sum_m ||w_m(\alpha)||^2 + \sum_i \sum_m e^m_i alpha^m_i
// s.t. \alpha^m_i <= C^m_i \forall m,i , \sum_m \alpha^m_i=0 \forall i
//
// where e^m_i = 0 if y_i = m,
// e^m_i = 1 if y_i != m,
// C^m_i = C if m = y_i,
// C^m_i = 0 if m != y_i,
// and w_m(\alpha) = \sum_i \alpha^m_i x_i
//
// Given:
// x, y, C
// eps is the stopping tolerance
//
// solution will be put in w
//
// See Appendix of LIBLINEAR paper, Fan et al. (2008)
#define GETI(i) ((int) prob->y[i])
// To support weights for instances, use GETI(i) (i)
class Solver_MCSVM_CS
{
public:
Solver_MCSVM_CS(const problem *prob, int nr_class, double *C, double eps=0.1, int max_iter=100000);
~Solver_MCSVM_CS();
void Solve(double *w);
private:
void solve_sub_problem(double A_i, int yi, double C_yi, int active_i, double *alpha_new);
bool be_shrunk(int i, int m, int yi, double alpha_i, double minG);
double *B, *C, *G;
int w_size, l;
int nr_class;
int max_iter;
double eps;
const problem *prob;
};
Solver_MCSVM_CS::Solver_MCSVM_CS(const problem *prob, int nr_class, double *weighted_C, double eps, int max_iter)
{
this->w_size = prob->n;
this->l = prob->l;
this->nr_class = nr_class;
this->eps = eps;
this->max_iter = max_iter;
this->prob = prob;
this->B = new double[nr_class];
this->G = new double[nr_class];
this->C = weighted_C;
}
Solver_MCSVM_CS::~Solver_MCSVM_CS()
{
delete[] B;
delete[] G;
}
int compare_double(const void *a, const void *b)
{
if(*(double *)a > *(double *)b)
return -1;
if(*(double *)a < *(double *)b)
return 1;
return 0;
}
void Solver_MCSVM_CS::solve_sub_problem(double A_i, int yi, double C_yi, int active_i, double *alpha_new)
{
int r;
double *D;
clone(D, B, active_i);
if(yi < active_i)
D[yi] += A_i*C_yi;
qsort(D, active_i, sizeof(double), compare_double);
double beta = D[0] - A_i*C_yi;
for(r=1;r<active_i && beta<r*D[r];r++)
beta += D[r];
beta /= r;
for(r=0;r<active_i;r++)
{
if(r == yi)
alpha_new[r] = min(C_yi, (beta-B[r])/A_i);
else
alpha_new[r] = min((double)0, (beta - B[r])/A_i);
}
delete[] D;
}
bool Solver_MCSVM_CS::be_shrunk(int i, int m, int yi, double alpha_i, double minG)
{
double bound = 0;
if(m == yi)
bound = C[GETI(i)];
if(alpha_i == bound && G[m] < minG)
return true;
return false;
}
void Solver_MCSVM_CS::Solve(double *w)
{
int i, m, s;
int iter = 0;
double *alpha = new double[l*nr_class];
double *alpha_new = new double[nr_class];
int *index = new int[l];
double *QD = new double[l];
int *d_ind = new int[nr_class];
double *d_val = new double[nr_class];
int *alpha_index = new int[nr_class*l];
int *y_index = new int[l];
int active_size = l;
int *active_size_i = new int[l];
double eps_shrink = max(10.0*eps, 1.0); // stopping tolerance for shrinking
bool start_from_all = true;
// Initial alpha can be set here. Note that
// sum_m alpha[i*nr_class+m] = 0, for all i=1,...,l-1
// alpha[i*nr_class+m] <= C[GETI(i)] if prob->y[i] == m
// alpha[i*nr_class+m] <= 0 if prob->y[i] != m
// If initial alpha isn't zero, uncomment the for loop below to initialize w
for(i=0;i<l*nr_class;i++)
alpha[i] = 0;
for(i=0;i<w_size*nr_class;i++)
w[i] = 0;
for(i=0;i<l;i++)
{
for(m=0;m<nr_class;m++)
alpha_index[i*nr_class+m] = m;
feature_node *xi = prob->x[i];
QD[i] = 0;
while(xi->index != -1)
{
double val = xi->value;
QD[i] += val*val;
// Uncomment the for loop if initial alpha isn't zero
// for(m=0; m<nr_class; m++)
// w[(xi->index-1)*nr_class+m] += alpha[i*nr_class+m]*val;
xi++;
}
active_size_i[i] = nr_class;
y_index[i] = (int)prob->y[i];
index[i] = i;
}
while(iter < max_iter)
{
double stopping = -INF;
for(i=0;i<active_size;i++)
{
int j = i+rand()%(active_size-i);
swap(index[i], index[j]);
}
for(s=0;s<active_size;s++)
{
i = index[s];
double Ai = QD[i];
double *alpha_i = &alpha[i*nr_class];
int *alpha_index_i = &alpha_index[i*nr_class];
if(Ai > 0)
{
for(m=0;m<active_size_i[i];m++)
G[m] = 1;
if(y_index[i] < active_size_i[i])
G[y_index[i]] = 0;
feature_node *xi = prob->x[i];
while(xi->index!= -1)
{
double *w_i = &w[(xi->index-1)*nr_class];
for(m=0;m<active_size_i[i];m++)
G[m] += w_i[alpha_index_i[m]]*(xi->value);
xi++;
}
double minG = INF;
double maxG = -INF;
for(m=0;m<active_size_i[i];m++)
{
if(alpha_i[alpha_index_i[m]] < 0 && G[m] < minG)
minG = G[m];
if(G[m] > maxG)
maxG = G[m];
}
if(y_index[i] < active_size_i[i])
if(alpha_i[(int) prob->y[i]] < C[GETI(i)] && G[y_index[i]] < minG)
minG = G[y_index[i]];
for(m=0;m<active_size_i[i];m++)
{
if(be_shrunk(i, m, y_index[i], alpha_i[alpha_index_i[m]], minG))
{
active_size_i[i]--;
while(active_size_i[i]>m)
{
if(!be_shrunk(i, active_size_i[i], y_index[i],
alpha_i[alpha_index_i[active_size_i[i]]], minG))
{
swap(alpha_index_i[m], alpha_index_i[active_size_i[i]]);
swap(G[m], G[active_size_i[i]]);
if(y_index[i] == active_size_i[i])
y_index[i] = m;
else if(y_index[i] == m)
y_index[i] = active_size_i[i];
break;
}
active_size_i[i]--;
}
}
}
if(active_size_i[i] <= 1)
{
active_size--;
swap(index[s], index[active_size]);
s--;
continue;
}
if(maxG-minG <= 1e-12)
continue;
else
stopping = max(maxG - minG, stopping);
for(m=0;m<active_size_i[i];m++)
B[m] = G[m] - Ai*alpha_i[alpha_index_i[m]] ;
solve_sub_problem(Ai, y_index[i], C[GETI(i)], active_size_i[i], alpha_new);
int nz_d = 0;
for(m=0;m<active_size_i[i];m++)
{
double d = alpha_new[m] - alpha_i[alpha_index_i[m]];
alpha_i[alpha_index_i[m]] = alpha_new[m];
if(fabs(d) >= 1e-12)
{
d_ind[nz_d] = alpha_index_i[m];
d_val[nz_d] = d;
nz_d++;
}
}
xi = prob->x[i];
while(xi->index != -1)
{
double *w_i = &w[(xi->index-1)*nr_class];
for(m=0;m<nz_d;m++)
w_i[d_ind[m]] += d_val[m]*xi->value;
xi++;
}
}
}
iter++;
if(iter % 10 == 0)
{
info(".");
}
if(stopping < eps_shrink)
{
if(stopping < eps && start_from_all == true)
break;
else
{
active_size = l;
for(i=0;i<l;i++)
active_size_i[i] = nr_class;
info("*");
eps_shrink = max(eps_shrink/2, eps);
start_from_all = true;
}
}
else
start_from_all = false;
}
info("\noptimization finished, #iter = %d\n",iter);
if (iter >= max_iter)
info("\nWARNING: reaching max number of iterations\n");
// calculate objective value
double v = 0;
int nSV = 0;
for(i=0;i<w_size*nr_class;i++)
v += w[i]*w[i];
v = 0.5*v;
for(i=0;i<l*nr_class;i++)
{
v += alpha[i];
if(fabs(alpha[i]) > 0)
nSV++;
}
for(i=0;i<l;i++)
v -= alpha[i*nr_class+(int)prob->y[i]];
info("Objective value = %lf\n",v);
info("nSV = %d\n",nSV);
delete [] alpha;
delete [] alpha_new;
delete [] index;
delete [] QD;
delete [] d_ind;
delete [] d_val;
delete [] alpha_index;
delete [] y_index;
delete [] active_size_i;
}
// A coordinate descent algorithm for
// L1-loss and L2-loss SVM dual problems
//
// min_\alpha 0.5(\alpha^T (Q + D)\alpha) - e^T \alpha,
// s.t. 0 <= \alpha_i <= upper_bound_i,
//
// where Qij = yi yj xi^T xj and
// D is a diagonal matrix
//
// In L1-SVM case:
// upper_bound_i = Cp if y_i = 1
// upper_bound_i = Cn if y_i = -1
// D_ii = 0
// In L2-SVM case:
// upper_bound_i = INF
// D_ii = 1/(2*Cp) if y_i = 1
// D_ii = 1/(2*Cn) if y_i = -1
//
// Given:
// x, y, Cp, Cn
// eps is the stopping tolerance
//
// solution will be put in w
//
// this function returns the number of iterations
//
// See Algorithm 3 of Hsieh et al., ICML 2008
#undef GETI
#define GETI(i) (y[i]+1)
// To support weights for instances, use GETI(i) (i)
static int solve_l2r_l1l2_svc(const problem *prob, const parameter *param, double *w, double Cp, double Cn, int max_iter=300)
{
int l = prob->l;
int w_size = prob->n;
double eps = param->eps;
int solver_type = param->solver_type;
int i, s, iter = 0;
double C, d, G;
double *QD = new double[l];
int *index = new int[l];
double *alpha = new double[l];
schar *y = new schar[l];
int active_size = l;
// PG: projected gradient, for shrinking and stopping
double PG;
double PGmax_old = INF;
double PGmin_old = -INF;
double PGmax_new, PGmin_new;
// default solver_type: L2R_L2LOSS_SVC_DUAL
double diag[3] = {0.5/Cn, 0, 0.5/Cp};
double upper_bound[3] = {INF, 0, INF};
if(solver_type == L2R_L1LOSS_SVC_DUAL)
{
diag[0] = 0;
diag[2] = 0;
upper_bound[0] = Cn;
upper_bound[2] = Cp;
}
for(i=0; i<l; i++)
{
if(prob->y[i] > 0)
{
y[i] = +1;
}
else
{
y[i] = -1;
}
}
// Initial alpha can be set here. Note that
// 0 <= alpha[i] <= upper_bound[GETI(i)]
for(i=0; i<l; i++)
alpha[i] = 0;
for(i=0; i<w_size; i++)
w[i] = 0;
for(i=0; i<l; i++)
{
QD[i] = diag[GETI(i)];
feature_node * const xi = prob->x[i];
QD[i] += sparse_operator::nrm2_sq(xi);
sparse_operator::axpy(y[i]*alpha[i], xi, w);
index[i] = i;
}
while (iter < max_iter)
{
PGmax_new = -INF;
PGmin_new = INF;
for (i=0; i<active_size; i++)
{
int j = i+rand()%(active_size-i);
swap(index[i], index[j]);
}
for (s=0; s<active_size; s++)
{
i = index[s];
const schar yi = y[i];
feature_node * const xi = prob->x[i];
G = yi*sparse_operator::dot(w, xi)-1;
C = upper_bound[GETI(i)];
G += alpha[i]*diag[GETI(i)];
PG = 0;
if (alpha[i] == 0)
{
if (G > PGmax_old)
{
active_size--;
swap(index[s], index[active_size]);
s--;
continue;
}
else if (G < 0)
PG = G;
}
else if (alpha[i] == C)
{
if (G < PGmin_old)
{