| 1 |
|
| #include <math.h> |
| 2 |
|
| #include <stdio.h> |
| 3 |
|
| #include <stdlib.h> |
| 4 |
|
| #include <string.h> |
| 5 |
|
| #include <stdarg.h> |
| 6 |
|
| #include <locale.h> |
| 7 |
|
| #include "linear.h" |
| 8 |
|
| #include "newton.h" |
| 9 |
|
| int liblinear_version = LIBLINEAR_VERSION; |
| 10 |
|
| typedef signed char schar; |
| 11 |
0 |
| template <class T> static inline void swap(T& x, T& y) { T t=x; x=y; y=t; } |
| 12 |
|
| #ifndef min |
| 13 |
60828 |
▶ | template <class T> static inline T min(T x,T y) { return (x<y)?x:y; } |
Branch: 2 branches
| Branch | True | False | Total |
| template <class T> static inline T min(T x,T y) { return (x<y)?x:y; } |
78 |
0 |
78 |
| template <class T> static inline T min(T x,T y) { return (x<y)?x:y; } |
59862 |
888 |
60750 |
|
| 14 |
|
| #endif |
| 15 |
|
| #ifndef max |
| 16 |
60826 |
▶ | template <class T> static inline T max(T x,T y) { return (x>y)?x:y; } |
Branch: 2 branches
| Branch | True | False | Total |
| template <class T> static inline T max(T x,T y) { return (x>y)?x:y; } |
78 |
0 |
78 |
| template <class T> static inline T max(T x,T y) { return (x>y)?x:y; } |
58956 |
1792 |
60748 |
|
| 17 |
|
| #endif |
| 18 |
0 |
| template <class S, class T> static inline void clone(T*& dst, S* src, int n) |
| 19 |
|
| { |
| 20 |
0 |
▶ | dst = new T[n]; |
Branch: 1 branch
| Branch | True | False | Total |
| dst = new T[n]; |
0 |
0 |
0 |
|
| 21 |
0 |
| memcpy((void *)dst,(void *)src,sizeof(T)*n); |
| 22 |
0 |
| } |
| 23 |
|
| #define INF HUGE_VAL |
| 24 |
|
| #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) |
| 25 |
|
| |
| 26 |
0 |
| static void print_string_stdout(const char *s) |
| 27 |
|
| { |
| 28 |
0 |
| fputs(s,stdout); |
| 29 |
0 |
| fflush(stdout); |
| 30 |
0 |
| } |
| 31 |
109 |
| static void print_null(const char *s) {} |
| 32 |
|
| |
| 33 |
|
| static void (*liblinear_print_string) (const char *) = &print_string_stdout; |
| 34 |
|
| |
| 35 |
|
| #if 1 |
| 36 |
36 |
| static void info(const char *fmt,...) |
| 37 |
|
| { |
| 38 |
|
| char buf[BUFSIZ]; |
| 39 |
|
| va_list ap; |
| 40 |
36 |
| va_start(ap,fmt); |
| 41 |
36 |
| vsprintf(buf,fmt,ap); |
| 42 |
36 |
| va_end(ap); |
| 43 |
36 |
▶ | (*liblinear_print_string)(buf); |
Branch: 1 branch
| Branch | True | False | Total |
| (*liblinear_print_string)(buf); |
36 |
0 |
36 |
|
| 44 |
36 |
| } |
| 45 |
|
| #else |
| 46 |
|
| static void info(const char *fmt,...) {} |
| 47 |
|
| #endif |
| 48 |
|
| class sparse_operator |
| 49 |
|
| { |
| 50 |
|
| public: |
| 51 |
540 |
| static double nrm2_sq(const feature_node *x) |
| 52 |
|
| { |
| 53 |
540 |
| double ret = 0; |
| 54 |
7296 |
▶ | while(x->index != -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(x->index != -1) |
6756 |
540 |
7296 |
|
| 55 |
|
| { |
| 56 |
6756 |
| ret += x->value*x->value; |
| 57 |
6756 |
| x++; |
| 58 |
|
| } |
| 59 |
540 |
| return ret; |
| 60 |
|
| } |
| 61 |
|
| |
| 62 |
112110 |
| static double dot(const double *s, const feature_node *x) |
| 63 |
|
| { |
| 64 |
112110 |
| double ret = 0; |
| 65 |
1514389 |
▶ | while(x->index != -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(x->index != -1) |
1402279 |
112110 |
1514389 |
|
| 66 |
|
| { |
| 67 |
1402279 |
| ret += s[x->index-1]*x->value; |
| 68 |
1402279 |
| x++; |
| 69 |
|
| } |
| 70 |
112110 |
| return ret; |
| 71 |
|
| } |
| 72 |
|
| |
| 73 |
0 |
| static double sparse_dot(const feature_node *x1, const feature_node *x2) |
| 74 |
|
| { |
| 75 |
0 |
| double ret = 0; |
| 76 |
0 |
▶ | while(x1->index != -1 && x2->index != -1) |
Branch: 2 branches
| Branch | True | False | Total |
| while(x1->index != -1 && x2->index != -1) |
0 |
0 |
0 |
| while(x1->index != -1 && x2->index != -1) |
0 |
0 |
0 |
|
| 77 |
|
| { |
| 78 |
0 |
▶ | if(x1->index == x2->index) |
Branch: 1 branch
| Branch | True | False | Total |
| if(x1->index == x2->index) |
0 |
0 |
0 |
|
| 79 |
|
| { |
| 80 |
0 |
| ret += x1->value * x2->value; |
| 81 |
0 |
| ++x1; |
| 82 |
0 |
| ++x2; |
| 83 |
|
| } |
| 84 |
|
| else |
| 85 |
|
| { |
| 86 |
0 |
▶ | if(x1->index > x2->index) |
Branch: 1 branch
| Branch | True | False | Total |
| if(x1->index > x2->index) |
0 |
0 |
0 |
|
| 87 |
0 |
| ++x2; |
| 88 |
|
| else |
| 89 |
0 |
| ++x1; |
| 90 |
|
| } |
| 91 |
|
| } |
| 92 |
0 |
| return ret; |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
102493 |
| static void axpy(const double a, const feature_node *x, double *y) |
| 96 |
|
| { |
| 97 |
1384147 |
▶ | while(x->index != -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(x->index != -1) |
1281654 |
102493 |
1384147 |
|
| 98 |
|
| { |
| 99 |
1281654 |
| y[x->index-1] += a*x->value; |
| 100 |
1281654 |
| x++; |
| 101 |
|
| } |
| 102 |
102493 |
| } |
| 103 |
|
| }; |
| 104 |
|
| |
| 105 |
|
| // L2-regularized empirical risk minimization |
| 106 |
|
| // min_w w^Tw/2 + \sum C_i \xi(w^Tx_i), where \xi() is the loss |
| 107 |
|
| |
| 108 |
|
| class l2r_erm_fun: public function |
| 109 |
|
| { |
| 110 |
|
| public: |
| 111 |
|
| l2r_erm_fun(const problem *prob, const parameter *param, double *C); |
| 112 |
|
| ~l2r_erm_fun(); |
| 113 |
|
| |
| 114 |
|
| double fun(double *w); |
| 115 |
|
| double linesearch_and_update(double *w, double *d, double *f, double *g, double alpha); |
| 116 |
|
| int get_nr_variable(void); |
| 117 |
|
| |
| 118 |
|
| protected: |
| 119 |
|
| virtual double C_times_loss(int i, double wx_i) = 0; |
| 120 |
|
| void Xv(double *v, double *Xv); |
| 121 |
|
| void XTv(double *v, double *XTv); |
| 122 |
|
| |
| 123 |
|
| double *C; |
| 124 |
|
| const problem *prob; |
| 125 |
|
| double *wx; |
| 126 |
|
| double *tmp; // a working array |
| 127 |
|
| double wTw; |
| 128 |
|
| int regularize_bias; |
| 129 |
|
| }; |
| 130 |
|
| |
| 131 |
76 |
| l2r_erm_fun::l2r_erm_fun(const problem *prob, const parameter *param, double *C) |
| 132 |
|
| { |
| 133 |
76 |
| int l=prob->l; |
| 134 |
|
| |
| 135 |
76 |
| this->prob = prob; |
| 136 |
|
| |
| 137 |
76 |
▶ | wx = new double[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| wx = new double[l]; |
76 |
0 |
76 |
| wx = new double[l]; |
76 |
0 |
76 |
|
| 138 |
76 |
▶ | tmp = new double[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| tmp = new double[l]; |
76 |
0 |
76 |
| tmp = new double[l]; |
76 |
0 |
76 |
|
| 139 |
76 |
| this->C = C; |
| 140 |
76 |
| this->regularize_bias = param->regularize_bias; |
| 141 |
76 |
| } |
| 142 |
|
| |
| 143 |
0 |
| l2r_erm_fun::~l2r_erm_fun() |
| 144 |
|
| { |
| 145 |
76 |
▶ | delete[] wx; |
Branch: 1 branch
| Branch | True | False | Total |
| delete[] wx; |
76 |
0 |
76 |
|
| 146 |
76 |
▶ | delete[] tmp; |
Branch: 1 branch
| Branch | True | False | Total |
| delete[] tmp; |
76 |
0 |
76 |
|
| 147 |
0 |
| } |
| 148 |
|
| |
| 149 |
152 |
| double l2r_erm_fun::fun(double *w) |
| 150 |
|
| { |
| 151 |
|
| int i; |
| 152 |
152 |
| double f=0; |
| 153 |
152 |
| int l=prob->l; |
| 154 |
152 |
| int w_size=get_nr_variable(); |
| 155 |
|
| |
| 156 |
152 |
| wTw = 0; |
| 157 |
152 |
| Xv(w, wx); |
| 158 |
|
| |
| 159 |
2128 |
▶ | for(i=0;i<w_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<w_size;i++) |
1976 |
152 |
2128 |
|
| 160 |
1976 |
| wTw += w[i]*w[i]; |
| 161 |
152 |
▶ | if(regularize_bias == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(regularize_bias == 0) |
0 |
152 |
152 |
|
| 162 |
0 |
| wTw -= w[w_size-1]*w[w_size-1]; |
| 163 |
33092 |
▶ | for(i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) |
32940 |
152 |
33092 |
|
| 164 |
32940 |
| f += C_times_loss(i, wx[i]); |
| 165 |
152 |
| f = f + 0.5 * wTw; |
| 166 |
|
| |
| 167 |
152 |
| return f; |
| 168 |
|
| } |
| 169 |
|
| |
| 170 |
1037 |
| int l2r_erm_fun::get_nr_variable(void) |
| 171 |
|
| { |
| 172 |
1037 |
| return prob->n; |
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
|
| // On entry *f must be the function value of w |
| 176 |
|
| // On exit w is updated and *f is the new function value |
| 177 |
63 |
| double l2r_erm_fun::linesearch_and_update(double *w, double *s, double *f, double *g, double alpha) |
| 178 |
|
| { |
| 179 |
|
| int i; |
| 180 |
63 |
| int l = prob->l; |
| 181 |
63 |
| double sTs = 0; |
| 182 |
63 |
| double wTs = 0; |
| 183 |
63 |
| double gTs = 0; |
| 184 |
63 |
| double eta = 0.01; |
| 185 |
63 |
| int w_size = get_nr_variable(); |
| 186 |
63 |
| int max_num_linesearch = 20; |
| 187 |
63 |
| double fold = *f; |
| 188 |
63 |
| Xv(s, tmp); |
| 189 |
|
| |
| 190 |
882 |
▶ | for (i=0;i<w_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0;i<w_size;i++) |
819 |
63 |
882 |
|
| 191 |
|
| { |
| 192 |
819 |
| sTs += s[i] * s[i]; |
| 193 |
819 |
| wTs += s[i] * w[i]; |
| 194 |
819 |
| gTs += s[i] * g[i]; |
| 195 |
|
| } |
| 196 |
63 |
▶ | if(regularize_bias == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(regularize_bias == 0) |
0 |
63 |
63 |
|
| 197 |
|
| { |
| 198 |
|
| // bias not used in calculating (w + \alpha s)^T (w + \alpha s) |
| 199 |
0 |
| sTs -= s[w_size-1] * s[w_size-1]; |
| 200 |
0 |
| wTs -= s[w_size-1] * w[w_size-1]; |
| 201 |
|
| } |
| 202 |
|
| |
| 203 |
63 |
| int num_linesearch = 0; |
| 204 |
63 |
▶ | for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++) |
63 |
0 |
63 |
|
| 205 |
|
| { |
| 206 |
63 |
| double loss = 0; |
| 207 |
13887 |
▶ | for(i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) |
13824 |
63 |
13887 |
|
| 208 |
|
| { |
| 209 |
13824 |
| double inner_product = tmp[i] * alpha + wx[i]; |
| 210 |
13824 |
| loss += C_times_loss(i, inner_product); |
| 211 |
|
| } |
| 212 |
63 |
| *f = loss + (alpha * alpha * sTs + wTw) / 2.0 + alpha * wTs; |
| 213 |
63 |
▶ | if (*f - fold <= eta * alpha * gTs) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*f - fold <= eta * alpha * gTs) |
63 |
0 |
63 |
|
| 214 |
|
| { |
| 215 |
13887 |
▶ | for (i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0;i<l;i++) |
13824 |
63 |
13887 |
|
| 216 |
13824 |
| wx[i] += alpha * tmp[i]; |
| 217 |
63 |
| break; |
| 218 |
|
| } |
| 219 |
|
| else |
| 220 |
0 |
| alpha *= 0.5; |
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
63 |
▶ | if (num_linesearch >= max_num_linesearch) |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_linesearch >= max_num_linesearch) |
0 |
63 |
63 |
|
| 224 |
|
| { |
| 225 |
0 |
| *f = fold; |
| 226 |
0 |
| return 0; |
| 227 |
|
| } |
| 228 |
|
| else |
| 229 |
882 |
▶ | for (i=0;i<w_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0;i<w_size;i++) |
819 |
63 |
882 |
|
| 230 |
819 |
| w[i] += alpha * s[i]; |
| 231 |
|
| |
| 232 |
63 |
| wTw += alpha * alpha * sTs + 2* alpha * wTs; |
| 233 |
63 |
| return alpha; |
| 234 |
|
| } |
| 235 |
|
| |
| 236 |
215 |
| void l2r_erm_fun::Xv(double *v, double *Xv) |
| 237 |
|
| { |
| 238 |
|
| int i; |
| 239 |
215 |
| int l=prob->l; |
| 240 |
215 |
| feature_node **x=prob->x; |
| 241 |
|
| |
| 242 |
46979 |
▶ | for(i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) |
46764 |
215 |
46979 |
|
| 243 |
46764 |
| Xv[i]=sparse_operator::dot(v, x[i]); |
| 244 |
215 |
| } |
| 245 |
|
| |
| 246 |
0 |
| void l2r_erm_fun::XTv(double *v, double *XTv) |
| 247 |
|
| { |
| 248 |
|
| int i; |
| 249 |
0 |
| int l=prob->l; |
| 250 |
0 |
| int w_size=get_nr_variable(); |
| 251 |
0 |
| feature_node **x=prob->x; |
| 252 |
|
| |
| 253 |
0 |
▶ | for(i=0;i<w_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<w_size;i++) |
0 |
0 |
0 |
|
| 254 |
0 |
| XTv[i]=0; |
| 255 |
0 |
▶ | for(i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) |
0 |
0 |
0 |
|
| 256 |
0 |
| sparse_operator::axpy(v[i], x[i], XTv); |
| 257 |
0 |
| } |
| 258 |
|
| |
| 259 |
|
| class l2r_lr_fun: public l2r_erm_fun |
| 260 |
|
| { |
| 261 |
|
| public: |
| 262 |
|
| l2r_lr_fun(const problem *prob, const parameter *param, double *C); |
| 263 |
|
| ~l2r_lr_fun(); |
| 264 |
|
| |
| 265 |
|
| void grad(double *w, double *g); |
| 266 |
|
| void Hv(double *s, double *Hs); |
| 267 |
|
| |
| 268 |
|
| void get_diag_preconditioner(double *M); |
| 269 |
|
| |
| 270 |
|
| private: |
| 271 |
|
| double *D; |
| 272 |
|
| double C_times_loss(int i, double wx_i); |
| 273 |
|
| }; |
| 274 |
|
| |
| 275 |
0 |
| l2r_lr_fun::l2r_lr_fun(const problem *prob, const parameter *param, double *C): |
| 276 |
0 |
| l2r_erm_fun(prob, param, C) |
| 277 |
|
| { |
| 278 |
0 |
| int l=prob->l; |
| 279 |
0 |
▶ | D = new double[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| D = new double[l]; |
0 |
0 |
0 |
| D = new double[l]; |
0 |
0 |
0 |
|
| 280 |
0 |
| } |
| 281 |
|
| |
| 282 |
0 |
| l2r_lr_fun::~l2r_lr_fun() |
| 283 |
|
| { |
| 284 |
0 |
▶ | delete[] D; |
Branch: 1 branch
| Branch | True | False | Total |
| delete[] D; |
0 |
0 |
0 |
|
| 285 |
0 |
| } |
| 286 |
|
| |
| 287 |
0 |
| double l2r_lr_fun::C_times_loss(int i, double wx_i) |
| 288 |
|
| { |
| 289 |
0 |
| double ywx_i = wx_i * prob->y[i]; |
| 290 |
0 |
▶ | if (ywx_i >= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ywx_i >= 0) |
0 |
0 |
0 |
|
| 291 |
0 |
| return C[i]*log(1 + exp(-ywx_i)); |
| 292 |
|
| else |
| 293 |
0 |
| return C[i]*(-ywx_i + log(1 + exp(ywx_i))); |
| 294 |
|
| } |
| 295 |
|
| |
| 296 |
0 |
| void l2r_lr_fun::grad(double *w, double *g) |
| 297 |
|
| { |
| 298 |
|
| int i; |
| 299 |
0 |
| double *y=prob->y; |
| 300 |
0 |
| int l=prob->l; |
| 301 |
0 |
| int w_size=get_nr_variable(); |
| 302 |
|
| |
| 303 |
0 |
▶ | for(i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) |
0 |
0 |
0 |
|
| 304 |
|
| { |
| 305 |
0 |
| tmp[i] = 1/(1 + exp(-y[i]*wx[i])); |
| 306 |
0 |
| D[i] = tmp[i]*(1-tmp[i]); |
| 307 |
0 |
| tmp[i] = C[i]*(tmp[i]-1)*y[i]; |
| 308 |
|
| } |
| 309 |
0 |
| XTv(tmp, g); |
| 310 |
|
| |
| 311 |
0 |
▶ | for(i=0;i<w_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<w_size;i++) |
0 |
0 |
0 |
|
| 312 |
0 |
| g[i] = w[i] + g[i]; |
| 313 |
0 |
▶ | if(regularize_bias == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(regularize_bias == 0) |
0 |
0 |
0 |
|
| 314 |
0 |
| g[w_size-1] -= w[w_size-1]; |
| 315 |
0 |
| } |
| 316 |
|
| |
| 317 |
0 |
| void l2r_lr_fun::get_diag_preconditioner(double *M) |
| 318 |
|
| { |
| 319 |
|
| int i; |
| 320 |
0 |
| int l = prob->l; |
| 321 |
0 |
| int w_size=get_nr_variable(); |
| 322 |
0 |
| feature_node **x = prob->x; |
| 323 |
|
| |
| 324 |
0 |
▶ | for (i=0; i<w_size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<w_size; i++) |
0 |
0 |
0 |
|
| 325 |
0 |
| M[i] = 1; |
| 326 |
0 |
▶ | if(regularize_bias == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(regularize_bias == 0) |
0 |
0 |
0 |
|
| 327 |
0 |
| M[w_size-1] = 0; |
| 328 |
|
| |
| 329 |
0 |
▶ | for (i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<l; i++) |
0 |
0 |
0 |
|
| 330 |
|
| { |
| 331 |
0 |
| feature_node *xi = x[i]; |
| 332 |
0 |
▶ | while (xi->index!=-1) |
Branch: 1 branch
| Branch | True | False | Total |
| while (xi->index!=-1) |
0 |
0 |
0 |
|
| 333 |
|
| { |
| 334 |
0 |
| M[xi->index-1] += xi->value*xi->value*C[i]*D[i]; |
| 335 |
0 |
| xi++; |
| 336 |
|
| } |
| 337 |
|
| } |
| 338 |
0 |
| } |
| 339 |
|
| |
| 340 |
0 |
| void l2r_lr_fun::Hv(double *s, double *Hs) |
| 341 |
|
| { |
| 342 |
|
| int i; |
| 343 |
0 |
| int l=prob->l; |
| 344 |
0 |
| int w_size=get_nr_variable(); |
| 345 |
0 |
| feature_node **x=prob->x; |
| 346 |
|
| |
| 347 |
0 |
▶ | for(i=0;i<w_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<w_size;i++) |
0 |
0 |
0 |
|
| 348 |
0 |
| Hs[i] = 0; |
| 349 |
0 |
▶ | for(i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) |
0 |
0 |
0 |
|
| 350 |
|
| { |
| 351 |
0 |
| feature_node * const xi=x[i]; |
| 352 |
0 |
| double xTs = sparse_operator::dot(s, xi); |
| 353 |
|
| |
| 354 |
0 |
| xTs = C[i]*D[i]*xTs; |
| 355 |
|
| |
| 356 |
0 |
| sparse_operator::axpy(xTs, xi, Hs); |
| 357 |
|
| } |
| 358 |
0 |
▶ | for(i=0;i<w_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<w_size;i++) |
0 |
0 |
0 |
|
| 359 |
0 |
| Hs[i] = s[i] + Hs[i]; |
| 360 |
0 |
▶ | if(regularize_bias == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(regularize_bias == 0) |
0 |
0 |
0 |
|
| 361 |
0 |
| Hs[w_size-1] -= s[w_size-1]; |
| 362 |
0 |
| } |
| 363 |
|
| |
| 364 |
|
| class l2r_l2_svc_fun: public l2r_erm_fun |
| 365 |
|
| { |
| 366 |
|
| public: |
| 367 |
|
| l2r_l2_svc_fun(const problem *prob, const parameter *param, double *C); |
| 368 |
|
| ~l2r_l2_svc_fun(); |
| 369 |
|
| |
| 370 |
|
| void grad(double *w, double *g); |
| 371 |
|
| void Hv(double *s, double *Hs); |
| 372 |
|
| |
| 373 |
|
| void get_diag_preconditioner(double *M); |
| 374 |
|
| |
| 375 |
|
| protected: |
| 376 |
|
| void subXTv(double *v, double *XTv); |
| 377 |
|
| |
| 378 |
|
| int *I; |
| 379 |
|
| int sizeI; |
| 380 |
|
| |
| 381 |
|
| private: |
| 382 |
|
| double C_times_loss(int i, double wx_i); |
| 383 |
|
| }; |
| 384 |
|
| |
| 385 |
76 |
| l2r_l2_svc_fun::l2r_l2_svc_fun(const problem *prob, const parameter *param, double *C): |
| 386 |
76 |
| l2r_erm_fun(prob, param, C) |
| 387 |
|
| { |
| 388 |
76 |
▶ | I = new int[prob->l]; |
Branch: 2 branches
| Branch | True | False | Total |
| I = new int[prob->l]; |
76 |
0 |
76 |
| I = new int[prob->l]; |
76 |
0 |
76 |
|
| 389 |
76 |
| } |
| 390 |
|
| |
| 391 |
0 |
| l2r_l2_svc_fun::~l2r_l2_svc_fun() |
| 392 |
|
| { |
| 393 |
76 |
▶ | delete[] I; |
Branch: 1 branch
| Branch | True | False | Total |
| delete[] I; |
76 |
0 |
76 |
|
| 394 |
0 |
| } |
| 395 |
|
| |
| 396 |
46764 |
| double l2r_l2_svc_fun::C_times_loss(int i, double wx_i) |
| 397 |
|
| { |
| 398 |
46764 |
| double d = 1 - prob->y[i] * wx_i; |
| 399 |
46764 |
▶ | if (d > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (d > 0) |
39539 |
7225 |
46764 |
|
| 400 |
39539 |
| return C[i] * d * d; |
| 401 |
|
| else |
| 402 |
7225 |
| return 0; |
| 403 |
|
| } |
| 404 |
|
| |
| 405 |
215 |
| void l2r_l2_svc_fun::grad(double *w, double *g) |
| 406 |
|
| { |
| 407 |
|
| int i; |
| 408 |
215 |
| double *y=prob->y; |
| 409 |
215 |
| int l=prob->l; |
| 410 |
215 |
| int w_size=get_nr_variable(); |
| 411 |
|
| |
| 412 |
215 |
| sizeI = 0; |
| 413 |
46979 |
▶ | for (i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0;i<l;i++) |
46764 |
215 |
46979 |
|
| 414 |
|
| { |
| 415 |
46764 |
| tmp[i] = wx[i] * y[i]; |
| 416 |
46764 |
▶ | if (tmp[i] < 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp[i] < 1) |
39539 |
7225 |
46764 |
|
| 417 |
|
| { |
| 418 |
39539 |
| tmp[sizeI] = C[i]*y[i]*(tmp[i]-1); |
| 419 |
39539 |
| I[sizeI] = i; |
| 420 |
39539 |
| sizeI++; |
| 421 |
|
| } |
| 422 |
|
| } |
| 423 |
215 |
| subXTv(tmp, g); |
| 424 |
|
| |
| 425 |
3010 |
▶ | for(i=0;i<w_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<w_size;i++) |
2795 |
215 |
3010 |
|
| 426 |
2795 |
| g[i] = w[i] + 2*g[i]; |
| 427 |
215 |
▶ | if(regularize_bias == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(regularize_bias == 0) |
0 |
215 |
215 |
|
| 428 |
0 |
| g[w_size-1] -= w[w_size-1]; |
| 429 |
215 |
| } |
| 430 |
|
| |
| 431 |
63 |
| void l2r_l2_svc_fun::get_diag_preconditioner(double *M) |
| 432 |
|
| { |
| 433 |
|
| int i; |
| 434 |
63 |
| int w_size=get_nr_variable(); |
| 435 |
63 |
| feature_node **x = prob->x; |
| 436 |
|
| |
| 437 |
882 |
▶ | for (i=0; i<w_size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<w_size; i++) |
819 |
63 |
882 |
|
| 438 |
819 |
| M[i] = 1; |
| 439 |
63 |
▶ | if(regularize_bias == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(regularize_bias == 0) |
0 |
63 |
63 |
|
| 440 |
0 |
| M[w_size-1] = 0; |
| 441 |
|
| |
| 442 |
11515 |
▶ | for (i=0; i<sizeI; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<sizeI; i++) |
11452 |
63 |
11515 |
|
| 443 |
|
| { |
| 444 |
11452 |
| int idx = I[i]; |
| 445 |
11452 |
| feature_node *xi = x[idx]; |
| 446 |
154679 |
▶ | while (xi->index!=-1) |
Branch: 1 branch
| Branch | True | False | Total |
| while (xi->index!=-1) |
143227 |
11452 |
154679 |
|
| 447 |
|
| { |
| 448 |
143227 |
| M[xi->index-1] += xi->value*xi->value*C[idx]*2; |
| 449 |
143227 |
| xi++; |
| 450 |
|
| } |
| 451 |
|
| } |
| 452 |
63 |
| } |
| 453 |
|
| |
| 454 |
190 |
| void l2r_l2_svc_fun::Hv(double *s, double *Hs) |
| 455 |
|
| { |
| 456 |
|
| int i; |
| 457 |
190 |
| int w_size=get_nr_variable(); |
| 458 |
190 |
| feature_node **x=prob->x; |
| 459 |
|
| |
| 460 |
2660 |
▶ | for(i=0;i<w_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<w_size;i++) |
2470 |
190 |
2660 |
|
| 461 |
2470 |
| Hs[i]=0; |
| 462 |
33621 |
▶ | for(i=0;i<sizeI;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<sizeI;i++) |
33431 |
190 |
33621 |
|
| 463 |
|
| { |
| 464 |
33431 |
| feature_node * const xi=x[I[i]]; |
| 465 |
33431 |
| double xTs = sparse_operator::dot(s, xi); |
| 466 |
|
| |
| 467 |
33431 |
| xTs = C[I[i]]*xTs; |
| 468 |
|
| |
| 469 |
33431 |
| sparse_operator::axpy(xTs, xi, Hs); |
| 470 |
|
| } |
| 471 |
2660 |
▶ | for(i=0;i<w_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<w_size;i++) |
2470 |
190 |
2660 |
|
| 472 |
2470 |
| Hs[i] = s[i] + 2*Hs[i]; |
| 473 |
190 |
▶ | if(regularize_bias == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(regularize_bias == 0) |
0 |
190 |
190 |
|
| 474 |
0 |
| Hs[w_size-1] -= s[w_size-1]; |
| 475 |
190 |
| } |
| 476 |
|
| |
| 477 |
215 |
| void l2r_l2_svc_fun::subXTv(double *v, double *XTv) |
| 478 |
|
| { |
| 479 |
|
| int i; |
| 480 |
215 |
| int w_size=get_nr_variable(); |
| 481 |
215 |
| feature_node **x=prob->x; |
| 482 |
|
| |
| 483 |
3010 |
▶ | for(i=0;i<w_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<w_size;i++) |
2795 |
215 |
3010 |
|
| 484 |
2795 |
| XTv[i]=0; |
| 485 |
39754 |
▶ | for(i=0;i<sizeI;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<sizeI;i++) |
39539 |
215 |
39754 |
|
| 486 |
39539 |
| sparse_operator::axpy(v[i], x[I[i]], XTv); |
| 487 |
215 |
| } |
| 488 |
|
| |
| 489 |
|
| class l2r_l2_svr_fun: public l2r_l2_svc_fun |
| 490 |
|
| { |
| 491 |
|
| public: |
| 492 |
|
| l2r_l2_svr_fun(const problem *prob, const parameter *param, double *C); |
| 493 |
|
| |
| 494 |
|
| void grad(double *w, double *g); |
| 495 |
|
| |
| 496 |
|
| private: |
| 497 |
|
| double C_times_loss(int i, double wx_i); |
| 498 |
|
| double p; |
| 499 |
|
| }; |
| 500 |
|
| |
| 501 |
0 |
| l2r_l2_svr_fun::l2r_l2_svr_fun(const problem *prob, const parameter *param, double *C): |
| 502 |
0 |
| l2r_l2_svc_fun(prob, param, C) |
| 503 |
|
| { |
| 504 |
0 |
| this->p = param->p; |
| 505 |
0 |
| this->regularize_bias = param->regularize_bias; |
| 506 |
0 |
| } |
| 507 |
|
| |
| 508 |
0 |
| double l2r_l2_svr_fun::C_times_loss(int i, double wx_i) |
| 509 |
|
| { |
| 510 |
0 |
| double d = wx_i - prob->y[i]; |
| 511 |
0 |
▶ | if(d < -p) |
Branch: 1 branch
| Branch | True | False | Total |
| if(d < -p) |
0 |
0 |
0 |
|
| 512 |
0 |
| return C[i]*(d+p)*(d+p); |
| 513 |
0 |
▶ | else if(d > p) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(d > p) |
0 |
0 |
0 |
|
| 514 |
0 |
| return C[i]*(d-p)*(d-p); |
| 515 |
0 |
| return 0; |
| 516 |
|
| } |
| 517 |
|
| |
| 518 |
0 |
| void l2r_l2_svr_fun::grad(double *w, double *g) |
| 519 |
|
| { |
| 520 |
|
| int i; |
| 521 |
0 |
| double *y=prob->y; |
| 522 |
0 |
| int l=prob->l; |
| 523 |
0 |
| int w_size=get_nr_variable(); |
| 524 |
|
| double d; |
| 525 |
|
| |
| 526 |
0 |
| sizeI = 0; |
| 527 |
0 |
▶ | for(i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) |
0 |
0 |
0 |
|
| 528 |
|
| { |
| 529 |
0 |
| d = wx[i] - y[i]; |
| 530 |
|
| |
| 531 |
|
| // generate index set I |
| 532 |
0 |
▶ | if(d < -p) |
Branch: 1 branch
| Branch | True | False | Total |
| if(d < -p) |
0 |
0 |
0 |
|
| 533 |
|
| { |
| 534 |
0 |
| tmp[sizeI] = C[i]*(d+p); |
| 535 |
0 |
| I[sizeI] = i; |
| 536 |
0 |
| sizeI++; |
| 537 |
|
| } |
| 538 |
0 |
▶ | else if(d > p) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(d > p) |
0 |
0 |
0 |
|
| 539 |
|
| { |
| 540 |
0 |
| tmp[sizeI] = C[i]*(d-p); |
| 541 |
0 |
| I[sizeI] = i; |
| 542 |
0 |
| sizeI++; |
| 543 |
|
| } |
| 544 |
|
| |
| 545 |
|
| } |
| 546 |
0 |
| subXTv(tmp, g); |
| 547 |
|
| |
| 548 |
0 |
▶ | for(i=0;i<w_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<w_size;i++) |
0 |
0 |
0 |
|
| 549 |
0 |
| g[i] = w[i] + 2*g[i]; |
| 550 |
0 |
▶ | if(regularize_bias == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(regularize_bias == 0) |
0 |
0 |
0 |
|
| 551 |
0 |
| g[w_size-1] -= w[w_size-1]; |
| 552 |
0 |
| } |
| 553 |
|
| |
| 554 |
|
| // A coordinate descent algorithm for |
| 555 |
|
| // multi-class support vector machines by Crammer and Singer |
| 556 |
|
| // |
| 557 |
|
| // min_{\alpha} 0.5 \sum_m ||w_m(\alpha)||^2 + \sum_i \sum_m e^m_i alpha^m_i |
| 558 |
|
| // s.t. \alpha^m_i <= C^m_i \forall m,i , \sum_m \alpha^m_i=0 \forall i |
| 559 |
|
| // |
| 560 |
|
| // where e^m_i = 0 if y_i = m, |
| 561 |
|
| // e^m_i = 1 if y_i != m, |
| 562 |
|
| // C^m_i = C if m = y_i, |
| 563 |
|
| // C^m_i = 0 if m != y_i, |
| 564 |
|
| // and w_m(\alpha) = \sum_i \alpha^m_i x_i |
| 565 |
|
| // |
| 566 |
|
| // Given: |
| 567 |
|
| // x, y, C |
| 568 |
|
| // eps is the stopping tolerance |
| 569 |
|
| // |
| 570 |
|
| // solution will be put in w |
| 571 |
|
| // |
| 572 |
|
| // See Appendix of LIBLINEAR paper, Fan et al. (2008) |
| 573 |
|
| |
| 574 |
|
| #define GETI(i) ((int) prob->y[i]) |
| 575 |
|
| // To support weights for instances, use GETI(i) (i) |
| 576 |
|
| |
| 577 |
|
| class Solver_MCSVM_CS |
| 578 |
|
| { |
| 579 |
|
| public: |
| 580 |
|
| Solver_MCSVM_CS(const problem *prob, int nr_class, double *C, double eps=0.1, int max_iter=100000); |
| 581 |
|
| ~Solver_MCSVM_CS(); |
| 582 |
|
| void Solve(double *w); |
| 583 |
|
| private: |
| 584 |
|
| void solve_sub_problem(double A_i, int yi, double C_yi, int active_i, double *alpha_new); |
| 585 |
|
| bool be_shrunk(int i, int m, int yi, double alpha_i, double minG); |
| 586 |
|
| double *B, *C, *G; |
| 587 |
|
| int w_size, l; |
| 588 |
|
| int nr_class; |
| 589 |
|
| int max_iter; |
| 590 |
|
| double eps; |
| 591 |
|
| const problem *prob; |
| 592 |
|
| }; |
| 593 |
|
| |
| 594 |
0 |
| Solver_MCSVM_CS::Solver_MCSVM_CS(const problem *prob, int nr_class, double *weighted_C, double eps, int max_iter) |
| 595 |
|
| { |
| 596 |
0 |
| this->w_size = prob->n; |
| 597 |
0 |
| this->l = prob->l; |
| 598 |
0 |
| this->nr_class = nr_class; |
| 599 |
0 |
| this->eps = eps; |
| 600 |
0 |
| this->max_iter = max_iter; |
| 601 |
0 |
| this->prob = prob; |
| 602 |
0 |
▶ | this->B = new double[nr_class]; |
Branch: 1 branch
| Branch | True | False | Total |
| this->B = new double[nr_class]; |
0 |
0 |
0 |
|
| 603 |
0 |
▶ | this->G = new double[nr_class]; |
Branch: 1 branch
| Branch | True | False | Total |
| this->G = new double[nr_class]; |
0 |
0 |
0 |
|
| 604 |
0 |
| this->C = weighted_C; |
| 605 |
0 |
| } |
| 606 |
|
| |
| 607 |
0 |
| Solver_MCSVM_CS::~Solver_MCSVM_CS() |
| 608 |
|
| { |
| 609 |
0 |
▶ | delete[] B; |
Branch: 1 branch
| Branch | True | False | Total |
| delete[] B; |
0 |
0 |
0 |
|
| 610 |
0 |
▶ | delete[] G; |
Branch: 1 branch
| Branch | True | False | Total |
| delete[] G; |
0 |
0 |
0 |
|
| 611 |
0 |
| } |
| 612 |
|
| |
| 613 |
0 |
| int compare_double(const void *a, const void *b) |
| 614 |
|
| { |
| 615 |
0 |
▶ | if(*(double *)a > *(double *)b) |
Branch: 1 branch
| Branch | True | False | Total |
| if(*(double *)a > *(double *)b) |
0 |
0 |
0 |
|
| 616 |
0 |
| return -1; |
| 617 |
0 |
▶ | if(*(double *)a < *(double *)b) |
Branch: 1 branch
| Branch | True | False | Total |
| if(*(double *)a < *(double *)b) |
0 |
0 |
0 |
|
| 618 |
0 |
| return 1; |
| 619 |
0 |
| return 0; |
| 620 |
|
| } |
| 621 |
|
| |
| 622 |
0 |
| void Solver_MCSVM_CS::solve_sub_problem(double A_i, int yi, double C_yi, int active_i, double *alpha_new) |
| 623 |
|
| { |
| 624 |
|
| int r; |
| 625 |
|
| double *D; |
| 626 |
|
| |
| 627 |
0 |
▶ | clone(D, B, active_i); |
Branch: 1 branch
| Branch | True | False | Total |
| clone(D, B, active_i); |
0 |
0 |
0 |
|
| 628 |
0 |
▶ | if(yi < active_i) |
Branch: 1 branch
| Branch | True | False | Total |
| if(yi < active_i) |
0 |
0 |
0 |
|
| 629 |
0 |
| D[yi] += A_i*C_yi; |
| 630 |
0 |
▶ | qsort(D, active_i, sizeof(double), compare_double); |
Branch: 1 branch
| Branch | True | False | Total |
| qsort(D, active_i, sizeof(double), compare_double); |
0 |
0 |
0 |
|
| 631 |
|
| |
| 632 |
0 |
| double beta = D[0] - A_i*C_yi; |
| 633 |
0 |
▶ | for(r=1;r<active_i && beta<r*D[r];r++) |
Branch: 2 branches
| Branch | True | False | Total |
| for(r=1;r<active_i && beta<r*D[r];r++) |
0 |
0 |
0 |
| for(r=1;r<active_i && beta<r*D[r];r++) |
0 |
0 |
0 |
|
| 634 |
0 |
| beta += D[r]; |
| 635 |
0 |
| beta /= r; |
| 636 |
|
| |
| 637 |
0 |
▶ | for(r=0;r<active_i;r++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(r=0;r<active_i;r++) |
0 |
0 |
0 |
|
| 638 |
|
| { |
| 639 |
0 |
▶ | if(r == yi) |
Branch: 1 branch
| Branch | True | False | Total |
| if(r == yi) |
0 |
0 |
0 |
|
| 640 |
0 |
| alpha_new[r] = min(C_yi, (beta-B[r])/A_i); |
| 641 |
|
| else |
| 642 |
0 |
| alpha_new[r] = min((double)0, (beta - B[r])/A_i); |
| 643 |
|
| } |
| 644 |
0 |
▶ | delete[] D; |
Branch: 1 branch
| Branch | True | False | Total |
| delete[] D; |
0 |
0 |
0 |
|
| 645 |
0 |
| } |
| 646 |
|
| |
| 647 |
0 |
| bool Solver_MCSVM_CS::be_shrunk(int i, int m, int yi, double alpha_i, double minG) |
| 648 |
|
| { |
| 649 |
0 |
| double bound = 0; |
| 650 |
0 |
▶ | if(m == yi) |
Branch: 1 branch
| Branch | True | False | Total |
| if(m == yi) |
0 |
0 |
0 |
|
| 651 |
0 |
| bound = C[GETI(i)]; |
| 652 |
0 |
▶ | if(alpha_i == bound && G[m] < minG) |
Branch: 2 branches
| Branch | True | False | Total |
| if(alpha_i == bound && G[m] < minG) |
0 |
0 |
0 |
| if(alpha_i == bound && G[m] < minG) |
0 |
0 |
0 |
|
| 653 |
0 |
| return true; |
| 654 |
0 |
| return false; |
| 655 |
|
| } |
| 656 |
|
| |
| 657 |
0 |
| void Solver_MCSVM_CS::Solve(double *w) |
| 658 |
|
| { |
| 659 |
|
| int i, m, s; |
| 660 |
0 |
| int iter = 0; |
| 661 |
0 |
▶ | double *alpha = new double[l*nr_class]; |
Branch: 1 branch
| Branch | True | False | Total |
| double *alpha = new double[l*nr_class]; |
0 |
0 |
0 |
|
| 662 |
0 |
▶ | double *alpha_new = new double[nr_class]; |
Branch: 1 branch
| Branch | True | False | Total |
| double *alpha_new = new double[nr_class]; |
0 |
0 |
0 |
|
| 663 |
0 |
▶ | int *index = new int[l]; |
Branch: 1 branch
| Branch | True | False | Total |
| int *index = new int[l]; |
0 |
0 |
0 |
|
| 664 |
0 |
▶ | double *QD = new double[l]; |
Branch: 1 branch
| Branch | True | False | Total |
| double *QD = new double[l]; |
0 |
0 |
0 |
|
| 665 |
0 |
▶ | int *d_ind = new int[nr_class]; |
Branch: 1 branch
| Branch | True | False | Total |
| int *d_ind = new int[nr_class]; |
0 |
0 |
0 |
|
| 666 |
0 |
▶ | double *d_val = new double[nr_class]; |
Branch: 1 branch
| Branch | True | False | Total |
| double *d_val = new double[nr_class]; |
0 |
0 |
0 |
|
| 667 |
0 |
▶ | int *alpha_index = new int[nr_class*l]; |
Branch: 1 branch
| Branch | True | False | Total |
| int *alpha_index = new int[nr_class*l]; |
0 |
0 |
0 |
|
| 668 |
0 |
▶ | int *y_index = new int[l]; |
Branch: 1 branch
| Branch | True | False | Total |
| int *y_index = new int[l]; |
0 |
0 |
0 |
|
| 669 |
0 |
| int active_size = l; |
| 670 |
0 |
▶ | int *active_size_i = new int[l]; |
Branch: 1 branch
| Branch | True | False | Total |
| int *active_size_i = new int[l]; |
0 |
0 |
0 |
|
| 671 |
0 |
| double eps_shrink = max(10.0*eps, 1.0); // stopping tolerance for shrinking |
| 672 |
0 |
| bool start_from_all = true; |
| 673 |
|
| |
| 674 |
|
| // Initial alpha can be set here. Note that |
| 675 |
|
| // sum_m alpha[i*nr_class+m] = 0, for all i=1,...,l-1 |
| 676 |
|
| // alpha[i*nr_class+m] <= C[GETI(i)] if prob->y[i] == m |
| 677 |
|
| // alpha[i*nr_class+m] <= 0 if prob->y[i] != m |
| 678 |
|
| // If initial alpha isn't zero, uncomment the for loop below to initialize w |
| 679 |
0 |
▶ | for(i=0;i<l*nr_class;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l*nr_class;i++) |
0 |
0 |
0 |
|
| 680 |
0 |
| alpha[i] = 0; |
| 681 |
|
| |
| 682 |
0 |
▶ | for(i=0;i<w_size*nr_class;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<w_size*nr_class;i++) |
0 |
0 |
0 |
|
| 683 |
0 |
| w[i] = 0; |
| 684 |
0 |
▶ | for(i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) |
0 |
0 |
0 |
|
| 685 |
|
| { |
| 686 |
0 |
▶ | for(m=0;m<nr_class;m++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(m=0;m<nr_class;m++) |
0 |
0 |
0 |
|
| 687 |
0 |
| alpha_index[i*nr_class+m] = m; |
| 688 |
0 |
| feature_node *xi = prob->x[i]; |
| 689 |
0 |
| QD[i] = 0; |
| 690 |
0 |
▶ | while(xi->index != -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(xi->index != -1) |
0 |
0 |
0 |
|
| 691 |
|
| { |
| 692 |
0 |
| double val = xi->value; |
| 693 |
0 |
| QD[i] += val*val; |
| 694 |
|
| |
| 695 |
|
| // Uncomment the for loop if initial alpha isn't zero |
| 696 |
|
| // for(m=0; m<nr_class; m++) |
| 697 |
|
| // w[(xi->index-1)*nr_class+m] += alpha[i*nr_class+m]*val; |
| 698 |
0 |
| xi++; |
| 699 |
|
| } |
| 700 |
0 |
| active_size_i[i] = nr_class; |
| 701 |
0 |
| y_index[i] = (int)prob->y[i]; |
| 702 |
0 |
| index[i] = i; |
| 703 |
|
| } |
| 704 |
|
| |
| 705 |
0 |
▶ | while(iter < max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| while(iter < max_iter) |
0 |
0 |
0 |
|
| 706 |
|
| { |
| 707 |
0 |
| double stopping = -INF; |
| 708 |
0 |
▶ | for(i=0;i<active_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<active_size;i++) |
0 |
0 |
0 |
|
| 709 |
|
| { |
| 710 |
0 |
| int j = i+rand()%(active_size-i); |
| 711 |
0 |
| swap(index[i], index[j]); |
| 712 |
|
| } |
| 713 |
0 |
▶ | for(s=0;s<active_size;s++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(s=0;s<active_size;s++) |
0 |
0 |
0 |
|
| 714 |
|
| { |
| 715 |
0 |
| i = index[s]; |
| 716 |
0 |
| double Ai = QD[i]; |
| 717 |
0 |
| double *alpha_i = &alpha[i*nr_class]; |
| 718 |
0 |
| int *alpha_index_i = &alpha_index[i*nr_class]; |
| 719 |
|
| |
| 720 |
0 |
▶ | if(Ai > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(Ai > 0) |
0 |
0 |
0 |
|
| 721 |
|
| { |
| 722 |
0 |
▶ | for(m=0;m<active_size_i[i];m++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(m=0;m<active_size_i[i];m++) |
0 |
0 |
0 |
|
| 723 |
0 |
| G[m] = 1; |
| 724 |
0 |
▶ | if(y_index[i] < active_size_i[i]) |
Branch: 1 branch
| Branch | True | False | Total |
| if(y_index[i] < active_size_i[i]) |
0 |
0 |
0 |
|
| 725 |
0 |
| G[y_index[i]] = 0; |
| 726 |
|
| |
| 727 |
0 |
| feature_node *xi = prob->x[i]; |
| 728 |
0 |
▶ | while(xi->index!= -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(xi->index!= -1) |
0 |
0 |
0 |
|
| 729 |
|
| { |
| 730 |
0 |
| double *w_i = &w[(xi->index-1)*nr_class]; |
| 731 |
0 |
▶ | for(m=0;m<active_size_i[i];m++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(m=0;m<active_size_i[i];m++) |
0 |
0 |
0 |
|
| 732 |
0 |
| G[m] += w_i[alpha_index_i[m]]*(xi->value); |
| 733 |
0 |
| xi++; |
| 734 |
|
| } |
| 735 |
|
| |
| 736 |
0 |
| double minG = INF; |
| 737 |
0 |
| double maxG = -INF; |
| 738 |
0 |
▶ | for(m=0;m<active_size_i[i];m++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(m=0;m<active_size_i[i];m++) |
0 |
0 |
0 |
|
| 739 |
|
| { |
| 740 |
0 |
▶ | if(alpha_i[alpha_index_i[m]] < 0 && G[m] < minG) |
Branch: 2 branches
| Branch | True | False | Total |
| if(alpha_i[alpha_index_i[m]] < 0 && G[m] < minG) |
0 |
0 |
0 |
| if(alpha_i[alpha_index_i[m]] < 0 && G[m] < minG) |
0 |
0 |
0 |
|
| 741 |
0 |
| minG = G[m]; |
| 742 |
0 |
▶ | if(G[m] > maxG) |
Branch: 1 branch
| Branch | True | False | Total |
| if(G[m] > maxG) |
0 |
0 |
0 |
|
| 743 |
0 |
| maxG = G[m]; |
| 744 |
|
| } |
| 745 |
0 |
▶ | if(y_index[i] < active_size_i[i]) |
Branch: 1 branch
| Branch | True | False | Total |
| if(y_index[i] < active_size_i[i]) |
0 |
0 |
0 |
|
| 746 |
0 |
▶ | if(alpha_i[(int) prob->y[i]] < C[GETI(i)] && G[y_index[i]] < minG) |
Branch: 2 branches
| Branch | True | False | Total |
| if(alpha_i[(int) prob->y[i]] < C[GETI(i)] && G[y_index[i]] < minG) |
0 |
0 |
0 |
| if(alpha_i[(int) prob->y[i]] < C[GETI(i)] && G[y_index[i]] < minG) |
0 |
0 |
0 |
|
| 747 |
0 |
| minG = G[y_index[i]]; |
| 748 |
|
| |
| 749 |
0 |
▶ | for(m=0;m<active_size_i[i];m++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(m=0;m<active_size_i[i];m++) |
0 |
0 |
0 |
|
| 750 |
|
| { |
| 751 |
0 |
▶ | if(be_shrunk(i, m, y_index[i], alpha_i[alpha_index_i[m]], minG)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(be_shrunk(i, m, y_index[i], alpha_i[alpha_index_i[m]], minG)) |
0 |
0 |
0 |
|
| 752 |
|
| { |
| 753 |
0 |
| active_size_i[i]--; |
| 754 |
0 |
▶ | while(active_size_i[i]>m) |
Branch: 1 branch
| Branch | True | False | Total |
| while(active_size_i[i]>m) |
0 |
0 |
0 |
|
| 755 |
|
| { |
| 756 |
0 |
▶ | if(!be_shrunk(i, active_size_i[i], y_index[i], |
Branch: 1 branch
| Branch | True | False | Total |
| if(!be_shrunk(i, active_size_i[i], y_index[i], |
0 |
0 |
0 |
|
| 757 |
0 |
| alpha_i[alpha_index_i[active_size_i[i]]], minG)) |
| 758 |
|
| { |
| 759 |
0 |
| swap(alpha_index_i[m], alpha_index_i[active_size_i[i]]); |
| 760 |
0 |
| swap(G[m], G[active_size_i[i]]); |
| 761 |
0 |
▶ | if(y_index[i] == active_size_i[i]) |
Branch: 1 branch
| Branch | True | False | Total |
| if(y_index[i] == active_size_i[i]) |
0 |
0 |
0 |
|
| 762 |
0 |
| y_index[i] = m; |
| 763 |
0 |
▶ | else if(y_index[i] == m) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(y_index[i] == m) |
0 |
0 |
0 |
|
| 764 |
0 |
| y_index[i] = active_size_i[i]; |
| 765 |
0 |
| break; |
| 766 |
|
| } |
| 767 |
0 |
| active_size_i[i]--; |
| 768 |
|
| } |
| 769 |
|
| } |
| 770 |
|
| } |
| 771 |
|
| |
| 772 |
0 |
▶ | if(active_size_i[i] <= 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if(active_size_i[i] <= 1) |
0 |
0 |
0 |
|
| 773 |
|
| { |
| 774 |
0 |
| active_size--; |
| 775 |
0 |
| swap(index[s], index[active_size]); |
| 776 |
0 |
| s--; |
| 777 |
0 |
| continue; |
| 778 |
|
| } |
| 779 |
|
| |
| 780 |
0 |
▶ | if(maxG-minG <= 1e-12) |
Branch: 1 branch
| Branch | True | False | Total |
| if(maxG-minG <= 1e-12) |
0 |
0 |
0 |
|
| 781 |
0 |
| continue; |
| 782 |
|
| else |
| 783 |
0 |
| stopping = max(maxG - minG, stopping); |
| 784 |
|
| |
| 785 |
0 |
▶ | for(m=0;m<active_size_i[i];m++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(m=0;m<active_size_i[i];m++) |
0 |
0 |
0 |
|
| 786 |
0 |
| B[m] = G[m] - Ai*alpha_i[alpha_index_i[m]] ; |
| 787 |
|
| |
| 788 |
0 |
| solve_sub_problem(Ai, y_index[i], C[GETI(i)], active_size_i[i], alpha_new); |
| 789 |
0 |
| int nz_d = 0; |
| 790 |
0 |
▶ | for(m=0;m<active_size_i[i];m++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(m=0;m<active_size_i[i];m++) |
0 |
0 |
0 |
|
| 791 |
|
| { |
| 792 |
0 |
| double d = alpha_new[m] - alpha_i[alpha_index_i[m]]; |
| 793 |
0 |
| alpha_i[alpha_index_i[m]] = alpha_new[m]; |
| 794 |
0 |
▶ | if(fabs(d) >= 1e-12) |
Branch: 1 branch
| Branch | True | False | Total |
| if(fabs(d) >= 1e-12) |
0 |
0 |
0 |
|
| 795 |
|
| { |
| 796 |
0 |
| d_ind[nz_d] = alpha_index_i[m]; |
| 797 |
0 |
| d_val[nz_d] = d; |
| 798 |
0 |
| nz_d++; |
| 799 |
|
| } |
| 800 |
|
| } |
| 801 |
|
| |
| 802 |
0 |
| xi = prob->x[i]; |
| 803 |
0 |
▶ | while(xi->index != -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(xi->index != -1) |
0 |
0 |
0 |
|
| 804 |
|
| { |
| 805 |
0 |
| double *w_i = &w[(xi->index-1)*nr_class]; |
| 806 |
0 |
▶ | for(m=0;m<nz_d;m++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(m=0;m<nz_d;m++) |
0 |
0 |
0 |
|
| 807 |
0 |
| w_i[d_ind[m]] += d_val[m]*xi->value; |
| 808 |
0 |
| xi++; |
| 809 |
|
| } |
| 810 |
|
| } |
| 811 |
|
| } |
| 812 |
|
| |
| 813 |
0 |
| iter++; |
| 814 |
0 |
▶ | if(iter % 10 == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(iter % 10 == 0) |
0 |
0 |
0 |
|
| 815 |
|
| { |
| 816 |
0 |
| info("."); |
| 817 |
|
| } |
| 818 |
|
| |
| 819 |
0 |
▶ | if(stopping < eps_shrink) |
Branch: 1 branch
| Branch | True | False | Total |
| if(stopping < eps_shrink) |
0 |
0 |
0 |
|
| 820 |
|
| { |
| 821 |
0 |
▶ | if(stopping < eps && start_from_all == true) |
Branch: 2 branches
| Branch | True | False | Total |
| if(stopping < eps && start_from_all == true) |
0 |
0 |
0 |
| if(stopping < eps && start_from_all == true) |
0 |
0 |
0 |
|
| 822 |
|
| break; |
| 823 |
|
| else |
| 824 |
|
| { |
| 825 |
0 |
| active_size = l; |
| 826 |
0 |
▶ | for(i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) |
0 |
0 |
0 |
|
| 827 |
0 |
| active_size_i[i] = nr_class; |
| 828 |
0 |
| info("*"); |
| 829 |
0 |
| eps_shrink = max(eps_shrink/2, eps); |
| 830 |
0 |
| start_from_all = true; |
| 831 |
|
| } |
| 832 |
|
| } |
| 833 |
|
| else |
| 834 |
0 |
| start_from_all = false; |
| 835 |
|
| } |
| 836 |
|
| |
| 837 |
0 |
| info("\noptimization finished, #iter = %d\n",iter); |
| 838 |
0 |
▶ | if (iter >= max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| if (iter >= max_iter) |
0 |
0 |
0 |
|
| 839 |
0 |
| info("\nWARNING: reaching max number of iterations\n"); |
| 840 |
|
| |
| 841 |
|
| // calculate objective value |
| 842 |
0 |
| double v = 0; |
| 843 |
0 |
| int nSV = 0; |
| 844 |
0 |
▶ | for(i=0;i<w_size*nr_class;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<w_size*nr_class;i++) |
0 |
0 |
0 |
|
| 845 |
0 |
| v += w[i]*w[i]; |
| 846 |
0 |
| v = 0.5*v; |
| 847 |
0 |
▶ | for(i=0;i<l*nr_class;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l*nr_class;i++) |
0 |
0 |
0 |
|
| 848 |
|
| { |
| 849 |
0 |
| v += alpha[i]; |
| 850 |
0 |
▶ | if(fabs(alpha[i]) > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(fabs(alpha[i]) > 0) |
0 |
0 |
0 |
|
| 851 |
0 |
| nSV++; |
| 852 |
|
| } |
| 853 |
0 |
▶ | for(i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) |
0 |
0 |
0 |
|
| 854 |
0 |
| v -= alpha[i*nr_class+(int)prob->y[i]]; |
| 855 |
0 |
| info("Objective value = %lf\n",v); |
| 856 |
0 |
| info("nSV = %d\n",nSV); |
| 857 |
|
| |
| 858 |
0 |
▶ | delete [] alpha; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] alpha; |
0 |
0 |
0 |
|
| 859 |
0 |
▶ | delete [] alpha_new; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] alpha_new; |
0 |
0 |
0 |
|
| 860 |
0 |
▶ | delete [] index; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] index; |
0 |
0 |
0 |
|
| 861 |
0 |
▶ | delete [] QD; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] QD; |
0 |
0 |
0 |
|
| 862 |
0 |
▶ | delete [] d_ind; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] d_ind; |
0 |
0 |
0 |
|
| 863 |
0 |
▶ | delete [] d_val; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] d_val; |
0 |
0 |
0 |
|
| 864 |
0 |
▶ | delete [] alpha_index; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] alpha_index; |
0 |
0 |
0 |
|
| 865 |
0 |
▶ | delete [] y_index; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] y_index; |
0 |
0 |
0 |
|
| 866 |
0 |
▶ | delete [] active_size_i; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] active_size_i; |
0 |
0 |
0 |
|
| 867 |
0 |
| } |
| 868 |
|
| |
| 869 |
|
| // A coordinate descent algorithm for |
| 870 |
|
| // L1-loss and L2-loss SVM dual problems |
| 871 |
|
| // |
| 872 |
|
| // min_\alpha 0.5(\alpha^T (Q + D)\alpha) - e^T \alpha, |
| 873 |
|
| // s.t. 0 <= \alpha_i <= upper_bound_i, |
| 874 |
|
| // |
| 875 |
|
| // where Qij = yi yj xi^T xj and |
| 876 |
|
| // D is a diagonal matrix |
| 877 |
|
| // |
| 878 |
|
| // In L1-SVM case: |
| 879 |
|
| // upper_bound_i = Cp if y_i = 1 |
| 880 |
|
| // upper_bound_i = Cn if y_i = -1 |
| 881 |
|
| // D_ii = 0 |
| 882 |
|
| // In L2-SVM case: |
| 883 |
|
| // upper_bound_i = INF |
| 884 |
|
| // D_ii = 1/(2*Cp) if y_i = 1 |
| 885 |
|
| // D_ii = 1/(2*Cn) if y_i = -1 |
| 886 |
|
| // |
| 887 |
|
| // Given: |
| 888 |
|
| // x, y, Cp, Cn |
| 889 |
|
| // eps is the stopping tolerance |
| 890 |
|
| // |
| 891 |
|
| // solution will be put in w |
| 892 |
|
| // |
| 893 |
|
| // this function returns the number of iterations |
| 894 |
|
| // |
| 895 |
|
| // See Algorithm 3 of Hsieh et al., ICML 2008 |
| 896 |
|
| |
| 897 |
|
| #undef GETI |
| 898 |
|
| #define GETI(i) (y[i]+1) |
| 899 |
|
| // To support weights for instances, use GETI(i) (i) |
| 900 |
|
| |
| 901 |
2 |
| static int solve_l2r_l1l2_svc(const problem *prob, const parameter *param, double *w, double Cp, double Cn, int max_iter=300) |
| 902 |
|
| { |
| 903 |
2 |
| int l = prob->l; |
| 904 |
2 |
| int w_size = prob->n; |
| 905 |
2 |
| double eps = param->eps; |
| 906 |
2 |
| int solver_type = param->solver_type; |
| 907 |
2 |
| int i, s, iter = 0; |
| 908 |
|
| double C, d, G; |
| 909 |
2 |
▶ | double *QD = new double[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *QD = new double[l]; |
2 |
0 |
2 |
| double *QD = new double[l]; |
2 |
0 |
2 |
|
| 910 |
2 |
▶ | int *index = new int[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| int *index = new int[l]; |
2 |
0 |
2 |
| int *index = new int[l]; |
2 |
0 |
2 |
|
| 911 |
2 |
▶ | double *alpha = new double[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *alpha = new double[l]; |
2 |
0 |
2 |
| double *alpha = new double[l]; |
2 |
0 |
2 |
|
| 912 |
2 |
▶ | schar *y = new schar[l]; |
Branch: 1 branch
| Branch | True | False | Total |
| schar *y = new schar[l]; |
2 |
0 |
2 |
|
| 913 |
2 |
| int active_size = l; |
| 914 |
|
| |
| 915 |
|
| // PG: projected gradient, for shrinking and stopping |
| 916 |
|
| double PG; |
| 917 |
2 |
| double PGmax_old = INF; |
| 918 |
2 |
| double PGmin_old = -INF; |
| 919 |
|
| double PGmax_new, PGmin_new; |
| 920 |
|
| |
| 921 |
|
| // default solver_type: L2R_L2LOSS_SVC_DUAL |
| 922 |
2 |
| double diag[3] = {0.5/Cn, 0, 0.5/Cp}; |
| 923 |
2 |
| double upper_bound[3] = {INF, 0, INF}; |
| 924 |
2 |
▶ | if(solver_type == L2R_L1LOSS_SVC_DUAL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(solver_type == L2R_L1LOSS_SVC_DUAL) |
0 |
2 |
2 |
|
| 925 |
|
| { |
| 926 |
0 |
| diag[0] = 0; |
| 927 |
0 |
| diag[2] = 0; |
| 928 |
0 |
| upper_bound[0] = Cn; |
| 929 |
0 |
| upper_bound[2] = Cp; |
| 930 |
|
| } |
| 931 |
|
| |
| 932 |
542 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
540 |
2 |
542 |
|
| 933 |
|
| { |
| 934 |
540 |
▶ | if(prob->y[i] > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(prob->y[i] > 0) |
240 |
300 |
540 |
|
| 935 |
|
| { |
| 936 |
240 |
| y[i] = +1; |
| 937 |
|
| } |
| 938 |
|
| else |
| 939 |
|
| { |
| 940 |
300 |
| y[i] = -1; |
| 941 |
|
| } |
| 942 |
|
| } |
| 943 |
|
| |
| 944 |
|
| // Initial alpha can be set here. Note that |
| 945 |
|
| // 0 <= alpha[i] <= upper_bound[GETI(i)] |
| 946 |
542 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
540 |
2 |
542 |
|
| 947 |
540 |
| alpha[i] = 0; |
| 948 |
|
| |
| 949 |
28 |
▶ | for(i=0; i<w_size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<w_size; i++) |
26 |
2 |
28 |
|
| 950 |
26 |
| w[i] = 0; |
| 951 |
542 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
540 |
2 |
542 |
|
| 952 |
|
| { |
| 953 |
540 |
| QD[i] = diag[GETI(i)]; |
| 954 |
|
| |
| 955 |
540 |
| feature_node * const xi = prob->x[i]; |
| 956 |
540 |
| QD[i] += sparse_operator::nrm2_sq(xi); |
| 957 |
540 |
| sparse_operator::axpy(y[i]*alpha[i], xi, w); |
| 958 |
|
| |
| 959 |
540 |
| index[i] = i; |
| 960 |
|
| } |
| 961 |
|
| |
| 962 |
141 |
▶ | while (iter < max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| while (iter < max_iter) |
141 |
0 |
141 |
|
| 963 |
|
| { |
| 964 |
141 |
| PGmax_new = -INF; |
| 965 |
141 |
| PGmin_new = INF; |
| 966 |
|
| |
| 967 |
32056 |
▶ | for (i=0; i<active_size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<active_size; i++) |
31915 |
141 |
32056 |
|
| 968 |
|
| { |
| 969 |
31915 |
| int j = i+rand()%(active_size-i); |
| 970 |
31915 |
| swap(index[i], index[j]); |
| 971 |
|
| } |
| 972 |
|
| |
| 973 |
32056 |
▶ | for (s=0; s<active_size; s++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (s=0; s<active_size; s++) |
31915 |
141 |
32056 |
|
| 974 |
|
| { |
| 975 |
31915 |
| i = index[s]; |
| 976 |
31915 |
| const schar yi = y[i]; |
| 977 |
31915 |
| feature_node * const xi = prob->x[i]; |
| 978 |
|
| |
| 979 |
31915 |
| G = yi*sparse_operator::dot(w, xi)-1; |
| 980 |
|
| |
| 981 |
31915 |
| C = upper_bound[GETI(i)]; |
| 982 |
31915 |
| G += alpha[i]*diag[GETI(i)]; |
| 983 |
|
| |
| 984 |
31915 |
| PG = 0; |
| 985 |
31915 |
▶ | if (alpha[i] == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (alpha[i] == 0) |
3995 |
27920 |
31915 |
|
| 986 |
|
| { |
| 987 |
3995 |
▶ | if (G > PGmax_old) |
Branch: 1 branch
| Branch | True | False | Total |
| if (G > PGmax_old) |
150 |
3845 |
3995 |
|
| 988 |
|
| { |
| 989 |
150 |
| active_size--; |
| 990 |
150 |
| swap(index[s], index[active_size]); |
| 991 |
150 |
| s--; |
| 992 |
150 |
| continue; |
| 993 |
|
| } |
| 994 |
3845 |
▶ | else if (G < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (G < 0) |
1063 |
2782 |
3845 |
|
| 995 |
1063 |
| PG = G; |
| 996 |
|
| } |
| 997 |
27920 |
▶ | else if (alpha[i] == C) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (alpha[i] == C) |
0 |
27920 |
27920 |
|
| 998 |
|
| { |
| 999 |
0 |
▶ | if (G < PGmin_old) |
Branch: 1 branch
| Branch | True | False | Total |
| if (G < PGmin_old) |
0 |
0 |
0 |
|
| 1000 |
|
| { |
| 1001 |
0 |
| active_size--; |
| 1002 |
0 |
| swap(index[s], index[active_size]); |
| 1003 |
0 |
| s--; |
| 1004 |
0 |
| continue; |
| 1005 |
|
| } |
| 1006 |
0 |
▶ | else if (G > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (G > 0) |
0 |
0 |
0 |
|
| 1007 |
0 |
| PG = G; |
| 1008 |
|
| } |
| 1009 |
|
| else |
| 1010 |
27920 |
| PG = G; |
| 1011 |
|
| |
| 1012 |
31765 |
| PGmax_new = max(PGmax_new, PG); |
| 1013 |
31765 |
| PGmin_new = min(PGmin_new, PG); |
| 1014 |
|
| |
| 1015 |
31765 |
▶ | if(fabs(PG) > 1.0e-12) |
Branch: 1 branch
| Branch | True | False | Total |
| if(fabs(PG) > 1.0e-12) |
28983 |
2782 |
31765 |
|
| 1016 |
|
| { |
| 1017 |
28983 |
| double alpha_old = alpha[i]; |
| 1018 |
28983 |
| alpha[i] = min(max(alpha[i] - G/QD[i], 0.0), C); |
| 1019 |
28983 |
| d = (alpha[i] - alpha_old)*yi; |
| 1020 |
28983 |
| sparse_operator::axpy(d, xi, w); |
| 1021 |
|
| } |
| 1022 |
|
| } |
| 1023 |
|
| |
| 1024 |
141 |
| iter++; |
| 1025 |
141 |
▶ | if(iter % 10 == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(iter % 10 == 0) |
14 |
127 |
141 |
|
| 1026 |
14 |
▶ | info("."); |
Branch: 1 branch
| Branch | True | False | Total |
| info("."); |
14 |
0 |
14 |
|
| 1027 |
|
| |
| 1028 |
141 |
▶ | if(PGmax_new - PGmin_new <= eps && |
Branch: 1 branch
| Branch | True | False | Total |
| if(PGmax_new - PGmin_new <= eps && |
4 |
137 |
141 |
|
| 1029 |
4 |
▶ | fabs(PGmax_new) <= eps && fabs(PGmin_new) <= eps) |
Branch: 2 branches
| Branch | True | False | Total |
| fabs(PGmax_new) <= eps && fabs(PGmin_new) <= eps) |
4 |
0 |
4 |
| fabs(PGmax_new) <= eps && fabs(PGmin_new) <= eps) |
4 |
0 |
4 |
|
| 1030 |
|
| { |
| 1031 |
4 |
▶ | if(active_size == l) |
Branch: 1 branch
| Branch | True | False | Total |
| if(active_size == l) |
2 |
2 |
4 |
|
| 1032 |
2 |
| break; |
| 1033 |
|
| else |
| 1034 |
|
| { |
| 1035 |
2 |
| active_size = l; |
| 1036 |
2 |
▶ | info("*"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("*"); |
2 |
0 |
2 |
|
| 1037 |
2 |
| PGmax_old = INF; |
| 1038 |
2 |
| PGmin_old = -INF; |
| 1039 |
2 |
| continue; |
| 1040 |
|
| } |
| 1041 |
|
| } |
| 1042 |
137 |
| PGmax_old = PGmax_new; |
| 1043 |
137 |
| PGmin_old = PGmin_new; |
| 1044 |
137 |
▶ | if (PGmax_old <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (PGmax_old <= 0) |
2 |
135 |
137 |
|
| 1045 |
2 |
| PGmax_old = INF; |
| 1046 |
137 |
▶ | if (PGmin_old >= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (PGmin_old >= 0) |
0 |
137 |
137 |
|
| 1047 |
0 |
| PGmin_old = -INF; |
| 1048 |
|
| } |
| 1049 |
|
| |
| 1050 |
2 |
▶ | info("\noptimization finished, #iter = %d\n",iter); |
Branch: 1 branch
| Branch | True | False | Total |
| info("\noptimization finished, #iter = %d\n",iter); |
2 |
0 |
2 |
|
| 1051 |
|
| |
| 1052 |
|
| // calculate objective value |
| 1053 |
|
| |
| 1054 |
2 |
| double v = 0; |
| 1055 |
2 |
| int nSV = 0; |
| 1056 |
28 |
▶ | for(i=0; i<w_size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<w_size; i++) |
26 |
2 |
28 |
|
| 1057 |
26 |
| v += w[i]*w[i]; |
| 1058 |
542 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
540 |
2 |
542 |
|
| 1059 |
|
| { |
| 1060 |
540 |
| v += alpha[i]*(alpha[i]*diag[GETI(i)] - 2); |
| 1061 |
540 |
▶ | if(alpha[i] > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(alpha[i] > 0) |
387 |
153 |
540 |
|
| 1062 |
387 |
| ++nSV; |
| 1063 |
|
| } |
| 1064 |
2 |
▶ | info("Objective value = %lf\n",v/2); |
Branch: 1 branch
| Branch | True | False | Total |
| info("Objective value = %lf\n",v/2); |
2 |
0 |
2 |
|
| 1065 |
2 |
▶ | info("nSV = %d\n",nSV); |
Branch: 1 branch
| Branch | True | False | Total |
| info("nSV = %d\n",nSV); |
2 |
0 |
2 |
|
| 1066 |
|
| |
| 1067 |
2 |
▶ | delete [] QD; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] QD; |
2 |
0 |
2 |
|
| 1068 |
2 |
▶ | delete [] alpha; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] alpha; |
2 |
0 |
2 |
|
| 1069 |
2 |
▶ | delete [] y; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] y; |
2 |
0 |
2 |
|
| 1070 |
2 |
▶ | delete [] index; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] index; |
2 |
0 |
2 |
|
| 1071 |
|
| |
| 1072 |
2 |
| return iter; |
| 1073 |
|
| } |
| 1074 |
|
| |
| 1075 |
|
| |
| 1076 |
|
| // A coordinate descent algorithm for |
| 1077 |
|
| // L1-loss and L2-loss epsilon-SVR dual problem |
| 1078 |
|
| // |
| 1079 |
|
| // min_\beta 0.5\beta^T (Q + diag(lambda)) \beta - p \sum_{i=1}^l|\beta_i| + \sum_{i=1}^l yi\beta_i, |
| 1080 |
|
| // s.t. -upper_bound_i <= \beta_i <= upper_bound_i, |
| 1081 |
|
| // |
| 1082 |
|
| // where Qij = xi^T xj and |
| 1083 |
|
| // D is a diagonal matrix |
| 1084 |
|
| // |
| 1085 |
|
| // In L1-SVM case: |
| 1086 |
|
| // upper_bound_i = C |
| 1087 |
|
| // lambda_i = 0 |
| 1088 |
|
| // In L2-SVM case: |
| 1089 |
|
| // upper_bound_i = INF |
| 1090 |
|
| // lambda_i = 1/(2*C) |
| 1091 |
|
| // |
| 1092 |
|
| // Given: |
| 1093 |
|
| // x, y, p, C |
| 1094 |
|
| // eps is the stopping tolerance |
| 1095 |
|
| // |
| 1096 |
|
| // solution will be put in w |
| 1097 |
|
| // |
| 1098 |
|
| // this function returns the number of iterations |
| 1099 |
|
| // |
| 1100 |
|
| // See Algorithm 4 of Ho and Lin, 2012 |
| 1101 |
|
| |
| 1102 |
|
| #undef GETI |
| 1103 |
|
| #define GETI(i) (0) |
| 1104 |
|
| // To support weights for instances, use GETI(i) (i) |
| 1105 |
|
| |
| 1106 |
0 |
| static int solve_l2r_l1l2_svr(const problem *prob, const parameter *param, double *w, int max_iter=300) |
| 1107 |
|
| { |
| 1108 |
0 |
| const int solver_type = param->solver_type; |
| 1109 |
0 |
| int l = prob->l; |
| 1110 |
0 |
| double C = param->C; |
| 1111 |
0 |
| double p = param->p; |
| 1112 |
0 |
| int w_size = prob->n; |
| 1113 |
0 |
| double eps = param->eps; |
| 1114 |
0 |
| int i, s, iter = 0; |
| 1115 |
0 |
| int active_size = l; |
| 1116 |
0 |
▶ | int *index = new int[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| int *index = new int[l]; |
0 |
0 |
0 |
| int *index = new int[l]; |
0 |
0 |
0 |
|
| 1117 |
|
| |
| 1118 |
|
| double d, G, H; |
| 1119 |
0 |
| double Gmax_old = INF; |
| 1120 |
|
| double Gmax_new, Gnorm1_new; |
| 1121 |
0 |
| double Gnorm1_init = -1.0; // Gnorm1_init is initialized at the first iteration |
| 1122 |
0 |
▶ | double *beta = new double[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *beta = new double[l]; |
0 |
0 |
0 |
| double *beta = new double[l]; |
0 |
0 |
0 |
|
| 1123 |
0 |
▶ | double *QD = new double[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *QD = new double[l]; |
0 |
0 |
0 |
| double *QD = new double[l]; |
0 |
0 |
0 |
|
| 1124 |
0 |
| double *y = prob->y; |
| 1125 |
|
| |
| 1126 |
|
| // L2R_L2LOSS_SVR_DUAL |
| 1127 |
|
| double lambda[1], upper_bound[1]; |
| 1128 |
0 |
| lambda[0] = 0.5/C; |
| 1129 |
0 |
| upper_bound[0] = INF; |
| 1130 |
|
| |
| 1131 |
0 |
▶ | if(solver_type == L2R_L1LOSS_SVR_DUAL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(solver_type == L2R_L1LOSS_SVR_DUAL) |
0 |
0 |
0 |
|
| 1132 |
|
| { |
| 1133 |
0 |
| lambda[0] = 0; |
| 1134 |
0 |
| upper_bound[0] = C; |
| 1135 |
|
| } |
| 1136 |
|
| |
| 1137 |
|
| // Initial beta can be set here. Note that |
| 1138 |
|
| // -upper_bound <= beta[i] <= upper_bound |
| 1139 |
0 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
0 |
0 |
0 |
|
| 1140 |
0 |
| beta[i] = 0; |
| 1141 |
|
| |
| 1142 |
0 |
▶ | for(i=0; i<w_size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<w_size; i++) |
0 |
0 |
0 |
|
| 1143 |
0 |
| w[i] = 0; |
| 1144 |
0 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
0 |
0 |
0 |
|
| 1145 |
|
| { |
| 1146 |
0 |
| feature_node * const xi = prob->x[i]; |
| 1147 |
0 |
| QD[i] = sparse_operator::nrm2_sq(xi); |
| 1148 |
0 |
| sparse_operator::axpy(beta[i], xi, w); |
| 1149 |
|
| |
| 1150 |
0 |
| index[i] = i; |
| 1151 |
|
| } |
| 1152 |
|
| |
| 1153 |
|
| |
| 1154 |
0 |
▶ | while(iter < max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| while(iter < max_iter) |
0 |
0 |
0 |
|
| 1155 |
|
| { |
| 1156 |
0 |
| Gmax_new = 0; |
| 1157 |
0 |
| Gnorm1_new = 0; |
| 1158 |
|
| |
| 1159 |
0 |
▶ | for(i=0; i<active_size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<active_size; i++) |
0 |
0 |
0 |
|
| 1160 |
|
| { |
| 1161 |
0 |
| int j = i+rand()%(active_size-i); |
| 1162 |
0 |
| swap(index[i], index[j]); |
| 1163 |
|
| } |
| 1164 |
|
| |
| 1165 |
0 |
▶ | for(s=0; s<active_size; s++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(s=0; s<active_size; s++) |
0 |
0 |
0 |
|
| 1166 |
|
| { |
| 1167 |
0 |
| i = index[s]; |
| 1168 |
0 |
| G = -y[i] + lambda[GETI(i)]*beta[i]; |
| 1169 |
0 |
| H = QD[i] + lambda[GETI(i)]; |
| 1170 |
|
| |
| 1171 |
0 |
| feature_node * const xi = prob->x[i]; |
| 1172 |
0 |
| G += sparse_operator::dot(w, xi); |
| 1173 |
|
| |
| 1174 |
0 |
| double Gp = G+p; |
| 1175 |
0 |
| double Gn = G-p; |
| 1176 |
0 |
| double violation = 0; |
| 1177 |
0 |
▶ | if(beta[i] == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(beta[i] == 0) |
0 |
0 |
0 |
|
| 1178 |
|
| { |
| 1179 |
0 |
▶ | if(Gp < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(Gp < 0) |
0 |
0 |
0 |
|
| 1180 |
0 |
| violation = -Gp; |
| 1181 |
0 |
▶ | else if(Gn > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(Gn > 0) |
0 |
0 |
0 |
|
| 1182 |
0 |
| violation = Gn; |
| 1183 |
0 |
▶ | else if(Gp>Gmax_old && Gn<-Gmax_old) |
Branch: 2 branches
| Branch | True | False | Total |
| else if(Gp>Gmax_old && Gn<-Gmax_old) |
0 |
0 |
0 |
| else if(Gp>Gmax_old && Gn<-Gmax_old) |
0 |
0 |
0 |
|
| 1184 |
|
| { |
| 1185 |
0 |
| active_size--; |
| 1186 |
0 |
| swap(index[s], index[active_size]); |
| 1187 |
0 |
| s--; |
| 1188 |
0 |
| continue; |
| 1189 |
|
| } |
| 1190 |
|
| } |
| 1191 |
0 |
▶ | else if(beta[i] >= upper_bound[GETI(i)]) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(beta[i] >= upper_bound[GETI(i)]) |
0 |
0 |
0 |
|
| 1192 |
|
| { |
| 1193 |
0 |
▶ | if(Gp > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(Gp > 0) |
0 |
0 |
0 |
|
| 1194 |
0 |
| violation = Gp; |
| 1195 |
0 |
▶ | else if(Gp < -Gmax_old) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(Gp < -Gmax_old) |
0 |
0 |
0 |
|
| 1196 |
|
| { |
| 1197 |
0 |
| active_size--; |
| 1198 |
0 |
| swap(index[s], index[active_size]); |
| 1199 |
0 |
| s--; |
| 1200 |
0 |
| continue; |
| 1201 |
|
| } |
| 1202 |
|
| } |
| 1203 |
0 |
▶ | else if(beta[i] <= -upper_bound[GETI(i)]) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(beta[i] <= -upper_bound[GETI(i)]) |
0 |
0 |
0 |
|
| 1204 |
|
| { |
| 1205 |
0 |
▶ | if(Gn < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(Gn < 0) |
0 |
0 |
0 |
|
| 1206 |
0 |
| violation = -Gn; |
| 1207 |
0 |
▶ | else if(Gn > Gmax_old) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(Gn > Gmax_old) |
0 |
0 |
0 |
|
| 1208 |
|
| { |
| 1209 |
0 |
| active_size--; |
| 1210 |
0 |
| swap(index[s], index[active_size]); |
| 1211 |
0 |
| s--; |
| 1212 |
0 |
| continue; |
| 1213 |
|
| } |
| 1214 |
|
| } |
| 1215 |
0 |
▶ | else if(beta[i] > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(beta[i] > 0) |
0 |
0 |
0 |
|
| 1216 |
0 |
| violation = fabs(Gp); |
| 1217 |
|
| else |
| 1218 |
0 |
| violation = fabs(Gn); |
| 1219 |
|
| |
| 1220 |
0 |
| Gmax_new = max(Gmax_new, violation); |
| 1221 |
0 |
| Gnorm1_new += violation; |
| 1222 |
|
| |
| 1223 |
|
| // obtain Newton direction d |
| 1224 |
0 |
▶ | if(Gp < H*beta[i]) |
Branch: 1 branch
| Branch | True | False | Total |
| if(Gp < H*beta[i]) |
0 |
0 |
0 |
|
| 1225 |
0 |
| d = -Gp/H; |
| 1226 |
0 |
▶ | else if(Gn > H*beta[i]) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(Gn > H*beta[i]) |
0 |
0 |
0 |
|
| 1227 |
0 |
| d = -Gn/H; |
| 1228 |
|
| else |
| 1229 |
0 |
| d = -beta[i]; |
| 1230 |
|
| |
| 1231 |
0 |
▶ | if(fabs(d) < 1.0e-12) |
Branch: 1 branch
| Branch | True | False | Total |
| if(fabs(d) < 1.0e-12) |
0 |
0 |
0 |
|
| 1232 |
0 |
| continue; |
| 1233 |
|
| |
| 1234 |
0 |
| double beta_old = beta[i]; |
| 1235 |
0 |
| beta[i] = min(max(beta[i]+d, -upper_bound[GETI(i)]), upper_bound[GETI(i)]); |
| 1236 |
0 |
| d = beta[i]-beta_old; |
| 1237 |
|
| |
| 1238 |
0 |
▶ | if(d != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(d != 0) |
0 |
0 |
0 |
|
| 1239 |
0 |
| sparse_operator::axpy(d, xi, w); |
| 1240 |
|
| } |
| 1241 |
|
| |
| 1242 |
0 |
▶ | if(iter == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(iter == 0) |
0 |
0 |
0 |
|
| 1243 |
0 |
| Gnorm1_init = Gnorm1_new; |
| 1244 |
0 |
| iter++; |
| 1245 |
0 |
▶ | if(iter % 10 == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(iter % 10 == 0) |
0 |
0 |
0 |
|
| 1246 |
0 |
▶ | info("."); |
Branch: 1 branch
| Branch | True | False | Total |
| info("."); |
0 |
0 |
0 |
|
| 1247 |
|
| |
| 1248 |
0 |
▶ | if(Gnorm1_new <= eps*Gnorm1_init) |
Branch: 1 branch
| Branch | True | False | Total |
| if(Gnorm1_new <= eps*Gnorm1_init) |
0 |
0 |
0 |
|
| 1249 |
|
| { |
| 1250 |
0 |
▶ | if(active_size == l) |
Branch: 1 branch
| Branch | True | False | Total |
| if(active_size == l) |
0 |
0 |
0 |
|
| 1251 |
0 |
| break; |
| 1252 |
|
| else |
| 1253 |
|
| { |
| 1254 |
0 |
| active_size = l; |
| 1255 |
0 |
▶ | info("*"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("*"); |
0 |
0 |
0 |
|
| 1256 |
0 |
| Gmax_old = INF; |
| 1257 |
0 |
| continue; |
| 1258 |
|
| } |
| 1259 |
|
| } |
| 1260 |
|
| |
| 1261 |
0 |
| Gmax_old = Gmax_new; |
| 1262 |
|
| } |
| 1263 |
|
| |
| 1264 |
0 |
▶ | info("\noptimization finished, #iter = %d\n", iter); |
Branch: 1 branch
| Branch | True | False | Total |
| info("\noptimization finished, #iter = %d\n", iter); |
0 |
0 |
0 |
|
| 1265 |
|
| |
| 1266 |
|
| // calculate objective value |
| 1267 |
0 |
| double v = 0; |
| 1268 |
0 |
| int nSV = 0; |
| 1269 |
0 |
▶ | for(i=0; i<w_size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<w_size; i++) |
0 |
0 |
0 |
|
| 1270 |
0 |
| v += w[i]*w[i]; |
| 1271 |
0 |
| v = 0.5*v; |
| 1272 |
0 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
0 |
0 |
0 |
|
| 1273 |
|
| { |
| 1274 |
0 |
| v += p*fabs(beta[i]) - y[i]*beta[i] + 0.5*lambda[GETI(i)]*beta[i]*beta[i]; |
| 1275 |
0 |
▶ | if(beta[i] != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(beta[i] != 0) |
0 |
0 |
0 |
|
| 1276 |
0 |
| nSV++; |
| 1277 |
|
| } |
| 1278 |
|
| |
| 1279 |
0 |
▶ | info("Objective value = %lf\n", v); |
Branch: 1 branch
| Branch | True | False | Total |
| info("Objective value = %lf\n", v); |
0 |
0 |
0 |
|
| 1280 |
0 |
▶ | info("nSV = %d\n",nSV); |
Branch: 1 branch
| Branch | True | False | Total |
| info("nSV = %d\n",nSV); |
0 |
0 |
0 |
|
| 1281 |
|
| |
| 1282 |
0 |
▶ | delete [] beta; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] beta; |
0 |
0 |
0 |
|
| 1283 |
0 |
▶ | delete [] QD; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] QD; |
0 |
0 |
0 |
|
| 1284 |
0 |
▶ | delete [] index; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] index; |
0 |
0 |
0 |
|
| 1285 |
|
| |
| 1286 |
0 |
| return iter; |
| 1287 |
|
| } |
| 1288 |
|
| |
| 1289 |
|
| |
| 1290 |
|
| // A coordinate descent algorithm for |
| 1291 |
|
| // the dual of L2-regularized logistic regression problems |
| 1292 |
|
| // |
| 1293 |
|
| // min_\alpha 0.5(\alpha^T Q \alpha) + \sum \alpha_i log (\alpha_i) + (upper_bound_i - \alpha_i) log (upper_bound_i - \alpha_i), |
| 1294 |
|
| // s.t. 0 <= \alpha_i <= upper_bound_i, |
| 1295 |
|
| // |
| 1296 |
|
| // where Qij = yi yj xi^T xj and |
| 1297 |
|
| // upper_bound_i = Cp if y_i = 1 |
| 1298 |
|
| // upper_bound_i = Cn if y_i = -1 |
| 1299 |
|
| // |
| 1300 |
|
| // Given: |
| 1301 |
|
| // x, y, Cp, Cn |
| 1302 |
|
| // eps is the stopping tolerance |
| 1303 |
|
| // |
| 1304 |
|
| // solution will be put in w |
| 1305 |
|
| // |
| 1306 |
|
| // this function returns the number of iterations |
| 1307 |
|
| // |
| 1308 |
|
| // See Algorithm 5 of Yu et al., MLJ 2010 |
| 1309 |
|
| |
| 1310 |
|
| #undef GETI |
| 1311 |
|
| #define GETI(i) (y[i]+1) |
| 1312 |
|
| // To support weights for instances, use GETI(i) (i) |
| 1313 |
|
| |
| 1314 |
0 |
| static int solve_l2r_lr_dual(const problem *prob, const parameter *param, double *w, double Cp, double Cn, int max_iter=300) |
| 1315 |
|
| { |
| 1316 |
0 |
| int l = prob->l; |
| 1317 |
0 |
| int w_size = prob->n; |
| 1318 |
0 |
| double eps = param->eps; |
| 1319 |
0 |
| int i, s, iter = 0; |
| 1320 |
0 |
▶ | double *xTx = new double[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *xTx = new double[l]; |
0 |
0 |
0 |
| double *xTx = new double[l]; |
0 |
0 |
0 |
|
| 1321 |
0 |
▶ | int *index = new int[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| int *index = new int[l]; |
0 |
0 |
0 |
| int *index = new int[l]; |
0 |
0 |
0 |
|
| 1322 |
0 |
▶ | double *alpha = new double[2*l]; // store alpha and C - alpha |
Branch: 2 branches
| Branch | True | False | Total |
| double *alpha = new double[2*l]; // store alpha and C - alpha |
0 |
0 |
0 |
| double *alpha = new double[2*l]; // store alpha and C - alpha |
0 |
0 |
0 |
|
| 1323 |
0 |
▶ | schar *y = new schar[l]; |
Branch: 1 branch
| Branch | True | False | Total |
| schar *y = new schar[l]; |
0 |
0 |
0 |
|
| 1324 |
0 |
| int max_inner_iter = 100; // for inner Newton |
| 1325 |
0 |
| double innereps = 1e-2; |
| 1326 |
0 |
| double innereps_min = min(1e-8, eps); |
| 1327 |
0 |
| double upper_bound[3] = {Cn, 0, Cp}; |
| 1328 |
|
| |
| 1329 |
0 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
0 |
0 |
0 |
|
| 1330 |
|
| { |
| 1331 |
0 |
▶ | if(prob->y[i] > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(prob->y[i] > 0) |
0 |
0 |
0 |
|
| 1332 |
|
| { |
| 1333 |
0 |
| y[i] = +1; |
| 1334 |
|
| } |
| 1335 |
|
| else |
| 1336 |
|
| { |
| 1337 |
0 |
| y[i] = -1; |
| 1338 |
|
| } |
| 1339 |
|
| } |
| 1340 |
|
| |
| 1341 |
|
| // Initial alpha can be set here. Note that |
| 1342 |
|
| // 0 < alpha[i] < upper_bound[GETI(i)] |
| 1343 |
|
| // alpha[2*i] + alpha[2*i+1] = upper_bound[GETI(i)] |
| 1344 |
0 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
0 |
0 |
0 |
|
| 1345 |
|
| { |
| 1346 |
0 |
| alpha[2*i] = min(0.001*upper_bound[GETI(i)], 1e-8); |
| 1347 |
0 |
| alpha[2*i+1] = upper_bound[GETI(i)] - alpha[2*i]; |
| 1348 |
|
| } |
| 1349 |
|
| |
| 1350 |
0 |
▶ | for(i=0; i<w_size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<w_size; i++) |
0 |
0 |
0 |
|
| 1351 |
0 |
| w[i] = 0; |
| 1352 |
0 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
0 |
0 |
0 |
|
| 1353 |
|
| { |
| 1354 |
0 |
| feature_node * const xi = prob->x[i]; |
| 1355 |
0 |
| xTx[i] = sparse_operator::nrm2_sq(xi); |
| 1356 |
0 |
| sparse_operator::axpy(y[i]*alpha[2*i], xi, w); |
| 1357 |
0 |
| index[i] = i; |
| 1358 |
|
| } |
| 1359 |
|
| |
| 1360 |
0 |
▶ | while (iter < max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| while (iter < max_iter) |
0 |
0 |
0 |
|
| 1361 |
|
| { |
| 1362 |
0 |
▶ | for (i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<l; i++) |
0 |
0 |
0 |
|
| 1363 |
|
| { |
| 1364 |
0 |
| int j = i+rand()%(l-i); |
| 1365 |
0 |
| swap(index[i], index[j]); |
| 1366 |
|
| } |
| 1367 |
0 |
| int newton_iter = 0; |
| 1368 |
0 |
| double Gmax = 0; |
| 1369 |
0 |
▶ | for (s=0; s<l; s++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (s=0; s<l; s++) |
0 |
0 |
0 |
|
| 1370 |
|
| { |
| 1371 |
0 |
| i = index[s]; |
| 1372 |
0 |
| const schar yi = y[i]; |
| 1373 |
0 |
| double C = upper_bound[GETI(i)]; |
| 1374 |
0 |
| double ywTx = 0, xisq = xTx[i]; |
| 1375 |
0 |
| feature_node * const xi = prob->x[i]; |
| 1376 |
0 |
| ywTx = yi*sparse_operator::dot(w, xi); |
| 1377 |
0 |
| double a = xisq, b = ywTx; |
| 1378 |
|
| |
| 1379 |
|
| // Decide to minimize g_1(z) or g_2(z) |
| 1380 |
0 |
| int ind1 = 2*i, ind2 = 2*i+1, sign = 1; |
| 1381 |
0 |
▶ | if(0.5*a*(alpha[ind2]-alpha[ind1])+b < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(0.5*a*(alpha[ind2]-alpha[ind1])+b < 0) |
0 |
0 |
0 |
|
| 1382 |
|
| { |
| 1383 |
0 |
| ind1 = 2*i+1; |
| 1384 |
0 |
| ind2 = 2*i; |
| 1385 |
0 |
| sign = -1; |
| 1386 |
|
| } |
| 1387 |
|
| |
| 1388 |
|
| // g_t(z) = z*log(z) + (C-z)*log(C-z) + 0.5a(z-alpha_old)^2 + sign*b(z-alpha_old) |
| 1389 |
0 |
| double alpha_old = alpha[ind1]; |
| 1390 |
0 |
| double z = alpha_old; |
| 1391 |
0 |
▶ | if(C - z < 0.5 * C) |
Branch: 1 branch
| Branch | True | False | Total |
| if(C - z < 0.5 * C) |
0 |
0 |
0 |
|
| 1392 |
0 |
| z = 0.1*z; |
| 1393 |
0 |
| double gp = a*(z-alpha_old)+sign*b+log(z/(C-z)); |
| 1394 |
0 |
| Gmax = max(Gmax, fabs(gp)); |
| 1395 |
|
| |
| 1396 |
|
| // Newton method on the sub-problem |
| 1397 |
0 |
| const double eta = 0.1; // xi in the paper |
| 1398 |
0 |
| int inner_iter = 0; |
| 1399 |
0 |
▶ | while (inner_iter <= max_inner_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| while (inner_iter <= max_inner_iter) |
0 |
0 |
0 |
|
| 1400 |
|
| { |
| 1401 |
0 |
▶ | if(fabs(gp) < innereps) |
Branch: 1 branch
| Branch | True | False | Total |
| if(fabs(gp) < innereps) |
0 |
0 |
0 |
|
| 1402 |
0 |
| break; |
| 1403 |
0 |
| double gpp = a + C/(C-z)/z; |
| 1404 |
0 |
| double tmpz = z - gp/gpp; |
| 1405 |
0 |
▶ | if(tmpz <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(tmpz <= 0) |
0 |
0 |
0 |
|
| 1406 |
0 |
| z *= eta; |
| 1407 |
|
| else // tmpz in (0, C) |
| 1408 |
0 |
| z = tmpz; |
| 1409 |
0 |
| gp = a*(z-alpha_old)+sign*b+log(z/(C-z)); |
| 1410 |
0 |
| newton_iter++; |
| 1411 |
0 |
| inner_iter++; |
| 1412 |
|
| } |
| 1413 |
|
| |
| 1414 |
0 |
▶ | if(inner_iter > 0) // update w |
Branch: 1 branch
| Branch | True | False | Total |
| if(inner_iter > 0) // update w |
0 |
0 |
0 |
|
| 1415 |
|
| { |
| 1416 |
0 |
| alpha[ind1] = z; |
| 1417 |
0 |
| alpha[ind2] = C-z; |
| 1418 |
0 |
| sparse_operator::axpy(sign*(z-alpha_old)*yi, xi, w); |
| 1419 |
|
| } |
| 1420 |
|
| } |
| 1421 |
|
| |
| 1422 |
0 |
| iter++; |
| 1423 |
0 |
▶ | if(iter % 10 == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(iter % 10 == 0) |
0 |
0 |
0 |
|
| 1424 |
0 |
▶ | info("."); |
Branch: 1 branch
| Branch | True | False | Total |
| info("."); |
0 |
0 |
0 |
|
| 1425 |
|
| |
| 1426 |
0 |
▶ | if(Gmax < eps) |
Branch: 1 branch
| Branch | True | False | Total |
| if(Gmax < eps) |
0 |
0 |
0 |
|
| 1427 |
0 |
| break; |
| 1428 |
|
| |
| 1429 |
0 |
▶ | if(newton_iter <= l/10) |
Branch: 1 branch
| Branch | True | False | Total |
| if(newton_iter <= l/10) |
0 |
0 |
0 |
|
| 1430 |
0 |
| innereps = max(innereps_min, 0.1*innereps); |
| 1431 |
|
| |
| 1432 |
|
| } |
| 1433 |
|
| |
| 1434 |
0 |
▶ | info("\noptimization finished, #iter = %d\n",iter); |
Branch: 1 branch
| Branch | True | False | Total |
| info("\noptimization finished, #iter = %d\n",iter); |
0 |
0 |
0 |
|
| 1435 |
|
| |
| 1436 |
|
| // calculate objective value |
| 1437 |
|
| |
| 1438 |
0 |
| double v = 0; |
| 1439 |
0 |
▶ | for(i=0; i<w_size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<w_size; i++) |
0 |
0 |
0 |
|
| 1440 |
0 |
| v += w[i] * w[i]; |
| 1441 |
0 |
| v *= 0.5; |
| 1442 |
0 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
0 |
0 |
0 |
|
| 1443 |
0 |
| v += alpha[2*i] * log(alpha[2*i]) + alpha[2*i+1] * log(alpha[2*i+1]) |
| 1444 |
0 |
| - upper_bound[GETI(i)] * log(upper_bound[GETI(i)]); |
| 1445 |
0 |
▶ | info("Objective value = %lf\n", v); |
Branch: 1 branch
| Branch | True | False | Total |
| info("Objective value = %lf\n", v); |
0 |
0 |
0 |
|
| 1446 |
|
| |
| 1447 |
0 |
▶ | delete [] xTx; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] xTx; |
0 |
0 |
0 |
|
| 1448 |
0 |
▶ | delete [] alpha; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] alpha; |
0 |
0 |
0 |
|
| 1449 |
0 |
▶ | delete [] y; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] y; |
0 |
0 |
0 |
|
| 1450 |
0 |
▶ | delete [] index; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] index; |
0 |
0 |
0 |
|
| 1451 |
|
| |
| 1452 |
0 |
| return iter; |
| 1453 |
|
| } |
| 1454 |
|
| |
| 1455 |
|
| // A coordinate descent algorithm for |
| 1456 |
|
| // L1-regularized L2-loss support vector classification |
| 1457 |
|
| // |
| 1458 |
|
| // min_w \sum |wj| + C \sum max(0, 1-yi w^T xi)^2, |
| 1459 |
|
| // |
| 1460 |
|
| // Given: |
| 1461 |
|
| // x, y, Cp, Cn |
| 1462 |
|
| // eps is the stopping tolerance |
| 1463 |
|
| // |
| 1464 |
|
| // solution will be put in w |
| 1465 |
|
| // |
| 1466 |
|
| // this function returns the number of iterations |
| 1467 |
|
| // |
| 1468 |
|
| // See Yuan et al. (2010) and appendix of LIBLINEAR paper, Fan et al. (2008) |
| 1469 |
|
| // |
| 1470 |
|
| // To not regularize the bias (i.e., regularize_bias = 0), a constant feature = 1 |
| 1471 |
|
| // must have been added to the original data. (see -B and -R option) |
| 1472 |
|
| |
| 1473 |
|
| #undef GETI |
| 1474 |
|
| #define GETI(i) (y[i]+1) |
| 1475 |
|
| // To support weights for instances, use GETI(i) (i) |
| 1476 |
|
| |
| 1477 |
0 |
| static int solve_l1r_l2_svc(const problem *prob_col, const parameter* param, double *w, double Cp, double Cn, double eps) |
| 1478 |
|
| { |
| 1479 |
0 |
| int l = prob_col->l; |
| 1480 |
0 |
| int w_size = prob_col->n; |
| 1481 |
0 |
| int regularize_bias = param->regularize_bias; |
| 1482 |
0 |
| int j, s, iter = 0; |
| 1483 |
0 |
| int max_iter = 1000; |
| 1484 |
0 |
| int active_size = w_size; |
| 1485 |
0 |
| int max_num_linesearch = 20; |
| 1486 |
|
| |
| 1487 |
0 |
| double sigma = 0.01; |
| 1488 |
|
| double d, G_loss, G, H; |
| 1489 |
0 |
| double Gmax_old = INF; |
| 1490 |
|
| double Gmax_new, Gnorm1_new; |
| 1491 |
0 |
| double Gnorm1_init = -1.0; // Gnorm1_init is initialized at the first iteration |
| 1492 |
|
| double d_old, d_diff; |
| 1493 |
0 |
| double loss_old = 0, loss_new; |
| 1494 |
|
| double appxcond, cond; |
| 1495 |
|
| |
| 1496 |
0 |
▶ | int *index = new int[w_size]; |
Branch: 2 branches
| Branch | True | False | Total |
| int *index = new int[w_size]; |
0 |
0 |
0 |
| int *index = new int[w_size]; |
0 |
0 |
0 |
|
| 1497 |
0 |
▶ | schar *y = new schar[l]; |
Branch: 1 branch
| Branch | True | False | Total |
| schar *y = new schar[l]; |
0 |
0 |
0 |
|
| 1498 |
0 |
▶ | double *b = new double[l]; // b = 1-ywTx |
Branch: 2 branches
| Branch | True | False | Total |
| double *b = new double[l]; // b = 1-ywTx |
0 |
0 |
0 |
| double *b = new double[l]; // b = 1-ywTx |
0 |
0 |
0 |
|
| 1499 |
0 |
▶ | double *xj_sq = new double[w_size]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *xj_sq = new double[w_size]; |
0 |
0 |
0 |
| double *xj_sq = new double[w_size]; |
0 |
0 |
0 |
|
| 1500 |
|
| feature_node *x; |
| 1501 |
|
| |
| 1502 |
0 |
| double C[3] = {Cn,0,Cp}; |
| 1503 |
|
| |
| 1504 |
|
| // Initial w can be set here. |
| 1505 |
0 |
▶ | for(j=0; j<w_size; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<w_size; j++) |
0 |
0 |
0 |
|
| 1506 |
0 |
| w[j] = 0; |
| 1507 |
|
| |
| 1508 |
0 |
▶ | for(j=0; j<l; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<l; j++) |
0 |
0 |
0 |
|
| 1509 |
|
| { |
| 1510 |
0 |
| b[j] = 1; |
| 1511 |
0 |
▶ | if(prob_col->y[j] > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(prob_col->y[j] > 0) |
0 |
0 |
0 |
|
| 1512 |
0 |
| y[j] = 1; |
| 1513 |
|
| else |
| 1514 |
0 |
| y[j] = -1; |
| 1515 |
|
| } |
| 1516 |
0 |
▶ | for(j=0; j<w_size; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<w_size; j++) |
0 |
0 |
0 |
|
| 1517 |
|
| { |
| 1518 |
0 |
| index[j] = j; |
| 1519 |
0 |
| xj_sq[j] = 0; |
| 1520 |
0 |
| x = prob_col->x[j]; |
| 1521 |
0 |
▶ | while(x->index != -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(x->index != -1) |
0 |
0 |
0 |
|
| 1522 |
|
| { |
| 1523 |
0 |
| int ind = x->index-1; |
| 1524 |
0 |
| x->value *= y[ind]; // x->value stores yi*xij |
| 1525 |
0 |
| double val = x->value; |
| 1526 |
0 |
| b[ind] -= w[j]*val; |
| 1527 |
0 |
| xj_sq[j] += C[GETI(ind)]*val*val; |
| 1528 |
0 |
| x++; |
| 1529 |
|
| } |
| 1530 |
|
| } |
| 1531 |
|
| |
| 1532 |
0 |
▶ | while(iter < max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| while(iter < max_iter) |
0 |
0 |
0 |
|
| 1533 |
|
| { |
| 1534 |
0 |
| Gmax_new = 0; |
| 1535 |
0 |
| Gnorm1_new = 0; |
| 1536 |
|
| |
| 1537 |
0 |
▶ | for(j=0; j<active_size; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<active_size; j++) |
0 |
0 |
0 |
|
| 1538 |
|
| { |
| 1539 |
0 |
| int i = j+rand()%(active_size-j); |
| 1540 |
0 |
| swap(index[i], index[j]); |
| 1541 |
|
| } |
| 1542 |
|
| |
| 1543 |
0 |
▶ | for(s=0; s<active_size; s++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(s=0; s<active_size; s++) |
0 |
0 |
0 |
|
| 1544 |
|
| { |
| 1545 |
0 |
| j = index[s]; |
| 1546 |
0 |
| G_loss = 0; |
| 1547 |
0 |
| H = 0; |
| 1548 |
|
| |
| 1549 |
0 |
| x = prob_col->x[j]; |
| 1550 |
0 |
▶ | while(x->index != -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(x->index != -1) |
0 |
0 |
0 |
|
| 1551 |
|
| { |
| 1552 |
0 |
| int ind = x->index-1; |
| 1553 |
0 |
▶ | if(b[ind] > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(b[ind] > 0) |
0 |
0 |
0 |
|
| 1554 |
|
| { |
| 1555 |
0 |
| double val = x->value; |
| 1556 |
0 |
| double tmp = C[GETI(ind)]*val; |
| 1557 |
0 |
| G_loss -= tmp*b[ind]; |
| 1558 |
0 |
| H += tmp*val; |
| 1559 |
|
| } |
| 1560 |
0 |
| x++; |
| 1561 |
|
| } |
| 1562 |
0 |
| G_loss *= 2; |
| 1563 |
|
| |
| 1564 |
0 |
| G = G_loss; |
| 1565 |
0 |
| H *= 2; |
| 1566 |
0 |
| H = max(H, 1e-12); |
| 1567 |
|
| |
| 1568 |
0 |
| double violation = 0; |
| 1569 |
0 |
| double Gp = 0, Gn = 0; |
| 1570 |
0 |
▶ | if(j == w_size-1 && regularize_bias == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if(j == w_size-1 && regularize_bias == 0) |
0 |
0 |
0 |
| if(j == w_size-1 && regularize_bias == 0) |
0 |
0 |
0 |
|
| 1571 |
0 |
| violation = fabs(G); |
| 1572 |
|
| else |
| 1573 |
|
| { |
| 1574 |
0 |
| Gp = G+1; |
| 1575 |
0 |
| Gn = G-1; |
| 1576 |
0 |
▶ | if(w[j] == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(w[j] == 0) |
0 |
0 |
0 |
|
| 1577 |
|
| { |
| 1578 |
0 |
▶ | if(Gp < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(Gp < 0) |
0 |
0 |
0 |
|
| 1579 |
0 |
| violation = -Gp; |
| 1580 |
0 |
▶ | else if(Gn > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(Gn > 0) |
0 |
0 |
0 |
|
| 1581 |
0 |
| violation = Gn; |
| 1582 |
0 |
▶ | else if(Gp>Gmax_old/l && Gn<-Gmax_old/l) |
Branch: 2 branches
| Branch | True | False | Total |
| else if(Gp>Gmax_old/l && Gn<-Gmax_old/l) |
0 |
0 |
0 |
| else if(Gp>Gmax_old/l && Gn<-Gmax_old/l) |
0 |
0 |
0 |
|
| 1583 |
|
| { |
| 1584 |
0 |
| active_size--; |
| 1585 |
0 |
| swap(index[s], index[active_size]); |
| 1586 |
0 |
| s--; |
| 1587 |
0 |
| continue; |
| 1588 |
|
| } |
| 1589 |
|
| } |
| 1590 |
0 |
▶ | else if(w[j] > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(w[j] > 0) |
0 |
0 |
0 |
|
| 1591 |
0 |
| violation = fabs(Gp); |
| 1592 |
|
| else |
| 1593 |
0 |
| violation = fabs(Gn); |
| 1594 |
|
| } |
| 1595 |
0 |
| Gmax_new = max(Gmax_new, violation); |
| 1596 |
0 |
| Gnorm1_new += violation; |
| 1597 |
|
| |
| 1598 |
|
| // obtain Newton direction d |
| 1599 |
0 |
▶ | if(j == w_size-1 && regularize_bias == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if(j == w_size-1 && regularize_bias == 0) |
0 |
0 |
0 |
| if(j == w_size-1 && regularize_bias == 0) |
0 |
0 |
0 |
|
| 1600 |
0 |
| d = -G/H; |
| 1601 |
|
| else |
| 1602 |
|
| { |
| 1603 |
0 |
▶ | if(Gp < H*w[j]) |
Branch: 1 branch
| Branch | True | False | Total |
| if(Gp < H*w[j]) |
0 |
0 |
0 |
|
| 1604 |
0 |
| d = -Gp/H; |
| 1605 |
0 |
▶ | else if(Gn > H*w[j]) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(Gn > H*w[j]) |
0 |
0 |
0 |
|
| 1606 |
0 |
| d = -Gn/H; |
| 1607 |
|
| else |
| 1608 |
0 |
| d = -w[j]; |
| 1609 |
|
| } |
| 1610 |
|
| |
| 1611 |
0 |
▶ | if(fabs(d) < 1.0e-12) |
Branch: 1 branch
| Branch | True | False | Total |
| if(fabs(d) < 1.0e-12) |
0 |
0 |
0 |
|
| 1612 |
0 |
| continue; |
| 1613 |
|
| |
| 1614 |
|
| double delta; |
| 1615 |
0 |
▶ | if(j == w_size-1 && regularize_bias == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if(j == w_size-1 && regularize_bias == 0) |
0 |
0 |
0 |
| if(j == w_size-1 && regularize_bias == 0) |
0 |
0 |
0 |
|
| 1616 |
0 |
| delta = G*d; |
| 1617 |
|
| else |
| 1618 |
0 |
| delta = fabs(w[j]+d)-fabs(w[j]) + G*d; |
| 1619 |
0 |
| d_old = 0; |
| 1620 |
|
| int num_linesearch; |
| 1621 |
0 |
▶ | for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++) |
0 |
0 |
0 |
|
| 1622 |
|
| { |
| 1623 |
0 |
| d_diff = d_old - d; |
| 1624 |
0 |
▶ | if(j == w_size-1 && regularize_bias == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if(j == w_size-1 && regularize_bias == 0) |
0 |
0 |
0 |
| if(j == w_size-1 && regularize_bias == 0) |
0 |
0 |
0 |
|
| 1625 |
0 |
| cond = -sigma*delta; |
| 1626 |
|
| else |
| 1627 |
0 |
| cond = fabs(w[j]+d)-fabs(w[j]) - sigma*delta; |
| 1628 |
|
| |
| 1629 |
0 |
| appxcond = xj_sq[j]*d*d + G_loss*d + cond; |
| 1630 |
0 |
▶ | if(appxcond <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(appxcond <= 0) |
0 |
0 |
0 |
|
| 1631 |
|
| { |
| 1632 |
0 |
| x = prob_col->x[j]; |
| 1633 |
0 |
| sparse_operator::axpy(d_diff, x, b); |
| 1634 |
0 |
| break; |
| 1635 |
|
| } |
| 1636 |
|
| |
| 1637 |
0 |
▶ | if(num_linesearch == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(num_linesearch == 0) |
0 |
0 |
0 |
|
| 1638 |
|
| { |
| 1639 |
0 |
| loss_old = 0; |
| 1640 |
0 |
| loss_new = 0; |
| 1641 |
0 |
| x = prob_col->x[j]; |
| 1642 |
0 |
▶ | while(x->index != -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(x->index != -1) |
0 |
0 |
0 |
|
| 1643 |
|
| { |
| 1644 |
0 |
| int ind = x->index-1; |
| 1645 |
0 |
▶ | if(b[ind] > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(b[ind] > 0) |
0 |
0 |
0 |
|
| 1646 |
0 |
| loss_old += C[GETI(ind)]*b[ind]*b[ind]; |
| 1647 |
0 |
| double b_new = b[ind] + d_diff*x->value; |
| 1648 |
0 |
| b[ind] = b_new; |
| 1649 |
0 |
▶ | if(b_new > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(b_new > 0) |
0 |
0 |
0 |
|
| 1650 |
0 |
| loss_new += C[GETI(ind)]*b_new*b_new; |
| 1651 |
0 |
| x++; |
| 1652 |
|
| } |
| 1653 |
|
| } |
| 1654 |
|
| else |
| 1655 |
|
| { |
| 1656 |
0 |
| loss_new = 0; |
| 1657 |
0 |
| x = prob_col->x[j]; |
| 1658 |
0 |
▶ | while(x->index != -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(x->index != -1) |
0 |
0 |
0 |
|
| 1659 |
|
| { |
| 1660 |
0 |
| int ind = x->index-1; |
| 1661 |
0 |
| double b_new = b[ind] + d_diff*x->value; |
| 1662 |
0 |
| b[ind] = b_new; |
| 1663 |
0 |
▶ | if(b_new > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(b_new > 0) |
0 |
0 |
0 |
|
| 1664 |
0 |
| loss_new += C[GETI(ind)]*b_new*b_new; |
| 1665 |
0 |
| x++; |
| 1666 |
|
| } |
| 1667 |
|
| } |
| 1668 |
|
| |
| 1669 |
0 |
| cond = cond + loss_new - loss_old; |
| 1670 |
0 |
▶ | if(cond <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(cond <= 0) |
0 |
0 |
0 |
|
| 1671 |
0 |
| break; |
| 1672 |
|
| else |
| 1673 |
|
| { |
| 1674 |
0 |
| d_old = d; |
| 1675 |
0 |
| d *= 0.5; |
| 1676 |
0 |
| delta *= 0.5; |
| 1677 |
|
| } |
| 1678 |
|
| } |
| 1679 |
|
| |
| 1680 |
0 |
| w[j] += d; |
| 1681 |
|
| |
| 1682 |
|
| // recompute b[] if line search takes too many steps |
| 1683 |
0 |
▶ | if(num_linesearch >= max_num_linesearch) |
Branch: 1 branch
| Branch | True | False | Total |
| if(num_linesearch >= max_num_linesearch) |
0 |
0 |
0 |
|
| 1684 |
|
| { |
| 1685 |
0 |
▶ | info("#"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("#"); |
0 |
0 |
0 |
|
| 1686 |
0 |
▶ | for(int i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i=0; i<l; i++) |
0 |
0 |
0 |
|
| 1687 |
0 |
| b[i] = 1; |
| 1688 |
|
| |
| 1689 |
0 |
▶ | for(int i=0; i<w_size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i=0; i<w_size; i++) |
0 |
0 |
0 |
|
| 1690 |
|
| { |
| 1691 |
0 |
▶ | if(w[i]==0) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if(w[i]==0) continue; |
0 |
0 |
0 |
|
| 1692 |
0 |
| x = prob_col->x[i]; |
| 1693 |
0 |
| sparse_operator::axpy(-w[i], x, b); |
| 1694 |
|
| } |
| 1695 |
|
| } |
| 1696 |
|
| } |
| 1697 |
|
| |
| 1698 |
0 |
▶ | if(iter == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(iter == 0) |
0 |
0 |
0 |
|
| 1699 |
0 |
| Gnorm1_init = Gnorm1_new; |
| 1700 |
0 |
| iter++; |
| 1701 |
0 |
▶ | if(iter % 10 == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(iter % 10 == 0) |
0 |
0 |
0 |
|
| 1702 |
0 |
▶ | info("."); |
Branch: 1 branch
| Branch | True | False | Total |
| info("."); |
0 |
0 |
0 |
|
| 1703 |
|
| |
| 1704 |
0 |
▶ | if(Gnorm1_new <= eps*Gnorm1_init) |
Branch: 1 branch
| Branch | True | False | Total |
| if(Gnorm1_new <= eps*Gnorm1_init) |
0 |
0 |
0 |
|
| 1705 |
|
| { |
| 1706 |
0 |
▶ | if(active_size == w_size) |
Branch: 1 branch
| Branch | True | False | Total |
| if(active_size == w_size) |
0 |
0 |
0 |
|
| 1707 |
0 |
| break; |
| 1708 |
|
| else |
| 1709 |
|
| { |
| 1710 |
0 |
| active_size = w_size; |
| 1711 |
0 |
▶ | info("*"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("*"); |
0 |
0 |
0 |
|
| 1712 |
0 |
| Gmax_old = INF; |
| 1713 |
0 |
| continue; |
| 1714 |
|
| } |
| 1715 |
|
| } |
| 1716 |
|
| |
| 1717 |
0 |
| Gmax_old = Gmax_new; |
| 1718 |
|
| } |
| 1719 |
|
| |
| 1720 |
0 |
▶ | info("\noptimization finished, #iter = %d\n", iter); |
Branch: 1 branch
| Branch | True | False | Total |
| info("\noptimization finished, #iter = %d\n", iter); |
0 |
0 |
0 |
|
| 1721 |
0 |
▶ | if(iter >= max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| if(iter >= max_iter) |
0 |
0 |
0 |
|
| 1722 |
0 |
▶ | info("\nWARNING: reaching max number of iterations\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("\nWARNING: reaching max number of iterations\n"); |
0 |
0 |
0 |
|
| 1723 |
|
| |
| 1724 |
|
| // calculate objective value |
| 1725 |
|
| |
| 1726 |
0 |
| double v = 0; |
| 1727 |
0 |
| int nnz = 0; |
| 1728 |
0 |
▶ | for(j=0; j<w_size; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<w_size; j++) |
0 |
0 |
0 |
|
| 1729 |
|
| { |
| 1730 |
0 |
| x = prob_col->x[j]; |
| 1731 |
0 |
▶ | while(x->index != -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(x->index != -1) |
0 |
0 |
0 |
|
| 1732 |
|
| { |
| 1733 |
0 |
| x->value *= prob_col->y[x->index-1]; // restore x->value |
| 1734 |
0 |
| x++; |
| 1735 |
|
| } |
| 1736 |
0 |
▶ | if(w[j] != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(w[j] != 0) |
0 |
0 |
0 |
|
| 1737 |
|
| { |
| 1738 |
0 |
| v += fabs(w[j]); |
| 1739 |
0 |
| nnz++; |
| 1740 |
|
| } |
| 1741 |
|
| } |
| 1742 |
0 |
▶ | if (regularize_bias == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (regularize_bias == 0) |
0 |
0 |
0 |
|
| 1743 |
0 |
| v -= fabs(w[w_size-1]); |
| 1744 |
0 |
▶ | for(j=0; j<l; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<l; j++) |
0 |
0 |
0 |
|
| 1745 |
0 |
▶ | if(b[j] > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(b[j] > 0) |
0 |
0 |
0 |
|
| 1746 |
0 |
| v += C[GETI(j)]*b[j]*b[j]; |
| 1747 |
|
| |
| 1748 |
0 |
▶ | info("Objective value = %lf\n", v); |
Branch: 1 branch
| Branch | True | False | Total |
| info("Objective value = %lf\n", v); |
0 |
0 |
0 |
|
| 1749 |
0 |
▶ | info("#nonzeros/#features = %d/%d\n", nnz, w_size); |
Branch: 1 branch
| Branch | True | False | Total |
| info("#nonzeros/#features = %d/%d\n", nnz, w_size); |
0 |
0 |
0 |
|
| 1750 |
|
| |
| 1751 |
0 |
▶ | delete [] index; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] index; |
0 |
0 |
0 |
|
| 1752 |
0 |
▶ | delete [] y; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] y; |
0 |
0 |
0 |
|
| 1753 |
0 |
▶ | delete [] b; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] b; |
0 |
0 |
0 |
|
| 1754 |
0 |
▶ | delete [] xj_sq; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] xj_sq; |
0 |
0 |
0 |
|
| 1755 |
|
| |
| 1756 |
0 |
| return iter; |
| 1757 |
|
| } |
| 1758 |
|
| |
| 1759 |
|
| // A coordinate descent algorithm for |
| 1760 |
|
| // L1-regularized logistic regression problems |
| 1761 |
|
| // |
| 1762 |
|
| // min_w \sum |wj| + C \sum log(1+exp(-yi w^T xi)), |
| 1763 |
|
| // |
| 1764 |
|
| // Given: |
| 1765 |
|
| // x, y, Cp, Cn |
| 1766 |
|
| // eps is the stopping tolerance |
| 1767 |
|
| // |
| 1768 |
|
| // solution will be put in w |
| 1769 |
|
| // |
| 1770 |
|
| // this function returns the number of iterations |
| 1771 |
|
| // |
| 1772 |
|
| // See Yuan et al. (2011) and appendix of LIBLINEAR paper, Fan et al. (2008) |
| 1773 |
|
| // |
| 1774 |
|
| // To not regularize the bias (i.e., regularize_bias = 0), a constant feature = 1 |
| 1775 |
|
| // must have been added to the original data. (see -B and -R option) |
| 1776 |
|
| |
| 1777 |
|
| #undef GETI |
| 1778 |
|
| #define GETI(i) (y[i]+1) |
| 1779 |
|
| // To support weights for instances, use GETI(i) (i) |
| 1780 |
|
| |
| 1781 |
0 |
| static int solve_l1r_lr(const problem *prob_col, const parameter *param, double *w, double Cp, double Cn, double eps) |
| 1782 |
|
| { |
| 1783 |
0 |
| int l = prob_col->l; |
| 1784 |
0 |
| int w_size = prob_col->n; |
| 1785 |
0 |
| int regularize_bias = param->regularize_bias; |
| 1786 |
0 |
| int j, s, newton_iter=0, iter=0; |
| 1787 |
0 |
| int max_newton_iter = 100; |
| 1788 |
0 |
| int max_iter = 1000; |
| 1789 |
0 |
| int max_num_linesearch = 20; |
| 1790 |
|
| int active_size; |
| 1791 |
|
| int QP_active_size; |
| 1792 |
|
| |
| 1793 |
0 |
| double nu = 1e-12; |
| 1794 |
0 |
| double inner_eps = 1; |
| 1795 |
0 |
| double sigma = 0.01; |
| 1796 |
|
| double w_norm, w_norm_new; |
| 1797 |
|
| double z, G, H; |
| 1798 |
0 |
| double Gnorm1_init = -1.0; // Gnorm1_init is initialized at the first iteration |
| 1799 |
0 |
| double Gmax_old = INF; |
| 1800 |
|
| double Gmax_new, Gnorm1_new; |
| 1801 |
0 |
| double QP_Gmax_old = INF; |
| 1802 |
|
| double QP_Gmax_new, QP_Gnorm1_new; |
| 1803 |
|
| double delta, negsum_xTd, cond; |
| 1804 |
|
| |
| 1805 |
0 |
▶ | int *index = new int[w_size]; |
Branch: 2 branches
| Branch | True | False | Total |
| int *index = new int[w_size]; |
0 |
0 |
0 |
| int *index = new int[w_size]; |
0 |
0 |
0 |
|
| 1806 |
0 |
▶ | schar *y = new schar[l]; |
Branch: 1 branch
| Branch | True | False | Total |
| schar *y = new schar[l]; |
0 |
0 |
0 |
|
| 1807 |
0 |
▶ | double *Hdiag = new double[w_size]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *Hdiag = new double[w_size]; |
0 |
0 |
0 |
| double *Hdiag = new double[w_size]; |
0 |
0 |
0 |
|
| 1808 |
0 |
▶ | double *Grad = new double[w_size]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *Grad = new double[w_size]; |
0 |
0 |
0 |
| double *Grad = new double[w_size]; |
0 |
0 |
0 |
|
| 1809 |
0 |
▶ | double *wpd = new double[w_size]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *wpd = new double[w_size]; |
0 |
0 |
0 |
| double *wpd = new double[w_size]; |
0 |
0 |
0 |
|
| 1810 |
0 |
▶ | double *xjneg_sum = new double[w_size]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *xjneg_sum = new double[w_size]; |
0 |
0 |
0 |
| double *xjneg_sum = new double[w_size]; |
0 |
0 |
0 |
|
| 1811 |
0 |
▶ | double *xTd = new double[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *xTd = new double[l]; |
0 |
0 |
0 |
| double *xTd = new double[l]; |
0 |
0 |
0 |
|
| 1812 |
0 |
▶ | double *exp_wTx = new double[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *exp_wTx = new double[l]; |
0 |
0 |
0 |
| double *exp_wTx = new double[l]; |
0 |
0 |
0 |
|
| 1813 |
0 |
▶ | double *exp_wTx_new = new double[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *exp_wTx_new = new double[l]; |
0 |
0 |
0 |
| double *exp_wTx_new = new double[l]; |
0 |
0 |
0 |
|
| 1814 |
0 |
▶ | double *tau = new double[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *tau = new double[l]; |
0 |
0 |
0 |
| double *tau = new double[l]; |
0 |
0 |
0 |
|
| 1815 |
0 |
▶ | double *D = new double[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *D = new double[l]; |
0 |
0 |
0 |
| double *D = new double[l]; |
0 |
0 |
0 |
|
| 1816 |
|
| feature_node *x; |
| 1817 |
|
| |
| 1818 |
0 |
| double C[3] = {Cn,0,Cp}; |
| 1819 |
|
| |
| 1820 |
|
| // Initial w can be set here. |
| 1821 |
0 |
▶ | for(j=0; j<w_size; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<w_size; j++) |
0 |
0 |
0 |
|
| 1822 |
0 |
| w[j] = 0; |
| 1823 |
|
| |
| 1824 |
0 |
▶ | for(j=0; j<l; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<l; j++) |
0 |
0 |
0 |
|
| 1825 |
|
| { |
| 1826 |
0 |
▶ | if(prob_col->y[j] > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(prob_col->y[j] > 0) |
0 |
0 |
0 |
|
| 1827 |
0 |
| y[j] = 1; |
| 1828 |
|
| else |
| 1829 |
0 |
| y[j] = -1; |
| 1830 |
|
| |
| 1831 |
0 |
| exp_wTx[j] = 0; |
| 1832 |
|
| } |
| 1833 |
|
| |
| 1834 |
0 |
| w_norm = 0; |
| 1835 |
0 |
▶ | for(j=0; j<w_size; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<w_size; j++) |
0 |
0 |
0 |
|
| 1836 |
|
| { |
| 1837 |
0 |
| w_norm += fabs(w[j]); |
| 1838 |
0 |
| wpd[j] = w[j]; |
| 1839 |
0 |
| index[j] = j; |
| 1840 |
0 |
| xjneg_sum[j] = 0; |
| 1841 |
0 |
| x = prob_col->x[j]; |
| 1842 |
0 |
▶ | while(x->index != -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(x->index != -1) |
0 |
0 |
0 |
|
| 1843 |
|
| { |
| 1844 |
0 |
| int ind = x->index-1; |
| 1845 |
0 |
| double val = x->value; |
| 1846 |
0 |
| exp_wTx[ind] += w[j]*val; |
| 1847 |
0 |
▶ | if(y[ind] == -1) |
Branch: 1 branch
| Branch | True | False | Total |
| if(y[ind] == -1) |
0 |
0 |
0 |
|
| 1848 |
0 |
| xjneg_sum[j] += C[GETI(ind)]*val; |
| 1849 |
0 |
| x++; |
| 1850 |
|
| } |
| 1851 |
|
| } |
| 1852 |
0 |
▶ | if (regularize_bias == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (regularize_bias == 0) |
0 |
0 |
0 |
|
| 1853 |
0 |
| w_norm -= fabs(w[w_size-1]); |
| 1854 |
|
| |
| 1855 |
0 |
▶ | for(j=0; j<l; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<l; j++) |
0 |
0 |
0 |
|
| 1856 |
|
| { |
| 1857 |
0 |
| exp_wTx[j] = exp(exp_wTx[j]); |
| 1858 |
0 |
| double tau_tmp = 1/(1+exp_wTx[j]); |
| 1859 |
0 |
| tau[j] = C[GETI(j)]*tau_tmp; |
| 1860 |
0 |
| D[j] = C[GETI(j)]*exp_wTx[j]*tau_tmp*tau_tmp; |
| 1861 |
|
| } |
| 1862 |
|
| |
| 1863 |
0 |
▶ | while(newton_iter < max_newton_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| while(newton_iter < max_newton_iter) |
0 |
0 |
0 |
|
| 1864 |
|
| { |
| 1865 |
0 |
| Gmax_new = 0; |
| 1866 |
0 |
| Gnorm1_new = 0; |
| 1867 |
0 |
| active_size = w_size; |
| 1868 |
|
| |
| 1869 |
0 |
▶ | for(s=0; s<active_size; s++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(s=0; s<active_size; s++) |
0 |
0 |
0 |
|
| 1870 |
|
| { |
| 1871 |
0 |
| j = index[s]; |
| 1872 |
0 |
| Hdiag[j] = nu; |
| 1873 |
0 |
| Grad[j] = 0; |
| 1874 |
|
| |
| 1875 |
0 |
| double tmp = 0; |
| 1876 |
0 |
| x = prob_col->x[j]; |
| 1877 |
0 |
▶ | while(x->index != -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(x->index != -1) |
0 |
0 |
0 |
|
| 1878 |
|
| { |
| 1879 |
0 |
| int ind = x->index-1; |
| 1880 |
0 |
| Hdiag[j] += x->value*x->value*D[ind]; |
| 1881 |
0 |
| tmp += x->value*tau[ind]; |
| 1882 |
0 |
| x++; |
| 1883 |
|
| } |
| 1884 |
0 |
| Grad[j] = -tmp + xjneg_sum[j]; |
| 1885 |
|
| |
| 1886 |
0 |
| double violation = 0; |
| 1887 |
0 |
▶ | if (j == w_size-1 && regularize_bias == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (j == w_size-1 && regularize_bias == 0) |
0 |
0 |
0 |
| if (j == w_size-1 && regularize_bias == 0) |
0 |
0 |
0 |
|
| 1888 |
0 |
| violation = fabs(Grad[j]); |
| 1889 |
|
| else |
| 1890 |
|
| { |
| 1891 |
0 |
| double Gp = Grad[j]+1; |
| 1892 |
0 |
| double Gn = Grad[j]-1; |
| 1893 |
0 |
▶ | if(w[j] == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(w[j] == 0) |
0 |
0 |
0 |
|
| 1894 |
|
| { |
| 1895 |
0 |
▶ | if(Gp < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(Gp < 0) |
0 |
0 |
0 |
|
| 1896 |
0 |
| violation = -Gp; |
| 1897 |
0 |
▶ | else if(Gn > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(Gn > 0) |
0 |
0 |
0 |
|
| 1898 |
0 |
| violation = Gn; |
| 1899 |
|
| //outer-level shrinking |
| 1900 |
0 |
▶ | else if(Gp>Gmax_old/l && Gn<-Gmax_old/l) |
Branch: 2 branches
| Branch | True | False | Total |
| else if(Gp>Gmax_old/l && Gn<-Gmax_old/l) |
0 |
0 |
0 |
| else if(Gp>Gmax_old/l && Gn<-Gmax_old/l) |
0 |
0 |
0 |
|
| 1901 |
|
| { |
| 1902 |
0 |
| active_size--; |
| 1903 |
0 |
| swap(index[s], index[active_size]); |
| 1904 |
0 |
| s--; |
| 1905 |
0 |
| continue; |
| 1906 |
|
| } |
| 1907 |
|
| } |
| 1908 |
0 |
▶ | else if(w[j] > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(w[j] > 0) |
0 |
0 |
0 |
|
| 1909 |
0 |
| violation = fabs(Gp); |
| 1910 |
|
| else |
| 1911 |
0 |
| violation = fabs(Gn); |
| 1912 |
|
| } |
| 1913 |
0 |
| Gmax_new = max(Gmax_new, violation); |
| 1914 |
0 |
| Gnorm1_new += violation; |
| 1915 |
|
| } |
| 1916 |
|
| |
| 1917 |
0 |
▶ | if(newton_iter == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(newton_iter == 0) |
0 |
0 |
0 |
|
| 1918 |
0 |
| Gnorm1_init = Gnorm1_new; |
| 1919 |
|
| |
| 1920 |
0 |
▶ | if(Gnorm1_new <= eps*Gnorm1_init) |
Branch: 1 branch
| Branch | True | False | Total |
| if(Gnorm1_new <= eps*Gnorm1_init) |
0 |
0 |
0 |
|
| 1921 |
0 |
| break; |
| 1922 |
|
| |
| 1923 |
0 |
| iter = 0; |
| 1924 |
0 |
| QP_Gmax_old = INF; |
| 1925 |
0 |
| QP_active_size = active_size; |
| 1926 |
|
| |
| 1927 |
0 |
▶ | for(int i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i=0; i<l; i++) |
0 |
0 |
0 |
|
| 1928 |
0 |
| xTd[i] = 0; |
| 1929 |
|
| |
| 1930 |
|
| // optimize QP over wpd |
| 1931 |
0 |
▶ | while(iter < max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| while(iter < max_iter) |
0 |
0 |
0 |
|
| 1932 |
|
| { |
| 1933 |
0 |
| QP_Gmax_new = 0; |
| 1934 |
0 |
| QP_Gnorm1_new = 0; |
| 1935 |
|
| |
| 1936 |
0 |
▶ | for(j=0; j<QP_active_size; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<QP_active_size; j++) |
0 |
0 |
0 |
|
| 1937 |
|
| { |
| 1938 |
0 |
| int i = j+rand()%(QP_active_size-j); |
| 1939 |
0 |
| swap(index[i], index[j]); |
| 1940 |
|
| } |
| 1941 |
|
| |
| 1942 |
0 |
▶ | for(s=0; s<QP_active_size; s++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(s=0; s<QP_active_size; s++) |
0 |
0 |
0 |
|
| 1943 |
|
| { |
| 1944 |
0 |
| j = index[s]; |
| 1945 |
0 |
| H = Hdiag[j]; |
| 1946 |
|
| |
| 1947 |
0 |
| x = prob_col->x[j]; |
| 1948 |
0 |
| G = Grad[j] + (wpd[j]-w[j])*nu; |
| 1949 |
0 |
▶ | while(x->index != -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(x->index != -1) |
0 |
0 |
0 |
|
| 1950 |
|
| { |
| 1951 |
0 |
| int ind = x->index-1; |
| 1952 |
0 |
| G += x->value*D[ind]*xTd[ind]; |
| 1953 |
0 |
| x++; |
| 1954 |
|
| } |
| 1955 |
|
| |
| 1956 |
0 |
| double violation = 0; |
| 1957 |
0 |
▶ | if (j == w_size-1 && regularize_bias == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (j == w_size-1 && regularize_bias == 0) |
0 |
0 |
0 |
| if (j == w_size-1 && regularize_bias == 0) |
0 |
0 |
0 |
|
| 1958 |
|
| { |
| 1959 |
|
| // bias term not shrunken |
| 1960 |
0 |
| violation = fabs(G); |
| 1961 |
0 |
| z = -G/H; |
| 1962 |
|
| } |
| 1963 |
|
| else |
| 1964 |
|
| { |
| 1965 |
0 |
| double Gp = G+1; |
| 1966 |
0 |
| double Gn = G-1; |
| 1967 |
0 |
▶ | if(wpd[j] == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(wpd[j] == 0) |
0 |
0 |
0 |
|
| 1968 |
|
| { |
| 1969 |
0 |
▶ | if(Gp < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(Gp < 0) |
0 |
0 |
0 |
|
| 1970 |
0 |
| violation = -Gp; |
| 1971 |
0 |
▶ | else if(Gn > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(Gn > 0) |
0 |
0 |
0 |
|
| 1972 |
0 |
| violation = Gn; |
| 1973 |
|
| //inner-level shrinking |
| 1974 |
0 |
▶ | else if(Gp>QP_Gmax_old/l && Gn<-QP_Gmax_old/l) |
Branch: 2 branches
| Branch | True | False | Total |
| else if(Gp>QP_Gmax_old/l && Gn<-QP_Gmax_old/l) |
0 |
0 |
0 |
| else if(Gp>QP_Gmax_old/l && Gn<-QP_Gmax_old/l) |
0 |
0 |
0 |
|
| 1975 |
|
| { |
| 1976 |
0 |
| QP_active_size--; |
| 1977 |
0 |
| swap(index[s], index[QP_active_size]); |
| 1978 |
0 |
| s--; |
| 1979 |
0 |
| continue; |
| 1980 |
|
| } |
| 1981 |
|
| } |
| 1982 |
0 |
▶ | else if(wpd[j] > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(wpd[j] > 0) |
0 |
0 |
0 |
|
| 1983 |
0 |
| violation = fabs(Gp); |
| 1984 |
|
| else |
| 1985 |
0 |
| violation = fabs(Gn); |
| 1986 |
|
| |
| 1987 |
|
| // obtain solution of one-variable problem |
| 1988 |
0 |
▶ | if(Gp < H*wpd[j]) |
Branch: 1 branch
| Branch | True | False | Total |
| if(Gp < H*wpd[j]) |
0 |
0 |
0 |
|
| 1989 |
0 |
| z = -Gp/H; |
| 1990 |
0 |
▶ | else if(Gn > H*wpd[j]) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(Gn > H*wpd[j]) |
0 |
0 |
0 |
|
| 1991 |
0 |
| z = -Gn/H; |
| 1992 |
|
| else |
| 1993 |
0 |
| z = -wpd[j]; |
| 1994 |
|
| } |
| 1995 |
0 |
| QP_Gmax_new = max(QP_Gmax_new, violation); |
| 1996 |
0 |
| QP_Gnorm1_new += violation; |
| 1997 |
|
| |
| 1998 |
0 |
▶ | if(fabs(z) < 1.0e-12) |
Branch: 1 branch
| Branch | True | False | Total |
| if(fabs(z) < 1.0e-12) |
0 |
0 |
0 |
|
| 1999 |
0 |
| continue; |
| 2000 |
0 |
| z = min(max(z,-10.0),10.0); |
| 2001 |
|
| |
| 2002 |
0 |
| wpd[j] += z; |
| 2003 |
|
| |
| 2004 |
0 |
| x = prob_col->x[j]; |
| 2005 |
0 |
| sparse_operator::axpy(z, x, xTd); |
| 2006 |
|
| } |
| 2007 |
|
| |
| 2008 |
0 |
| iter++; |
| 2009 |
|
| |
| 2010 |
0 |
▶ | if(QP_Gnorm1_new <= inner_eps*Gnorm1_init) |
Branch: 1 branch
| Branch | True | False | Total |
| if(QP_Gnorm1_new <= inner_eps*Gnorm1_init) |
0 |
0 |
0 |
|
| 2011 |
|
| { |
| 2012 |
|
| //inner stopping |
| 2013 |
0 |
▶ | if(QP_active_size == active_size) |
Branch: 1 branch
| Branch | True | False | Total |
| if(QP_active_size == active_size) |
0 |
0 |
0 |
|
| 2014 |
0 |
| break; |
| 2015 |
|
| //active set reactivation |
| 2016 |
|
| else |
| 2017 |
|
| { |
| 2018 |
0 |
| QP_active_size = active_size; |
| 2019 |
0 |
| QP_Gmax_old = INF; |
| 2020 |
0 |
| continue; |
| 2021 |
|
| } |
| 2022 |
|
| } |
| 2023 |
|
| |
| 2024 |
0 |
| QP_Gmax_old = QP_Gmax_new; |
| 2025 |
|
| } |
| 2026 |
|
| |
| 2027 |
0 |
▶ | if(iter >= max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| if(iter >= max_iter) |
0 |
0 |
0 |
|
| 2028 |
0 |
▶ | info("WARNING: reaching max number of inner iterations\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("WARNING: reaching max number of inner iterations\n"); |
0 |
0 |
0 |
|
| 2029 |
|
| |
| 2030 |
0 |
| delta = 0; |
| 2031 |
0 |
| w_norm_new = 0; |
| 2032 |
0 |
▶ | for(j=0; j<w_size; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<w_size; j++) |
0 |
0 |
0 |
|
| 2033 |
|
| { |
| 2034 |
0 |
| delta += Grad[j]*(wpd[j]-w[j]); |
| 2035 |
0 |
▶ | if(wpd[j] != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(wpd[j] != 0) |
0 |
0 |
0 |
|
| 2036 |
0 |
| w_norm_new += fabs(wpd[j]); |
| 2037 |
|
| } |
| 2038 |
0 |
▶ | if (regularize_bias == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (regularize_bias == 0) |
0 |
0 |
0 |
|
| 2039 |
0 |
| w_norm_new -= fabs(wpd[w_size-1]); |
| 2040 |
0 |
| delta += (w_norm_new-w_norm); |
| 2041 |
|
| |
| 2042 |
0 |
| negsum_xTd = 0; |
| 2043 |
0 |
▶ | for(int i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i=0; i<l; i++) |
0 |
0 |
0 |
|
| 2044 |
0 |
▶ | if(y[i] == -1) |
Branch: 1 branch
| Branch | True | False | Total |
| if(y[i] == -1) |
0 |
0 |
0 |
|
| 2045 |
0 |
| negsum_xTd += C[GETI(i)]*xTd[i]; |
| 2046 |
|
| |
| 2047 |
|
| int num_linesearch; |
| 2048 |
0 |
▶ | for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++) |
0 |
0 |
0 |
|
| 2049 |
|
| { |
| 2050 |
0 |
| cond = w_norm_new - w_norm + negsum_xTd - sigma*delta; |
| 2051 |
|
| |
| 2052 |
0 |
▶ | for(int i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i=0; i<l; i++) |
0 |
0 |
0 |
|
| 2053 |
|
| { |
| 2054 |
0 |
| double exp_xTd = exp(xTd[i]); |
| 2055 |
0 |
| exp_wTx_new[i] = exp_wTx[i]*exp_xTd; |
| 2056 |
0 |
| cond += C[GETI(i)]*log((1+exp_wTx_new[i])/(exp_xTd+exp_wTx_new[i])); |
| 2057 |
|
| } |
| 2058 |
|
| |
| 2059 |
0 |
▶ | if(cond <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(cond <= 0) |
0 |
0 |
0 |
|
| 2060 |
|
| { |
| 2061 |
0 |
| w_norm = w_norm_new; |
| 2062 |
0 |
▶ | for(j=0; j<w_size; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<w_size; j++) |
0 |
0 |
0 |
|
| 2063 |
0 |
| w[j] = wpd[j]; |
| 2064 |
0 |
▶ | for(int i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i=0; i<l; i++) |
0 |
0 |
0 |
|
| 2065 |
|
| { |
| 2066 |
0 |
| exp_wTx[i] = exp_wTx_new[i]; |
| 2067 |
0 |
| double tau_tmp = 1/(1+exp_wTx[i]); |
| 2068 |
0 |
| tau[i] = C[GETI(i)]*tau_tmp; |
| 2069 |
0 |
| D[i] = C[GETI(i)]*exp_wTx[i]*tau_tmp*tau_tmp; |
| 2070 |
|
| } |
| 2071 |
0 |
| break; |
| 2072 |
|
| } |
| 2073 |
|
| else |
| 2074 |
|
| { |
| 2075 |
0 |
| w_norm_new = 0; |
| 2076 |
0 |
▶ | for(j=0; j<w_size; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<w_size; j++) |
0 |
0 |
0 |
|
| 2077 |
|
| { |
| 2078 |
0 |
| wpd[j] = (w[j]+wpd[j])*0.5; |
| 2079 |
0 |
▶ | if(wpd[j] != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(wpd[j] != 0) |
0 |
0 |
0 |
|
| 2080 |
0 |
| w_norm_new += fabs(wpd[j]); |
| 2081 |
|
| } |
| 2082 |
0 |
▶ | if (regularize_bias == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (regularize_bias == 0) |
0 |
0 |
0 |
|
| 2083 |
0 |
| w_norm_new -= fabs(wpd[w_size-1]); |
| 2084 |
0 |
| delta *= 0.5; |
| 2085 |
0 |
| negsum_xTd *= 0.5; |
| 2086 |
0 |
▶ | for(int i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i=0; i<l; i++) |
0 |
0 |
0 |
|
| 2087 |
0 |
| xTd[i] *= 0.5; |
| 2088 |
|
| } |
| 2089 |
|
| } |
| 2090 |
|
| |
| 2091 |
|
| // Recompute some info due to too many line search steps |
| 2092 |
0 |
▶ | if(num_linesearch >= max_num_linesearch) |
Branch: 1 branch
| Branch | True | False | Total |
| if(num_linesearch >= max_num_linesearch) |
0 |
0 |
0 |
|
| 2093 |
|
| { |
| 2094 |
0 |
▶ | for(int i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i=0; i<l; i++) |
0 |
0 |
0 |
|
| 2095 |
0 |
| exp_wTx[i] = 0; |
| 2096 |
|
| |
| 2097 |
0 |
▶ | for(int i=0; i<w_size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i=0; i<w_size; i++) |
0 |
0 |
0 |
|
| 2098 |
|
| { |
| 2099 |
0 |
▶ | if(w[i]==0) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if(w[i]==0) continue; |
0 |
0 |
0 |
|
| 2100 |
0 |
| x = prob_col->x[i]; |
| 2101 |
0 |
| sparse_operator::axpy(w[i], x, exp_wTx); |
| 2102 |
|
| } |
| 2103 |
|
| |
| 2104 |
0 |
▶ | for(int i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i=0; i<l; i++) |
0 |
0 |
0 |
|
| 2105 |
0 |
| exp_wTx[i] = exp(exp_wTx[i]); |
| 2106 |
|
| } |
| 2107 |
|
| |
| 2108 |
0 |
▶ | if(iter == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if(iter == 1) |
0 |
0 |
0 |
|
| 2109 |
0 |
| inner_eps *= 0.25; |
| 2110 |
|
| |
| 2111 |
0 |
| newton_iter++; |
| 2112 |
0 |
| Gmax_old = Gmax_new; |
| 2113 |
|
| |
| 2114 |
0 |
▶ | info("iter %3d #CD cycles %d\n", newton_iter, iter); |
Branch: 1 branch
| Branch | True | False | Total |
| info("iter %3d #CD cycles %d\n", newton_iter, iter); |
0 |
0 |
0 |
|
| 2115 |
|
| } |
| 2116 |
|
| |
| 2117 |
0 |
▶ | info("=========================\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("=========================\n"); |
0 |
0 |
0 |
|
| 2118 |
0 |
▶ | info("optimization finished, #iter = %d\n", newton_iter); |
Branch: 1 branch
| Branch | True | False | Total |
| info("optimization finished, #iter = %d\n", newton_iter); |
0 |
0 |
0 |
|
| 2119 |
0 |
▶ | if(newton_iter >= max_newton_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| if(newton_iter >= max_newton_iter) |
0 |
0 |
0 |
|
| 2120 |
0 |
▶ | info("WARNING: reaching max number of iterations\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("WARNING: reaching max number of iterations\n"); |
0 |
0 |
0 |
|
| 2121 |
|
| |
| 2122 |
|
| // calculate objective value |
| 2123 |
|
| |
| 2124 |
0 |
| double v = 0; |
| 2125 |
0 |
| int nnz = 0; |
| 2126 |
0 |
▶ | for(j=0; j<w_size; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<w_size; j++) |
0 |
0 |
0 |
|
| 2127 |
0 |
▶ | if(w[j] != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(w[j] != 0) |
0 |
0 |
0 |
|
| 2128 |
|
| { |
| 2129 |
0 |
| v += fabs(w[j]); |
| 2130 |
0 |
| nnz++; |
| 2131 |
|
| } |
| 2132 |
0 |
▶ | if (regularize_bias == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (regularize_bias == 0) |
0 |
0 |
0 |
|
| 2133 |
0 |
| v -= fabs(w[w_size-1]); |
| 2134 |
0 |
▶ | for(j=0; j<l; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<l; j++) |
0 |
0 |
0 |
|
| 2135 |
0 |
▶ | if(y[j] == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if(y[j] == 1) |
0 |
0 |
0 |
|
| 2136 |
0 |
| v += C[GETI(j)]*log(1+1/exp_wTx[j]); |
| 2137 |
|
| else |
| 2138 |
0 |
| v += C[GETI(j)]*log(1+exp_wTx[j]); |
| 2139 |
|
| |
| 2140 |
0 |
▶ | info("Objective value = %lf\n", v); |
Branch: 1 branch
| Branch | True | False | Total |
| info("Objective value = %lf\n", v); |
0 |
0 |
0 |
|
| 2141 |
0 |
▶ | info("#nonzeros/#features = %d/%d\n", nnz, w_size); |
Branch: 1 branch
| Branch | True | False | Total |
| info("#nonzeros/#features = %d/%d\n", nnz, w_size); |
0 |
0 |
0 |
|
| 2142 |
|
| |
| 2143 |
0 |
▶ | delete [] index; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] index; |
0 |
0 |
0 |
|
| 2144 |
0 |
▶ | delete [] y; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] y; |
0 |
0 |
0 |
|
| 2145 |
0 |
▶ | delete [] Hdiag; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] Hdiag; |
0 |
0 |
0 |
|
| 2146 |
0 |
▶ | delete [] Grad; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] Grad; |
0 |
0 |
0 |
|
| 2147 |
0 |
▶ | delete [] wpd; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] wpd; |
0 |
0 |
0 |
|
| 2148 |
0 |
▶ | delete [] xjneg_sum; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] xjneg_sum; |
0 |
0 |
0 |
|
| 2149 |
0 |
▶ | delete [] xTd; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] xTd; |
0 |
0 |
0 |
|
| 2150 |
0 |
▶ | delete [] exp_wTx; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] exp_wTx; |
0 |
0 |
0 |
|
| 2151 |
0 |
▶ | delete [] exp_wTx_new; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] exp_wTx_new; |
0 |
0 |
0 |
|
| 2152 |
0 |
▶ | delete [] tau; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] tau; |
0 |
0 |
0 |
|
| 2153 |
0 |
▶ | delete [] D; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] D; |
0 |
0 |
0 |
|
| 2154 |
|
| |
| 2155 |
0 |
| return newton_iter; |
| 2156 |
|
| } |
| 2157 |
|
| |
| 2158 |
0 |
| static int compare_feature_node(const void *a, const void *b) |
| 2159 |
|
| { |
| 2160 |
0 |
| double a_value = (*(feature_node *)a).value; |
| 2161 |
0 |
| double b_value = (*(feature_node *)b).value; |
| 2162 |
0 |
| int a_index = (*(feature_node *)a).index; |
| 2163 |
0 |
| int b_index = (*(feature_node *)b).index; |
| 2164 |
|
| |
| 2165 |
0 |
▶ | if(a_value < b_value) |
Branch: 1 branch
| Branch | True | False | Total |
| if(a_value < b_value) |
0 |
0 |
0 |
|
| 2166 |
0 |
| return -1; |
| 2167 |
0 |
▶ | else if(a_value == b_value) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(a_value == b_value) |
0 |
0 |
0 |
|
| 2168 |
|
| { |
| 2169 |
0 |
▶ | if(a_index < b_index) |
Branch: 1 branch
| Branch | True | False | Total |
| if(a_index < b_index) |
0 |
0 |
0 |
|
| 2170 |
0 |
| return -1; |
| 2171 |
0 |
▶ | else if(a_index == b_index) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(a_index == b_index) |
0 |
0 |
0 |
|
| 2172 |
0 |
| return 0; |
| 2173 |
|
| } |
| 2174 |
0 |
| return 1; |
| 2175 |
|
| } |
| 2176 |
|
| |
| 2177 |
|
| // elements before the returned index are < pivot, while those after are >= pivot |
| 2178 |
0 |
| static int partition(feature_node *nodes, int low, int high) |
| 2179 |
|
| { |
| 2180 |
|
| int i; |
| 2181 |
|
| int index; |
| 2182 |
|
| |
| 2183 |
0 |
| swap(nodes[low + rand()%(high-low+1)], nodes[high]); // select and move pivot to the end |
| 2184 |
|
| |
| 2185 |
0 |
| index = low; |
| 2186 |
0 |
▶ | for(i = low; i < high; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = low; i < high; i++) |
0 |
0 |
0 |
|
| 2187 |
0 |
▶ | if (compare_feature_node(&nodes[i], &nodes[high]) == -1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (compare_feature_node(&nodes[i], &nodes[high]) == -1) |
0 |
0 |
0 |
|
| 2188 |
|
| { |
| 2189 |
0 |
| swap(nodes[index], nodes[i]); |
| 2190 |
0 |
| index++; |
| 2191 |
|
| } |
| 2192 |
|
| |
| 2193 |
0 |
| swap(nodes[high], nodes[index]); |
| 2194 |
0 |
| return index; |
| 2195 |
|
| } |
| 2196 |
|
| |
| 2197 |
|
| // rearrange nodes so that nodes[:k] contains nodes with the k smallest values. |
| 2198 |
0 |
| static void quick_select_min_k(feature_node *nodes, int low, int high, int k) |
| 2199 |
|
| { |
| 2200 |
|
| int pivot; |
| 2201 |
0 |
▶ | if(low == high) |
Branch: 1 branch
| Branch | True | False | Total |
| if(low == high) |
0 |
0 |
0 |
|
| 2202 |
0 |
| return; |
| 2203 |
0 |
| pivot = partition(nodes, low, high); |
| 2204 |
0 |
▶ | if(pivot == k) |
Branch: 1 branch
| Branch | True | False | Total |
| if(pivot == k) |
0 |
0 |
0 |
|
| 2205 |
0 |
| return; |
| 2206 |
0 |
▶ | else if(k-1 < pivot) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(k-1 < pivot) |
0 |
0 |
0 |
|
| 2207 |
0 |
| return quick_select_min_k(nodes, low, pivot-1, k); |
| 2208 |
|
| else |
| 2209 |
0 |
| return quick_select_min_k(nodes, pivot+1, high, k); |
| 2210 |
|
| } |
| 2211 |
|
| |
| 2212 |
|
| // A two-level coordinate descent algorithm for |
| 2213 |
|
| // a scaled one-class SVM dual problem |
| 2214 |
|
| // |
| 2215 |
|
| // min_\alpha 0.5(\alpha^T Q \alpha), |
| 2216 |
|
| // s.t. 0 <= \alpha_i <= 1 and |
| 2217 |
|
| // e^T \alpha = \nu l |
| 2218 |
|
| // |
| 2219 |
|
| // where Qij = xi^T xj |
| 2220 |
|
| // |
| 2221 |
|
| // Given: |
| 2222 |
|
| // x, nu |
| 2223 |
|
| // eps is the stopping tolerance |
| 2224 |
|
| // |
| 2225 |
|
| // solution will be put in w and rho |
| 2226 |
|
| // |
| 2227 |
|
| // this function returns the number of iterations |
| 2228 |
|
| // |
| 2229 |
|
| // See Algorithm 7 in supplementary materials of Chou et al., SDM 2020. |
| 2230 |
|
| |
| 2231 |
0 |
| static int solve_oneclass_svm(const problem *prob, const parameter *param, double *w, double *rho) |
| 2232 |
|
| { |
| 2233 |
0 |
| int l = prob->l; |
| 2234 |
0 |
| int w_size = prob->n; |
| 2235 |
0 |
| double eps = param->eps; |
| 2236 |
0 |
| double nu = param->nu; |
| 2237 |
0 |
| int i, j, s, iter = 0; |
| 2238 |
|
| double Gi, Gj; |
| 2239 |
|
| double Qij, quad_coef, delta, sum; |
| 2240 |
|
| double old_alpha_i; |
| 2241 |
0 |
▶ | double *QD = new double[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *QD = new double[l]; |
0 |
0 |
0 |
| double *QD = new double[l]; |
0 |
0 |
0 |
|
| 2242 |
0 |
▶ | double *G = new double[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *G = new double[l]; |
0 |
0 |
0 |
| double *G = new double[l]; |
0 |
0 |
0 |
|
| 2243 |
0 |
▶ | int *index = new int[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| int *index = new int[l]; |
0 |
0 |
0 |
| int *index = new int[l]; |
0 |
0 |
0 |
|
| 2244 |
0 |
▶ | double *alpha = new double[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *alpha = new double[l]; |
0 |
0 |
0 |
| double *alpha = new double[l]; |
0 |
0 |
0 |
|
| 2245 |
|
| int max_inner_iter; |
| 2246 |
0 |
| int max_iter = 1000; |
| 2247 |
0 |
| int active_size = l; |
| 2248 |
|
| |
| 2249 |
|
| double negGmax; // max { -grad(f)_i | i in Iup } |
| 2250 |
|
| double negGmin; // min { -grad(f)_i | i in Ilow } |
| 2251 |
|
| // Iup = { i | alpha_i < 1 }, Ilow = { i | alpha_i > 0 } |
| 2252 |
0 |
▶ | feature_node *max_negG_of_Iup = new feature_node[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| feature_node *max_negG_of_Iup = new feature_node[l]; |
0 |
0 |
0 |
| feature_node *max_negG_of_Iup = new feature_node[l]; |
0 |
0 |
0 |
|
| 2253 |
0 |
▶ | feature_node *min_negG_of_Ilow = new feature_node[l]; |
Branch: 2 branches
| Branch | True | False | Total |
| feature_node *min_negG_of_Ilow = new feature_node[l]; |
0 |
0 |
0 |
| feature_node *min_negG_of_Ilow = new feature_node[l]; |
0 |
0 |
0 |
|
| 2254 |
|
| feature_node node; |
| 2255 |
|
| |
| 2256 |
0 |
| int n = (int)(nu*l); // # of alpha's at upper bound |
| 2257 |
0 |
▶ | for(i=0; i<n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<n; i++) |
0 |
0 |
0 |
|
| 2258 |
0 |
| alpha[i] = 1; |
| 2259 |
0 |
▶ | if (n<l) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n<l) |
0 |
0 |
0 |
|
| 2260 |
0 |
| alpha[i] = nu*l-n; |
| 2261 |
0 |
▶ | for(i=n+1; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=n+1; i<l; i++) |
0 |
0 |
0 |
|
| 2262 |
0 |
| alpha[i] = 0; |
| 2263 |
|
| |
| 2264 |
0 |
▶ | for(i=0; i<w_size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<w_size; i++) |
0 |
0 |
0 |
|
| 2265 |
0 |
| w[i] = 0; |
| 2266 |
0 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
0 |
0 |
0 |
|
| 2267 |
|
| { |
| 2268 |
0 |
| feature_node * const xi = prob->x[i]; |
| 2269 |
0 |
| QD[i] = sparse_operator::nrm2_sq(xi); |
| 2270 |
0 |
| sparse_operator::axpy(alpha[i], xi, w); |
| 2271 |
|
| |
| 2272 |
0 |
| index[i] = i; |
| 2273 |
|
| } |
| 2274 |
|
| |
| 2275 |
0 |
▶ | while (iter < max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| while (iter < max_iter) |
0 |
0 |
0 |
|
| 2276 |
|
| { |
| 2277 |
0 |
| negGmax = -INF; |
| 2278 |
0 |
| negGmin = INF; |
| 2279 |
|
| |
| 2280 |
0 |
▶ | for (s=0; s<active_size; s++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (s=0; s<active_size; s++) |
0 |
0 |
0 |
|
| 2281 |
|
| { |
| 2282 |
0 |
| i = index[s]; |
| 2283 |
0 |
| feature_node * const xi = prob->x[i]; |
| 2284 |
0 |
| G[i] = sparse_operator::dot(w, xi); |
| 2285 |
0 |
▶ | if (alpha[i] < 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (alpha[i] < 1) |
0 |
0 |
0 |
|
| 2286 |
0 |
| negGmax = max(negGmax, -G[i]); |
| 2287 |
0 |
▶ | if (alpha[i] > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (alpha[i] > 0) |
0 |
0 |
0 |
|
| 2288 |
0 |
| negGmin = min(negGmin, -G[i]); |
| 2289 |
|
| } |
| 2290 |
|
| |
| 2291 |
0 |
▶ | if (negGmax - negGmin < eps) |
Branch: 1 branch
| Branch | True | False | Total |
| if (negGmax - negGmin < eps) |
0 |
0 |
0 |
|
| 2292 |
|
| { |
| 2293 |
0 |
▶ | if (active_size == l) |
Branch: 1 branch
| Branch | True | False | Total |
| if (active_size == l) |
0 |
0 |
0 |
|
| 2294 |
0 |
| break; |
| 2295 |
|
| else |
| 2296 |
|
| { |
| 2297 |
0 |
| active_size = l; |
| 2298 |
0 |
▶ | info("*"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("*"); |
0 |
0 |
0 |
|
| 2299 |
0 |
| continue; |
| 2300 |
|
| } |
| 2301 |
|
| } |
| 2302 |
|
| |
| 2303 |
0 |
▶ | for(s=0; s<active_size; s++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(s=0; s<active_size; s++) |
0 |
0 |
0 |
|
| 2304 |
|
| { |
| 2305 |
0 |
| i = index[s]; |
| 2306 |
0 |
▶ | if ((alpha[i] == 1 && -G[i] > negGmax) || |
Branch: 2 branches
| Branch | True | False | Total |
| if ((alpha[i] == 1 && -G[i] > negGmax) || |
0 |
0 |
0 |
| if ((alpha[i] == 1 && -G[i] > negGmax) || |
0 |
0 |
0 |
|
| 2307 |
0 |
▶ | (alpha[i] == 0 && -G[i] < negGmin)) |
Branch: 2 branches
| Branch | True | False | Total |
| (alpha[i] == 0 && -G[i] < negGmin)) |
0 |
0 |
0 |
| (alpha[i] == 0 && -G[i] < negGmin)) |
0 |
0 |
0 |
|
| 2308 |
|
| { |
| 2309 |
0 |
| active_size--; |
| 2310 |
0 |
| swap(index[s], index[active_size]); |
| 2311 |
0 |
| s--; |
| 2312 |
|
| } |
| 2313 |
|
| } |
| 2314 |
|
| |
| 2315 |
0 |
| max_inner_iter = max(active_size/10, 1); |
| 2316 |
0 |
| int len_Iup = 0; |
| 2317 |
0 |
| int len_Ilow = 0; |
| 2318 |
0 |
▶ | for(s=0; s<active_size; s++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(s=0; s<active_size; s++) |
0 |
0 |
0 |
|
| 2319 |
|
| { |
| 2320 |
0 |
| i = index[s]; |
| 2321 |
0 |
| node.index = i; |
| 2322 |
0 |
| node.value = -G[i]; |
| 2323 |
|
| |
| 2324 |
0 |
▶ | if (alpha[i] < 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (alpha[i] < 1) |
0 |
0 |
0 |
|
| 2325 |
|
| { |
| 2326 |
0 |
| max_negG_of_Iup[len_Iup] = node; |
| 2327 |
0 |
| len_Iup++; |
| 2328 |
|
| } |
| 2329 |
|
| |
| 2330 |
0 |
▶ | if (alpha[i] > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (alpha[i] > 0) |
0 |
0 |
0 |
|
| 2331 |
|
| { |
| 2332 |
0 |
| min_negG_of_Ilow[len_Ilow] = node; |
| 2333 |
0 |
| len_Ilow++; |
| 2334 |
|
| } |
| 2335 |
|
| } |
| 2336 |
0 |
| max_inner_iter = min(max_inner_iter, min(len_Iup, len_Ilow)); |
| 2337 |
|
| |
| 2338 |
0 |
▶ | quick_select_min_k(max_negG_of_Iup, 0, len_Iup-1, len_Iup-max_inner_iter); |
Branch: 1 branch
| Branch | True | False | Total |
| quick_select_min_k(max_negG_of_Iup, 0, len_Iup-1, len_Iup-max_inner_iter); |
0 |
0 |
0 |
|
| 2339 |
0 |
▶ | qsort(&(max_negG_of_Iup[len_Iup-max_inner_iter]), max_inner_iter, sizeof(struct feature_node), compare_feature_node); |
Branch: 1 branch
| Branch | True | False | Total |
| qsort(&(max_negG_of_Iup[len_Iup-max_inner_iter]), max_inner_iter, sizeof(struct feature_node), compare_feature_node); |
0 |
0 |
0 |
|
| 2340 |
|
| |
| 2341 |
0 |
▶ | quick_select_min_k(min_negG_of_Ilow, 0, len_Ilow-1, max_inner_iter); |
Branch: 1 branch
| Branch | True | False | Total |
| quick_select_min_k(min_negG_of_Ilow, 0, len_Ilow-1, max_inner_iter); |
0 |
0 |
0 |
|
| 2342 |
0 |
▶ | qsort(min_negG_of_Ilow, max_inner_iter, sizeof(struct feature_node), compare_feature_node); |
Branch: 1 branch
| Branch | True | False | Total |
| qsort(min_negG_of_Ilow, max_inner_iter, sizeof(struct feature_node), compare_feature_node); |
0 |
0 |
0 |
|
| 2343 |
|
| |
| 2344 |
0 |
▶ | for (s=0; s<max_inner_iter; s++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (s=0; s<max_inner_iter; s++) |
0 |
0 |
0 |
|
| 2345 |
|
| { |
| 2346 |
0 |
| i = max_negG_of_Iup[len_Iup-s-1].index; |
| 2347 |
0 |
| j = min_negG_of_Ilow[s].index; |
| 2348 |
|
| |
| 2349 |
0 |
▶ | if ((alpha[i] == 0 && alpha[j] == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| if ((alpha[i] == 0 && alpha[j] == 0) || |
0 |
0 |
0 |
| if ((alpha[i] == 0 && alpha[j] == 0) || |
0 |
0 |
0 |
|
| 2350 |
0 |
▶ | (alpha[i] == 1 && alpha[j] == 1)) |
Branch: 2 branches
| Branch | True | False | Total |
| (alpha[i] == 1 && alpha[j] == 1)) |
0 |
0 |
0 |
| (alpha[i] == 1 && alpha[j] == 1)) |
0 |
0 |
0 |
|
| 2351 |
0 |
| continue; |
| 2352 |
|
| |
| 2353 |
0 |
| feature_node const * xi = prob->x[i]; |
| 2354 |
0 |
| feature_node const * xj = prob->x[j]; |
| 2355 |
|
| |
| 2356 |
0 |
| Gi = sparse_operator::dot(w, xi); |
| 2357 |
0 |
| Gj = sparse_operator::dot(w, xj); |
| 2358 |
|
| |
| 2359 |
0 |
| int violating_pair = 0; |
| 2360 |
0 |
▶ | if (alpha[i] < 1 && alpha[j] > 0 && -Gj + 1e-12 < -Gi) |
Branch: 3 branches
| Branch | True | False | Total |
| if (alpha[i] < 1 && alpha[j] > 0 && -Gj + 1e-12 < -Gi) |
0 |
0 |
0 |
| if (alpha[i] < 1 && alpha[j] > 0 && -Gj + 1e-12 < -Gi) |
0 |
0 |
0 |
| if (alpha[i] < 1 && alpha[j] > 0 && -Gj + 1e-12 < -Gi) |
0 |
0 |
0 |
|
| 2361 |
0 |
| violating_pair = 1; |
| 2362 |
|
| else |
| 2363 |
0 |
▶ | if (alpha[i] > 0 && alpha[j] < 1 && -Gi + 1e-12 < -Gj) |
Branch: 3 branches
| Branch | True | False | Total |
| if (alpha[i] > 0 && alpha[j] < 1 && -Gi + 1e-12 < -Gj) |
0 |
0 |
0 |
| if (alpha[i] > 0 && alpha[j] < 1 && -Gi + 1e-12 < -Gj) |
0 |
0 |
0 |
| if (alpha[i] > 0 && alpha[j] < 1 && -Gi + 1e-12 < -Gj) |
0 |
0 |
0 |
|
| 2364 |
0 |
| violating_pair = 1; |
| 2365 |
0 |
▶ | if (violating_pair == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (violating_pair == 0) |
0 |
0 |
0 |
|
| 2366 |
0 |
| continue; |
| 2367 |
|
| |
| 2368 |
0 |
| Qij = sparse_operator::sparse_dot(xi, xj); |
| 2369 |
0 |
| quad_coef = QD[i] + QD[j] - 2*Qij; |
| 2370 |
0 |
▶ | if(quad_coef <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(quad_coef <= 0) |
0 |
0 |
0 |
|
| 2371 |
0 |
| quad_coef = 1e-12; |
| 2372 |
0 |
| delta = (Gi - Gj) / quad_coef; |
| 2373 |
0 |
| old_alpha_i = alpha[i]; |
| 2374 |
0 |
| sum = alpha[i] + alpha[j]; |
| 2375 |
0 |
| alpha[i] = alpha[i] - delta; |
| 2376 |
0 |
| alpha[j] = alpha[j] + delta; |
| 2377 |
0 |
▶ | if (sum > 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sum > 1) |
0 |
0 |
0 |
|
| 2378 |
|
| { |
| 2379 |
0 |
▶ | if (alpha[i] > 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (alpha[i] > 1) |
0 |
0 |
0 |
|
| 2380 |
|
| { |
| 2381 |
0 |
| alpha[i] = 1; |
| 2382 |
0 |
| alpha[j] = sum - 1; |
| 2383 |
|
| } |
| 2384 |
|
| } |
| 2385 |
|
| else |
| 2386 |
|
| { |
| 2387 |
0 |
▶ | if (alpha[j] < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (alpha[j] < 0) |
0 |
0 |
0 |
|
| 2388 |
|
| { |
| 2389 |
0 |
| alpha[j] = 0; |
| 2390 |
0 |
| alpha[i] = sum; |
| 2391 |
|
| } |
| 2392 |
|
| } |
| 2393 |
0 |
▶ | if (sum > 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sum > 1) |
0 |
0 |
0 |
|
| 2394 |
|
| { |
| 2395 |
0 |
▶ | if (alpha[j] > 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (alpha[j] > 1) |
0 |
0 |
0 |
|
| 2396 |
|
| { |
| 2397 |
0 |
| alpha[j] = 1; |
| 2398 |
0 |
| alpha[i] = sum - 1; |
| 2399 |
|
| } |
| 2400 |
|
| } |
| 2401 |
|
| else |
| 2402 |
|
| { |
| 2403 |
0 |
▶ | if (alpha[i] < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (alpha[i] < 0) |
0 |
0 |
0 |
|
| 2404 |
|
| { |
| 2405 |
0 |
| alpha[i] = 0; |
| 2406 |
0 |
| alpha[j] = sum; |
| 2407 |
|
| } |
| 2408 |
|
| } |
| 2409 |
0 |
| delta = alpha[i] - old_alpha_i; |
| 2410 |
0 |
| sparse_operator::axpy(delta, xi, w); |
| 2411 |
0 |
| sparse_operator::axpy(-delta, xj, w); |
| 2412 |
|
| } |
| 2413 |
0 |
| iter++; |
| 2414 |
0 |
▶ | if (iter % 10 == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (iter % 10 == 0) |
0 |
0 |
0 |
|
| 2415 |
0 |
▶ | info("."); |
Branch: 1 branch
| Branch | True | False | Total |
| info("."); |
0 |
0 |
0 |
|
| 2416 |
|
| } |
| 2417 |
0 |
▶ | info("\noptimization finished, #iter = %d\n",iter); |
Branch: 1 branch
| Branch | True | False | Total |
| info("\noptimization finished, #iter = %d\n",iter); |
0 |
0 |
0 |
|
| 2418 |
0 |
▶ | if (iter >= max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| if (iter >= max_iter) |
0 |
0 |
0 |
|
| 2419 |
0 |
▶ | info("\nWARNING: reaching max number of iterations\n\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("\nWARNING: reaching max number of iterations\n\n"); |
0 |
0 |
0 |
|
| 2420 |
|
| |
| 2421 |
|
| // calculate object value |
| 2422 |
0 |
| double v = 0; |
| 2423 |
0 |
▶ | for(i=0; i<w_size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<w_size; i++) |
0 |
0 |
0 |
|
| 2424 |
0 |
| v += w[i]*w[i]; |
| 2425 |
0 |
| int nSV = 0; |
| 2426 |
0 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
0 |
0 |
0 |
|
| 2427 |
|
| { |
| 2428 |
0 |
▶ | if (alpha[i] > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (alpha[i] > 0) |
0 |
0 |
0 |
|
| 2429 |
0 |
| ++nSV; |
| 2430 |
|
| } |
| 2431 |
0 |
▶ | info("Objective value = %lf\n", v/2); |
Branch: 1 branch
| Branch | True | False | Total |
| info("Objective value = %lf\n", v/2); |
0 |
0 |
0 |
|
| 2432 |
0 |
▶ | info("nSV = %d\n", nSV); |
Branch: 1 branch
| Branch | True | False | Total |
| info("nSV = %d\n", nSV); |
0 |
0 |
0 |
|
| 2433 |
|
| |
| 2434 |
|
| // calculate rho |
| 2435 |
0 |
| double nr_free = 0; |
| 2436 |
0 |
| double ub = INF, lb = -INF, sum_free = 0; |
| 2437 |
0 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
0 |
0 |
0 |
|
| 2438 |
|
| { |
| 2439 |
0 |
| double G = sparse_operator::dot(w, prob->x[i]); |
| 2440 |
0 |
▶ | if (alpha[i] == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (alpha[i] == 1) |
0 |
0 |
0 |
|
| 2441 |
0 |
| lb = max(lb, G); |
| 2442 |
0 |
▶ | else if (alpha[i] == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (alpha[i] == 0) |
0 |
0 |
0 |
|
| 2443 |
0 |
| ub = min(ub, G); |
| 2444 |
|
| else |
| 2445 |
|
| { |
| 2446 |
0 |
| ++nr_free; |
| 2447 |
0 |
| sum_free += G; |
| 2448 |
|
| } |
| 2449 |
|
| } |
| 2450 |
|
| |
| 2451 |
0 |
▶ | if (nr_free > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nr_free > 0) |
0 |
0 |
0 |
|
| 2452 |
0 |
| *rho = sum_free/nr_free; |
| 2453 |
|
| else |
| 2454 |
0 |
| *rho = (ub + lb)/2; |
| 2455 |
0 |
▶ | info("rho = %lf\n", *rho); |
Branch: 1 branch
| Branch | True | False | Total |
| info("rho = %lf\n", *rho); |
0 |
0 |
0 |
|
| 2456 |
|
| |
| 2457 |
0 |
▶ | delete [] QD; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] QD; |
0 |
0 |
0 |
|
| 2458 |
0 |
▶ | delete [] G; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] G; |
0 |
0 |
0 |
|
| 2459 |
0 |
▶ | delete [] index; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] index; |
0 |
0 |
0 |
|
| 2460 |
0 |
▶ | delete [] alpha; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] alpha; |
0 |
0 |
0 |
|
| 2461 |
0 |
▶ | delete [] max_negG_of_Iup; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] max_negG_of_Iup; |
0 |
0 |
0 |
|
| 2462 |
0 |
▶ | delete [] min_negG_of_Ilow; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] min_negG_of_Ilow; |
0 |
0 |
0 |
|
| 2463 |
|
| |
| 2464 |
0 |
| return iter; |
| 2465 |
|
| } |
| 2466 |
|
| |
| 2467 |
|
| // transpose matrix X from row format to column format |
| 2468 |
0 |
| static void transpose(const problem *prob, feature_node **x_space_ret, problem *prob_col) |
| 2469 |
|
| { |
| 2470 |
|
| int i; |
| 2471 |
0 |
| int l = prob->l; |
| 2472 |
0 |
| int n = prob->n; |
| 2473 |
0 |
| size_t nnz = 0; |
| 2474 |
0 |
▶ | size_t *col_ptr = new size_t [n+1]; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t *col_ptr = new size_t [n+1]; |
0 |
0 |
0 |
|
| 2475 |
|
| feature_node *x_space; |
| 2476 |
0 |
| prob_col->l = l; |
| 2477 |
0 |
| prob_col->n = n; |
| 2478 |
0 |
▶ | prob_col->y = new double[l]; |
Branch: 1 branch
| Branch | True | False | Total |
| prob_col->y = new double[l]; |
0 |
0 |
0 |
|
| 2479 |
0 |
▶ | prob_col->x = new feature_node*[n]; |
Branch: 1 branch
| Branch | True | False | Total |
| prob_col->x = new feature_node*[n]; |
0 |
0 |
0 |
|
| 2480 |
|
| |
| 2481 |
0 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
0 |
0 |
0 |
|
| 2482 |
0 |
| prob_col->y[i] = prob->y[i]; |
| 2483 |
|
| |
| 2484 |
0 |
▶ | for(i=0; i<n+1; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<n+1; i++) |
0 |
0 |
0 |
|
| 2485 |
0 |
| col_ptr[i] = 0; |
| 2486 |
0 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
0 |
0 |
0 |
|
| 2487 |
|
| { |
| 2488 |
0 |
| feature_node *x = prob->x[i]; |
| 2489 |
0 |
▶ | while(x->index != -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(x->index != -1) |
0 |
0 |
0 |
|
| 2490 |
|
| { |
| 2491 |
0 |
| nnz++; |
| 2492 |
0 |
| col_ptr[x->index]++; |
| 2493 |
0 |
| x++; |
| 2494 |
|
| } |
| 2495 |
|
| } |
| 2496 |
0 |
▶ | for(i=1; i<n+1; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=1; i<n+1; i++) |
0 |
0 |
0 |
|
| 2497 |
0 |
| col_ptr[i] += col_ptr[i-1] + 1; |
| 2498 |
|
| |
| 2499 |
0 |
▶ | x_space = new feature_node[nnz+n]; |
Branch: 1 branch
| Branch | True | False | Total |
| x_space = new feature_node[nnz+n]; |
0 |
0 |
0 |
|
| 2500 |
0 |
▶ | for(i=0; i<n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<n; i++) |
0 |
0 |
0 |
|
| 2501 |
0 |
| prob_col->x[i] = &x_space[col_ptr[i]]; |
| 2502 |
|
| |
| 2503 |
0 |
▶ | for(i=0; i<l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<l; i++) |
0 |
0 |
0 |
|
| 2504 |
|
| { |
| 2505 |
0 |
| feature_node *x = prob->x[i]; |
| 2506 |
0 |
▶ | while(x->index != -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(x->index != -1) |
0 |
0 |
0 |
|
| 2507 |
|
| { |
| 2508 |
0 |
| int ind = x->index-1; |
| 2509 |
0 |
| x_space[col_ptr[ind]].index = i+1; // starts from 1 |
| 2510 |
0 |
| x_space[col_ptr[ind]].value = x->value; |
| 2511 |
0 |
| col_ptr[ind]++; |
| 2512 |
0 |
| x++; |
| 2513 |
|
| } |
| 2514 |
|
| } |
| 2515 |
0 |
▶ | for(i=0; i<n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<n; i++) |
0 |
0 |
0 |
|
| 2516 |
0 |
| x_space[col_ptr[i]].index = -1; |
| 2517 |
|
| |
| 2518 |
0 |
| *x_space_ret = x_space; |
| 2519 |
|
| |
| 2520 |
0 |
▶ | delete [] col_ptr; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] col_ptr; |
0 |
0 |
0 |
|
| 2521 |
0 |
| } |
| 2522 |
|
| |
| 2523 |
|
| // label: label name, start: begin of each class, count: #data of classes, perm: indices to the original data |
| 2524 |
|
| // perm, length l, must be allocated before calling this subroutine |
| 2525 |
78 |
| static void group_classes(const problem *prob, int *nr_class_ret, int **label_ret, int **start_ret, int **count_ret, int *perm) |
| 2526 |
|
| { |
| 2527 |
78 |
| int l = prob->l; |
| 2528 |
78 |
| int max_nr_class = 16; |
| 2529 |
78 |
| int nr_class = 0; |
| 2530 |
78 |
| int *label = Malloc(int,max_nr_class); |
| 2531 |
78 |
| int *count = Malloc(int,max_nr_class); |
| 2532 |
78 |
| int *data_label = Malloc(int,l); |
| 2533 |
|
| int i; |
| 2534 |
|
| |
| 2535 |
17088 |
▶ | for(i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) |
17010 |
78 |
17088 |
|
| 2536 |
|
| { |
| 2537 |
17010 |
| int this_label = (int)prob->y[i]; |
| 2538 |
|
| int j; |
| 2539 |
24828 |
▶ | for(j=0;j<nr_class;j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0;j<nr_class;j++) |
24672 |
156 |
24828 |
|
| 2540 |
|
| { |
| 2541 |
24672 |
▶ | if(this_label == label[j]) |
Branch: 1 branch
| Branch | True | False | Total |
| if(this_label == label[j]) |
16854 |
7818 |
24672 |
|
| 2542 |
|
| { |
| 2543 |
16854 |
| ++count[j]; |
| 2544 |
16854 |
| break; |
| 2545 |
|
| } |
| 2546 |
|
| } |
| 2547 |
17010 |
| data_label[i] = j; |
| 2548 |
17010 |
▶ | if(j == nr_class) |
Branch: 1 branch
| Branch | True | False | Total |
| if(j == nr_class) |
156 |
16854 |
17010 |
|
| 2549 |
|
| { |
| 2550 |
156 |
▶ | if(nr_class == max_nr_class) |
Branch: 1 branch
| Branch | True | False | Total |
| if(nr_class == max_nr_class) |
0 |
156 |
156 |
|
| 2551 |
|
| { |
| 2552 |
0 |
| max_nr_class *= 2; |
| 2553 |
0 |
| label = (int *)realloc(label,max_nr_class*sizeof(int)); |
| 2554 |
0 |
| count = (int *)realloc(count,max_nr_class*sizeof(int)); |
| 2555 |
|
| } |
| 2556 |
156 |
| label[nr_class] = this_label; |
| 2557 |
156 |
| count[nr_class] = 1; |
| 2558 |
156 |
| ++nr_class; |
| 2559 |
|
| } |
| 2560 |
|
| } |
| 2561 |
|
| |
| 2562 |
|
| // |
| 2563 |
|
| // Labels are ordered by their first occurrence in the training set. |
| 2564 |
|
| // However, for two-class sets with -1/+1 labels and -1 appears first, |
| 2565 |
|
| // we swap labels to ensure that internally the binary SVM has positive data corresponding to the +1 instances. |
| 2566 |
|
| // |
| 2567 |
78 |
▶ | if (nr_class == 2 && label[0] == -1 && label[1] == 1) |
Branch: 3 branches
| Branch | True | False | Total |
| if (nr_class == 2 && label[0] == -1 && label[1] == 1) |
78 |
0 |
78 |
| if (nr_class == 2 && label[0] == -1 && label[1] == 1) |
69 |
9 |
78 |
| if (nr_class == 2 && label[0] == -1 && label[1] == 1) |
69 |
0 |
69 |
|
| 2568 |
|
| { |
| 2569 |
69 |
| swap(label[0],label[1]); |
| 2570 |
69 |
| swap(count[0],count[1]); |
| 2571 |
14973 |
▶ | for(i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) |
14904 |
69 |
14973 |
|
| 2572 |
|
| { |
| 2573 |
14904 |
▶ | if(data_label[i] == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(data_label[i] == 0) |
8268 |
6636 |
14904 |
|
| 2574 |
8268 |
| data_label[i] = 1; |
| 2575 |
|
| else |
| 2576 |
6636 |
| data_label[i] = 0; |
| 2577 |
|
| } |
| 2578 |
|
| } |
| 2579 |
|
| |
| 2580 |
78 |
| int *start = Malloc(int,nr_class); |
| 2581 |
78 |
| start[0] = 0; |
| 2582 |
156 |
▶ | for(i=1;i<nr_class;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=1;i<nr_class;i++) |
78 |
78 |
156 |
|
| 2583 |
78 |
| start[i] = start[i-1]+count[i-1]; |
| 2584 |
17088 |
▶ | for(i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) |
17010 |
78 |
17088 |
|
| 2585 |
|
| { |
| 2586 |
17010 |
| perm[start[data_label[i]]] = i; |
| 2587 |
17010 |
| ++start[data_label[i]]; |
| 2588 |
|
| } |
| 2589 |
78 |
| start[0] = 0; |
| 2590 |
156 |
▶ | for(i=1;i<nr_class;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=1;i<nr_class;i++) |
78 |
78 |
156 |
|
| 2591 |
78 |
| start[i] = start[i-1]+count[i-1]; |
| 2592 |
|
| |
| 2593 |
78 |
| *nr_class_ret = nr_class; |
| 2594 |
78 |
| *label_ret = label; |
| 2595 |
78 |
| *start_ret = start; |
| 2596 |
78 |
| *count_ret = count; |
| 2597 |
78 |
| free(data_label); |
| 2598 |
78 |
| } |
| 2599 |
|
| |
| 2600 |
78 |
| static void train_one(const problem *prob, const parameter *param, double *w, double Cp, double Cn) |
| 2601 |
|
| { |
| 2602 |
78 |
| int solver_type = param->solver_type; |
| 2603 |
78 |
| int dual_solver_max_iter = 300; |
| 2604 |
|
| int iter; |
| 2605 |
|
| |
| 2606 |
78 |
▶ | bool is_regression = (solver_type==L2R_L2LOSS_SVR || |
Branch: 1 branch
| Branch | True | False | Total |
| bool is_regression = (solver_type==L2R_L2LOSS_SVR || |
78 |
0 |
78 |
|
| 2607 |
156 |
▶ | solver_type==L2R_L1LOSS_SVR_DUAL || |
Branch: 2 branches
| Branch | True | False | Total |
| solver_type==L2R_L1LOSS_SVR_DUAL || |
78 |
0 |
78 |
| solver_type==L2R_L1LOSS_SVR_DUAL || |
0 |
78 |
78 |
|
| 2608 |
|
| solver_type==L2R_L2LOSS_SVR_DUAL); |
| 2609 |
|
| |
| 2610 |
|
| // Some solvers use Cp,Cn but not C array; extensions possible but no plan for now |
| 2611 |
78 |
▶ | double *C = new double[prob->l]; |
Branch: 1 branch
| Branch | True | False | Total |
| double *C = new double[prob->l]; |
78 |
0 |
78 |
|
| 2612 |
78 |
| double primal_solver_tol = param->eps; |
| 2613 |
78 |
▶ | if(is_regression) |
Branch: 1 branch
| Branch | True | False | Total |
| if(is_regression) |
0 |
78 |
78 |
|
| 2614 |
|
| { |
| 2615 |
0 |
▶ | for(int i=0;i<prob->l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i=0;i<prob->l;i++) |
0 |
0 |
0 |
|
| 2616 |
0 |
| C[i] = param->C; |
| 2617 |
|
| } |
| 2618 |
|
| else |
| 2619 |
|
| { |
| 2620 |
78 |
| int pos = 0; |
| 2621 |
17088 |
▶ | for(int i=0;i<prob->l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i=0;i<prob->l;i++) |
17010 |
78 |
17088 |
|
| 2622 |
|
| { |
| 2623 |
17010 |
▶ | if(prob->y[i] > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(prob->y[i] > 0) |
7560 |
9450 |
17010 |
|
| 2624 |
|
| { |
| 2625 |
7560 |
| pos++; |
| 2626 |
7560 |
| C[i] = Cp; |
| 2627 |
|
| } |
| 2628 |
|
| else |
| 2629 |
9450 |
| C[i] = Cn; |
| 2630 |
|
| } |
| 2631 |
78 |
| int neg = prob->l - pos; |
| 2632 |
78 |
| primal_solver_tol = param->eps*max(min(pos,neg), 1)/prob->l; |
| 2633 |
|
| } |
| 2634 |
|
| |
| 2635 |
78 |
| switch(solver_type) |
| 2636 |
|
| { |
| 2637 |
0 |
| case L2R_LR: |
| 2638 |
|
| { |
| 2639 |
0 |
▶ | l2r_lr_fun fun_obj(prob, param, C); |
Branch: 1 branch
| Branch | True | False | Total |
| l2r_lr_fun fun_obj(prob, param, C); |
0 |
0 |
0 |
|
| 2640 |
0 |
▶ | NEWTON newton_obj(&fun_obj, primal_solver_tol); |
Branch: 1 branch
| Branch | True | False | Total |
| NEWTON newton_obj(&fun_obj, primal_solver_tol); |
0 |
0 |
0 |
|
| 2641 |
0 |
▶ | newton_obj.set_print_string(liblinear_print_string); |
Branch: 1 branch
| Branch | True | False | Total |
| newton_obj.set_print_string(liblinear_print_string); |
0 |
0 |
0 |
|
| 2642 |
0 |
▶ | newton_obj.newton(w); |
Branch: 1 branch
| Branch | True | False | Total |
| newton_obj.newton(w); |
0 |
0 |
0 |
|
| 2643 |
0 |
| break; |
| 2644 |
0 |
| } |
| 2645 |
76 |
| case L2R_L2LOSS_SVC: |
| 2646 |
|
| { |
| 2647 |
76 |
▶ | l2r_l2_svc_fun fun_obj(prob, param, C); |
Branch: 1 branch
| Branch | True | False | Total |
| l2r_l2_svc_fun fun_obj(prob, param, C); |
76 |
0 |
76 |
|
| 2648 |
76 |
▶ | NEWTON newton_obj(&fun_obj, primal_solver_tol); |
Branch: 1 branch
| Branch | True | False | Total |
| NEWTON newton_obj(&fun_obj, primal_solver_tol); |
76 |
0 |
76 |
|
| 2649 |
76 |
▶ | newton_obj.set_print_string(liblinear_print_string); |
Branch: 1 branch
| Branch | True | False | Total |
| newton_obj.set_print_string(liblinear_print_string); |
76 |
0 |
76 |
|
| 2650 |
76 |
▶ | newton_obj.newton(w); |
Branch: 1 branch
| Branch | True | False | Total |
| newton_obj.newton(w); |
76 |
0 |
76 |
|
| 2651 |
76 |
| break; |
| 2652 |
76 |
| } |
| 2653 |
2 |
| case L2R_L2LOSS_SVC_DUAL: |
| 2654 |
|
| { |
| 2655 |
2 |
| iter = solve_l2r_l1l2_svc(prob, param, w, Cp, Cn, dual_solver_max_iter); |
| 2656 |
2 |
▶ | if(iter >= dual_solver_max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| if(iter >= dual_solver_max_iter) |
0 |
2 |
2 |
|
| 2657 |
|
| { |
| 2658 |
0 |
▶ | info("\nWARNING: reaching max number of iterations\nSwitching to use -s 2\n\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("\nWARNING: reaching max number of iterations\nSwitching to use -s 2\n\n"); |
0 |
0 |
0 |
|
| 2659 |
|
| // primal_solver_tol obtained from eps for dual may be too loose |
| 2660 |
0 |
| primal_solver_tol *= 0.1; |
| 2661 |
0 |
▶ | l2r_l2_svc_fun fun_obj(prob, param, C); |
Branch: 1 branch
| Branch | True | False | Total |
| l2r_l2_svc_fun fun_obj(prob, param, C); |
0 |
0 |
0 |
|
| 2662 |
0 |
▶ | NEWTON newton_obj(&fun_obj, primal_solver_tol); |
Branch: 1 branch
| Branch | True | False | Total |
| NEWTON newton_obj(&fun_obj, primal_solver_tol); |
0 |
0 |
0 |
|
| 2663 |
0 |
▶ | newton_obj.set_print_string(liblinear_print_string); |
Branch: 1 branch
| Branch | True | False | Total |
| newton_obj.set_print_string(liblinear_print_string); |
0 |
0 |
0 |
|
| 2664 |
0 |
▶ | newton_obj.newton(w); |
Branch: 1 branch
| Branch | True | False | Total |
| newton_obj.newton(w); |
0 |
0 |
0 |
|
| 2665 |
0 |
| } |
| 2666 |
2 |
| break; |
| 2667 |
|
| } |
| 2668 |
0 |
| case L2R_L1LOSS_SVC_DUAL: |
| 2669 |
|
| { |
| 2670 |
0 |
| iter = solve_l2r_l1l2_svc(prob, param, w, Cp, Cn, dual_solver_max_iter); |
| 2671 |
0 |
▶ | if(iter >= dual_solver_max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| if(iter >= dual_solver_max_iter) |
0 |
0 |
0 |
|
| 2672 |
0 |
| info("\nWARNING: reaching max number of iterations\nUsing -s 2 may be faster (also see FAQ)\n\n"); |
| 2673 |
0 |
| break; |
| 2674 |
|
| } |
| 2675 |
0 |
| case L1R_L2LOSS_SVC: |
| 2676 |
|
| { |
| 2677 |
|
| problem prob_col; |
| 2678 |
0 |
| feature_node *x_space = NULL; |
| 2679 |
0 |
▶ | transpose(prob, &x_space ,&prob_col); |
Branch: 1 branch
| Branch | True | False | Total |
| transpose(prob, &x_space ,&prob_col); |
0 |
0 |
0 |
|
| 2680 |
0 |
▶ | solve_l1r_l2_svc(&prob_col, param, w, Cp, Cn, primal_solver_tol); |
Branch: 1 branch
| Branch | True | False | Total |
| solve_l1r_l2_svc(&prob_col, param, w, Cp, Cn, primal_solver_tol); |
0 |
0 |
0 |
|
| 2681 |
0 |
▶ | delete [] prob_col.y; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] prob_col.y; |
0 |
0 |
0 |
|
| 2682 |
0 |
▶ | delete [] prob_col.x; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] prob_col.x; |
0 |
0 |
0 |
|
| 2683 |
0 |
▶ | delete [] x_space; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] x_space; |
0 |
0 |
0 |
|
| 2684 |
0 |
| break; |
| 2685 |
|
| } |
| 2686 |
0 |
| case L1R_LR: |
| 2687 |
|
| { |
| 2688 |
|
| problem prob_col; |
| 2689 |
0 |
| feature_node *x_space = NULL; |
| 2690 |
0 |
▶ | transpose(prob, &x_space ,&prob_col); |
Branch: 1 branch
| Branch | True | False | Total |
| transpose(prob, &x_space ,&prob_col); |
0 |
0 |
0 |
|
| 2691 |
0 |
▶ | solve_l1r_lr(&prob_col, param, w, Cp, Cn, primal_solver_tol); |
Branch: 1 branch
| Branch | True | False | Total |
| solve_l1r_lr(&prob_col, param, w, Cp, Cn, primal_solver_tol); |
0 |
0 |
0 |
|
| 2692 |
0 |
▶ | delete [] prob_col.y; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] prob_col.y; |
0 |
0 |
0 |
|
| 2693 |
0 |
▶ | delete [] prob_col.x; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] prob_col.x; |
0 |
0 |
0 |
|
| 2694 |
0 |
▶ | delete [] x_space; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] x_space; |
0 |
0 |
0 |
|
| 2695 |
0 |
| break; |
| 2696 |
|
| } |
| 2697 |
0 |
| case L2R_LR_DUAL: |
| 2698 |
|
| { |
| 2699 |
0 |
| iter = solve_l2r_lr_dual(prob, param, w, Cp, Cn, dual_solver_max_iter); |
| 2700 |
0 |
▶ | if(iter >= dual_solver_max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| if(iter >= dual_solver_max_iter) |
0 |
0 |
0 |
|
| 2701 |
|
| { |
| 2702 |
0 |
▶ | info("\nWARNING: reaching max number of iterations\nSwitching to use -s 0\n\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("\nWARNING: reaching max number of iterations\nSwitching to use -s 0\n\n"); |
0 |
0 |
0 |
|
| 2703 |
|
| // primal_solver_tol obtained from eps for dual may be too loose |
| 2704 |
0 |
| primal_solver_tol *= 0.1; |
| 2705 |
0 |
▶ | l2r_lr_fun fun_obj(prob, param, C); |
Branch: 1 branch
| Branch | True | False | Total |
| l2r_lr_fun fun_obj(prob, param, C); |
0 |
0 |
0 |
|
| 2706 |
0 |
▶ | NEWTON newton_obj(&fun_obj, primal_solver_tol); |
Branch: 1 branch
| Branch | True | False | Total |
| NEWTON newton_obj(&fun_obj, primal_solver_tol); |
0 |
0 |
0 |
|
| 2707 |
0 |
▶ | newton_obj.set_print_string(liblinear_print_string); |
Branch: 1 branch
| Branch | True | False | Total |
| newton_obj.set_print_string(liblinear_print_string); |
0 |
0 |
0 |
|
| 2708 |
0 |
▶ | newton_obj.newton(w); |
Branch: 1 branch
| Branch | True | False | Total |
| newton_obj.newton(w); |
0 |
0 |
0 |
|
| 2709 |
0 |
| } |
| 2710 |
0 |
| break; |
| 2711 |
|
| } |
| 2712 |
0 |
| case L2R_L2LOSS_SVR: |
| 2713 |
|
| { |
| 2714 |
0 |
▶ | l2r_l2_svr_fun fun_obj(prob, param, C); |
Branch: 1 branch
| Branch | True | False | Total |
| l2r_l2_svr_fun fun_obj(prob, param, C); |
0 |
0 |
0 |
|
| 2715 |
0 |
▶ | NEWTON newton_obj(&fun_obj, primal_solver_tol); |
Branch: 1 branch
| Branch | True | False | Total |
| NEWTON newton_obj(&fun_obj, primal_solver_tol); |
0 |
0 |
0 |
|
| 2716 |
0 |
▶ | newton_obj.set_print_string(liblinear_print_string); |
Branch: 1 branch
| Branch | True | False | Total |
| newton_obj.set_print_string(liblinear_print_string); |
0 |
0 |
0 |
|
| 2717 |
0 |
▶ | newton_obj.newton(w); |
Branch: 1 branch
| Branch | True | False | Total |
| newton_obj.newton(w); |
0 |
0 |
0 |
|
| 2718 |
0 |
| break; |
| 2719 |
0 |
| } |
| 2720 |
0 |
| case L2R_L1LOSS_SVR_DUAL: |
| 2721 |
|
| { |
| 2722 |
0 |
| iter = solve_l2r_l1l2_svr(prob, param, w, dual_solver_max_iter); |
| 2723 |
0 |
▶ | if(iter >= dual_solver_max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| if(iter >= dual_solver_max_iter) |
0 |
0 |
0 |
|
| 2724 |
0 |
| info("\nWARNING: reaching max number of iterations\nUsing -s 11 may be faster (also see FAQ)\n\n"); |
| 2725 |
|
| |
| 2726 |
0 |
| break; |
| 2727 |
|
| } |
| 2728 |
0 |
| case L2R_L2LOSS_SVR_DUAL: |
| 2729 |
|
| { |
| 2730 |
0 |
| iter = solve_l2r_l1l2_svr(prob, param, w, dual_solver_max_iter); |
| 2731 |
0 |
▶ | if(iter >= dual_solver_max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| if(iter >= dual_solver_max_iter) |
0 |
0 |
0 |
|
| 2732 |
|
| { |
| 2733 |
0 |
▶ | info("\nWARNING: reaching max number of iterations\nSwitching to use -s 11\n\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("\nWARNING: reaching max number of iterations\nSwitching to use -s 11\n\n"); |
0 |
0 |
0 |
|
| 2734 |
|
| // primal_solver_tol obtained from eps for dual may be too loose |
| 2735 |
0 |
| primal_solver_tol *= 0.001; |
| 2736 |
0 |
▶ | l2r_l2_svr_fun fun_obj(prob, param, C); |
Branch: 1 branch
| Branch | True | False | Total |
| l2r_l2_svr_fun fun_obj(prob, param, C); |
0 |
0 |
0 |
|
| 2737 |
0 |
▶ | NEWTON newton_obj(&fun_obj, primal_solver_tol); |
Branch: 1 branch
| Branch | True | False | Total |
| NEWTON newton_obj(&fun_obj, primal_solver_tol); |
0 |
0 |
0 |
|
| 2738 |
0 |
▶ | newton_obj.set_print_string(liblinear_print_string); |
Branch: 1 branch
| Branch | True | False | Total |
| newton_obj.set_print_string(liblinear_print_string); |
0 |
0 |
0 |
|
| 2739 |
0 |
▶ | newton_obj.newton(w); |
Branch: 1 branch
| Branch | True | False | Total |
| newton_obj.newton(w); |
0 |
0 |
0 |
|
| 2740 |
0 |
| } |
| 2741 |
0 |
| break; |
| 2742 |
|
| } |
| 2743 |
0 |
| default: |
| 2744 |
0 |
| fprintf(stderr, "ERROR: unknown solver_type\n"); |
| 2745 |
0 |
| break; |
| 2746 |
|
| } |
| 2747 |
|
| |
| 2748 |
78 |
▶ | delete[] C; |
Branch: 1 branch
| Branch | True | False | Total |
| delete[] C; |
78 |
0 |
78 |
|
| 2749 |
78 |
| } |
| 2750 |
|
| |
| 2751 |
|
| // Calculate the initial C for parameter selection |
| 2752 |
0 |
| static double calc_start_C(const problem *prob, const parameter *param) |
| 2753 |
|
| { |
| 2754 |
|
| int i; |
| 2755 |
|
| double xTx, max_xTx; |
| 2756 |
0 |
| max_xTx = 0; |
| 2757 |
0 |
▶ | for(i=0; i<prob->l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<prob->l; i++) |
0 |
0 |
0 |
|
| 2758 |
|
| { |
| 2759 |
0 |
| xTx = 0; |
| 2760 |
0 |
| feature_node *xi=prob->x[i]; |
| 2761 |
0 |
▶ | while(xi->index != -1) |
Branch: 1 branch
| Branch | True | False | Total |
| while(xi->index != -1) |
0 |
0 |
0 |
|
| 2762 |
|
| { |
| 2763 |
0 |
| double val = xi->value; |
| 2764 |
0 |
| xTx += val*val; |
| 2765 |
0 |
| xi++; |
| 2766 |
|
| } |
| 2767 |
0 |
▶ | if(xTx > max_xTx) |
Branch: 1 branch
| Branch | True | False | Total |
| if(xTx > max_xTx) |
0 |
0 |
0 |
|
| 2768 |
0 |
| max_xTx = xTx; |
| 2769 |
|
| } |
| 2770 |
|
| |
| 2771 |
0 |
| double min_C = 1.0; |
| 2772 |
0 |
▶ | if(param->solver_type == L2R_LR) |
Branch: 1 branch
| Branch | True | False | Total |
| if(param->solver_type == L2R_LR) |
0 |
0 |
0 |
|
| 2773 |
0 |
| min_C = 1.0 / (prob->l * max_xTx); |
| 2774 |
0 |
▶ | else if(param->solver_type == L2R_L2LOSS_SVC) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(param->solver_type == L2R_L2LOSS_SVC) |
0 |
0 |
0 |
|
| 2775 |
0 |
| min_C = 1.0 / (2 * prob->l * max_xTx); |
| 2776 |
0 |
▶ | else if(param->solver_type == L2R_L2LOSS_SVR) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(param->solver_type == L2R_L2LOSS_SVR) |
0 |
0 |
0 |
|
| 2777 |
|
| { |
| 2778 |
|
| double sum_y, loss, y_abs; |
| 2779 |
0 |
| double delta2 = 0.1; |
| 2780 |
0 |
| sum_y = 0, loss = 0; |
| 2781 |
0 |
▶ | for(i=0; i<prob->l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<prob->l; i++) |
0 |
0 |
0 |
|
| 2782 |
|
| { |
| 2783 |
0 |
| y_abs = fabs(prob->y[i]); |
| 2784 |
0 |
| sum_y += y_abs; |
| 2785 |
0 |
| loss += max(y_abs - param->p, 0.0) * max(y_abs - param->p, 0.0); |
| 2786 |
|
| } |
| 2787 |
0 |
▶ | if(loss > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(loss > 0) |
0 |
0 |
0 |
|
| 2788 |
0 |
| min_C = delta2 * delta2 * loss / (8 * sum_y * sum_y * max_xTx); |
| 2789 |
|
| else |
| 2790 |
0 |
| min_C = INF; |
| 2791 |
|
| } |
| 2792 |
|
| |
| 2793 |
0 |
| return pow( 2, floor(log(min_C) / log(2.0)) ); |
| 2794 |
|
| } |
| 2795 |
|
| |
| 2796 |
0 |
| static double calc_max_p(const problem *prob) |
| 2797 |
|
| { |
| 2798 |
|
| int i; |
| 2799 |
0 |
| double max_p = 0.0; |
| 2800 |
0 |
▶ | for(i = 0; i < prob->l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < prob->l; i++) |
0 |
0 |
0 |
|
| 2801 |
0 |
| max_p = max(max_p, fabs(prob->y[i])); |
| 2802 |
|
| |
| 2803 |
0 |
| return max_p; |
| 2804 |
|
| } |
| 2805 |
|
| |
| 2806 |
2 |
| static void find_parameter_C(const problem *prob, parameter *param_tmp, double start_C, double max_C, double *best_C, double *best_score, const int *fold_start, const int *perm, const problem *subprob, int nr_fold) |
| 2807 |
|
| { |
| 2808 |
|
| // variables for CV |
| 2809 |
|
| int i; |
| 2810 |
2 |
| double *target = Malloc(double, prob->l); |
| 2811 |
|
| |
| 2812 |
|
| // variables for warm start |
| 2813 |
2 |
| double ratio = 2; |
| 2814 |
2 |
| double **prev_w = Malloc(double*, nr_fold); |
| 2815 |
12 |
▶ | for(i = 0; i < nr_fold; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < nr_fold; i++) |
10 |
2 |
12 |
|
| 2816 |
10 |
| prev_w[i] = NULL; |
| 2817 |
2 |
| int num_unchanged_w = 0; |
| 2818 |
2 |
| void (*default_print_string) (const char *) = liblinear_print_string; |
| 2819 |
|
| |
| 2820 |
2 |
▶ | if(param_tmp->solver_type == L2R_LR || param_tmp->solver_type == L2R_L2LOSS_SVC) |
Branch: 2 branches
| Branch | True | False | Total |
| if(param_tmp->solver_type == L2R_LR || param_tmp->solver_type == L2R_L2LOSS_SVC) |
2 |
0 |
2 |
| if(param_tmp->solver_type == L2R_LR || param_tmp->solver_type == L2R_L2LOSS_SVC) |
2 |
0 |
2 |
|
| 2821 |
2 |
| *best_score = 0.0; |
| 2822 |
0 |
▶ | else if(param_tmp->solver_type == L2R_L2LOSS_SVR) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(param_tmp->solver_type == L2R_L2LOSS_SVR) |
0 |
0 |
0 |
|
| 2823 |
0 |
| *best_score = INF; |
| 2824 |
2 |
| *best_C = start_C; |
| 2825 |
|
| |
| 2826 |
2 |
| param_tmp->C = start_C; |
| 2827 |
14 |
▶ | while(param_tmp->C <= max_C) |
Branch: 1 branch
| Branch | True | False | Total |
| while(param_tmp->C <= max_C) |
14 |
0 |
14 |
|
| 2828 |
|
| { |
| 2829 |
|
| //Output disabled for running CV at a particular C |
| 2830 |
14 |
| set_print_string_function(&print_null); |
| 2831 |
|
| |
| 2832 |
84 |
▶ | for(i=0; i<nr_fold; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<nr_fold; i++) |
70 |
14 |
84 |
|
| 2833 |
|
| { |
| 2834 |
|
| int j; |
| 2835 |
70 |
| int begin = fold_start[i]; |
| 2836 |
70 |
| int end = fold_start[i+1]; |
| 2837 |
|
| |
| 2838 |
70 |
| param_tmp->init_sol = prev_w[i]; |
| 2839 |
70 |
▶ | struct model *submodel = train(&subprob[i],param_tmp); |
Branch: 1 branch
| Branch | True | False | Total |
| struct model *submodel = train(&subprob[i],param_tmp); |
70 |
0 |
70 |
|
| 2840 |
|
| |
| 2841 |
|
| int total_w_size; |
| 2842 |
70 |
▶ | if(submodel->nr_class == 2) |
Branch: 1 branch
| Branch | True | False | Total |
| if(submodel->nr_class == 2) |
70 |
0 |
70 |
|
| 2843 |
70 |
| total_w_size = subprob[i].n; |
| 2844 |
|
| else |
| 2845 |
0 |
| total_w_size = subprob[i].n * submodel->nr_class; |
| 2846 |
|
| |
| 2847 |
70 |
▶ | if(prev_w[i] == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(prev_w[i] == NULL) |
10 |
60 |
70 |
|
| 2848 |
|
| { |
| 2849 |
10 |
| prev_w[i] = Malloc(double, total_w_size); |
| 2850 |
140 |
▶ | for(j=0; j<total_w_size; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<total_w_size; j++) |
130 |
10 |
140 |
|
| 2851 |
130 |
| prev_w[i][j] = submodel->w[j]; |
| 2852 |
|
| } |
| 2853 |
60 |
▶ | else if(num_unchanged_w >= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(num_unchanged_w >= 0) |
57 |
3 |
60 |
|
| 2854 |
|
| { |
| 2855 |
57 |
| double norm_w_diff = 0; |
| 2856 |
798 |
▶ | for(j=0; j<total_w_size; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<total_w_size; j++) |
741 |
57 |
798 |
|
| 2857 |
|
| { |
| 2858 |
741 |
| norm_w_diff += (submodel->w[j] - prev_w[i][j])*(submodel->w[j] - prev_w[i][j]); |
| 2859 |
741 |
| prev_w[i][j] = submodel->w[j]; |
| 2860 |
|
| } |
| 2861 |
57 |
| norm_w_diff = sqrt(norm_w_diff); |
| 2862 |
|
| |
| 2863 |
57 |
▶ | if(norm_w_diff > 1e-15) |
Branch: 1 branch
| Branch | True | False | Total |
| if(norm_w_diff > 1e-15) |
1 |
56 |
57 |
|
| 2864 |
1 |
| num_unchanged_w = -1; |
| 2865 |
|
| } |
| 2866 |
|
| else |
| 2867 |
|
| { |
| 2868 |
42 |
▶ | for(j=0; j<total_w_size; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<total_w_size; j++) |
39 |
3 |
42 |
|
| 2869 |
39 |
| prev_w[i][j] = submodel->w[j]; |
| 2870 |
|
| } |
| 2871 |
|
| |
| 2872 |
3850 |
▶ | for(j=begin; j<end; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=begin; j<end; j++) |
3780 |
70 |
3850 |
|
| 2873 |
3780 |
▶ | target[perm[j]] = predict(submodel,prob->x[perm[j]]); |
Branch: 1 branch
| Branch | True | False | Total |
| target[perm[j]] = predict(submodel,prob->x[perm[j]]); |
3780 |
0 |
3780 |
|
| 2874 |
|
| |
| 2875 |
70 |
▶ | free_and_destroy_model(&submodel); |
Branch: 1 branch
| Branch | True | False | Total |
| free_and_destroy_model(&submodel); |
70 |
0 |
70 |
|
| 2876 |
|
| } |
| 2877 |
14 |
| set_print_string_function(default_print_string); |
| 2878 |
|
| |
| 2879 |
14 |
▶ | if(param_tmp->solver_type == L2R_LR || param_tmp->solver_type == L2R_L2LOSS_SVC) |
Branch: 2 branches
| Branch | True | False | Total |
| if(param_tmp->solver_type == L2R_LR || param_tmp->solver_type == L2R_L2LOSS_SVC) |
14 |
0 |
14 |
| if(param_tmp->solver_type == L2R_LR || param_tmp->solver_type == L2R_L2LOSS_SVC) |
14 |
0 |
14 |
|
| 2880 |
|
| { |
| 2881 |
14 |
| int total_correct = 0; |
| 2882 |
3794 |
▶ | for(i=0; i<prob->l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<prob->l; i++) |
3780 |
14 |
3794 |
|
| 2883 |
3780 |
▶ | if(target[i] == prob->y[i]) |
Branch: 1 branch
| Branch | True | False | Total |
| if(target[i] == prob->y[i]) |
3102 |
678 |
3780 |
|
| 2884 |
3102 |
| ++total_correct; |
| 2885 |
14 |
| double current_rate = (double)total_correct/prob->l; |
| 2886 |
14 |
▶ | if(current_rate > *best_score) |
Branch: 1 branch
| Branch | True | False | Total |
| if(current_rate > *best_score) |
2 |
12 |
14 |
|
| 2887 |
|
| { |
| 2888 |
2 |
| *best_C = param_tmp->C; |
| 2889 |
2 |
| *best_score = current_rate; |
| 2890 |
|
| } |
| 2891 |
|
| |
| 2892 |
14 |
| info("log2c=%7.2f\trate=%g\n",log(param_tmp->C)/log(2.0),100.0*current_rate); |
| 2893 |
14 |
| } |
| 2894 |
0 |
▶ | else if(param_tmp->solver_type == L2R_L2LOSS_SVR) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(param_tmp->solver_type == L2R_L2LOSS_SVR) |
0 |
0 |
0 |
|
| 2895 |
|
| { |
| 2896 |
0 |
| double total_error = 0.0; |
| 2897 |
0 |
▶ | for(i=0; i<prob->l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<prob->l; i++) |
0 |
0 |
0 |
|
| 2898 |
|
| { |
| 2899 |
0 |
| double y = prob->y[i]; |
| 2900 |
0 |
| double v = target[i]; |
| 2901 |
0 |
| total_error += (v-y)*(v-y); |
| 2902 |
|
| } |
| 2903 |
0 |
| double current_error = total_error/prob->l; |
| 2904 |
0 |
▶ | if(current_error < *best_score) |
Branch: 1 branch
| Branch | True | False | Total |
| if(current_error < *best_score) |
0 |
0 |
0 |
|
| 2905 |
|
| { |
| 2906 |
0 |
| *best_C = param_tmp->C; |
| 2907 |
0 |
| *best_score = current_error; |
| 2908 |
|
| } |
| 2909 |
|
| |
| 2910 |
0 |
| info("log2c=%7.2f\tp=%7.2f\tMean squared error=%g\n",log(param_tmp->C)/log(2.0),param_tmp->p,current_error); |
| 2911 |
|
| } |
| 2912 |
|
| |
| 2913 |
14 |
| num_unchanged_w++; |
| 2914 |
14 |
▶ | if(num_unchanged_w == 5) |
Branch: 1 branch
| Branch | True | False | Total |
| if(num_unchanged_w == 5) |
2 |
12 |
14 |
|
| 2915 |
2 |
| break; |
| 2916 |
12 |
| param_tmp->C = param_tmp->C*ratio; |
| 2917 |
|
| } |
| 2918 |
|
| |
| 2919 |
2 |
▶ | if(param_tmp->C > max_C) |
Branch: 1 branch
| Branch | True | False | Total |
| if(param_tmp->C > max_C) |
0 |
2 |
2 |
|
| 2920 |
0 |
| info("WARNING: maximum C reached.\n"); |
| 2921 |
2 |
| free(target); |
| 2922 |
12 |
▶ | for(i=0; i<nr_fold; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<nr_fold; i++) |
10 |
2 |
12 |
|
| 2923 |
10 |
| free(prev_w[i]); |
| 2924 |
2 |
| free(prev_w); |
| 2925 |
2 |
| } |
| 2926 |
|
| |
| 2927 |
|
| |
| 2928 |
|
| // |
| 2929 |
|
| // Interface functions |
| 2930 |
|
| // |
| 2931 |
78 |
| model* train(const problem *prob, const parameter *param) |
| 2932 |
|
| { |
| 2933 |
|
| int i,j; |
| 2934 |
78 |
| int l = prob->l; |
| 2935 |
78 |
| int n = prob->n; |
| 2936 |
78 |
| int w_size = prob->n; |
| 2937 |
78 |
| model *model_ = Malloc(model,1); |
| 2938 |
|
| |
| 2939 |
78 |
▶ | if(prob->bias>=0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(prob->bias>=0) |
0 |
78 |
78 |
|
| 2940 |
0 |
| model_->nr_feature=n-1; |
| 2941 |
|
| else |
| 2942 |
78 |
| model_->nr_feature=n; |
| 2943 |
78 |
| model_->param = *param; |
| 2944 |
78 |
| model_->bias = prob->bias; |
| 2945 |
|
| |
| 2946 |
78 |
▶ | if(check_regression_model(model_)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(check_regression_model(model_)) |
0 |
78 |
78 |
|
| 2947 |
|
| { |
| 2948 |
0 |
| model_->w = Malloc(double, w_size); |
| 2949 |
|
| |
| 2950 |
0 |
▶ | if(param->init_sol != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(param->init_sol != NULL) |
0 |
0 |
0 |
|
| 2951 |
0 |
▶ | for(i=0;i<w_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<w_size;i++) |
0 |
0 |
0 |
|
| 2952 |
0 |
| model_->w[i] = param->init_sol[i]; |
| 2953 |
|
| else |
| 2954 |
0 |
▶ | for(i=0;i<w_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<w_size;i++) |
0 |
0 |
0 |
|
| 2955 |
0 |
| model_->w[i] = 0; |
| 2956 |
|
| |
| 2957 |
0 |
| model_->nr_class = 2; |
| 2958 |
0 |
| model_->label = NULL; |
| 2959 |
0 |
| train_one(prob, param, model_->w, 0, 0); |
| 2960 |
|
| } |
| 2961 |
78 |
▶ | else if(check_oneclass_model(model_)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(check_oneclass_model(model_)) |
0 |
78 |
78 |
|
| 2962 |
|
| { |
| 2963 |
0 |
| model_->w = Malloc(double, w_size); |
| 2964 |
0 |
| model_->nr_class = 2; |
| 2965 |
0 |
| model_->label = NULL; |
| 2966 |
0 |
| solve_oneclass_svm(prob, param, model_->w, &(model_->rho)); |
| 2967 |
|
| } |
| 2968 |
|
| else |
| 2969 |
|
| { |
| 2970 |
|
| int nr_class; |
| 2971 |
78 |
| int *label = NULL; |
| 2972 |
78 |
| int *start = NULL; |
| 2973 |
78 |
| int *count = NULL; |
| 2974 |
78 |
| int *perm = Malloc(int,l); |
| 2975 |
|
| |
| 2976 |
|
| // group training data of the same class |
| 2977 |
78 |
▶ | group_classes(prob,&nr_class,&label,&start,&count,perm); |
Branch: 1 branch
| Branch | True | False | Total |
| group_classes(prob,&nr_class,&label,&start,&count,perm); |
78 |
0 |
78 |
|
| 2978 |
|
| |
| 2979 |
78 |
| model_->nr_class=nr_class; |
| 2980 |
78 |
| model_->label = Malloc(int,nr_class); |
| 2981 |
234 |
▶ | for(i=0;i<nr_class;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<nr_class;i++) |
156 |
78 |
234 |
|
| 2982 |
156 |
| model_->label[i] = label[i]; |
| 2983 |
|
| |
| 2984 |
|
| // calculate weighted C |
| 2985 |
78 |
| double *weighted_C = Malloc(double, nr_class); |
| 2986 |
234 |
▶ | for(i=0;i<nr_class;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<nr_class;i++) |
156 |
78 |
234 |
|
| 2987 |
156 |
| weighted_C[i] = param->C; |
| 2988 |
234 |
▶ | for(i=0;i<param->nr_weight;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<param->nr_weight;i++) |
156 |
78 |
234 |
|
| 2989 |
|
| { |
| 2990 |
234 |
▶ | for(j=0;j<nr_class;j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0;j<nr_class;j++) |
234 |
0 |
234 |
|
| 2991 |
234 |
▶ | if(param->weight_label[i] == label[j]) |
Branch: 1 branch
| Branch | True | False | Total |
| if(param->weight_label[i] == label[j]) |
156 |
78 |
234 |
|
| 2992 |
156 |
| break; |
| 2993 |
156 |
▶ | if(j == nr_class) |
Branch: 1 branch
| Branch | True | False | Total |
| if(j == nr_class) |
0 |
156 |
156 |
|
| 2994 |
0 |
▶ | fprintf(stderr,"WARNING: class label %d specified in weight is not found\n", param->weight_label[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| fprintf(stderr,"WARNING: class label %d specified in weight is not found\n", param->weight_label[i]); |
0 |
0 |
0 |
|
| 2995 |
|
| else |
| 2996 |
156 |
| weighted_C[j] *= param->weight[i]; |
| 2997 |
|
| } |
| 2998 |
|
| |
| 2999 |
|
| // constructing the subproblem |
| 3000 |
78 |
| feature_node **x = Malloc(feature_node *,l); |
| 3001 |
17088 |
▶ | for(i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) |
17010 |
78 |
17088 |
|
| 3002 |
17010 |
| x[i] = prob->x[perm[i]]; |
| 3003 |
|
| |
| 3004 |
|
| int k; |
| 3005 |
|
| problem sub_prob; |
| 3006 |
78 |
| sub_prob.l = l; |
| 3007 |
78 |
| sub_prob.n = n; |
| 3008 |
78 |
| sub_prob.x = Malloc(feature_node *,sub_prob.l); |
| 3009 |
78 |
| sub_prob.y = Malloc(double,sub_prob.l); |
| 3010 |
|
| |
| 3011 |
17088 |
▶ | for(k=0; k<sub_prob.l; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(k=0; k<sub_prob.l; k++) |
17010 |
78 |
17088 |
|
| 3012 |
17010 |
| sub_prob.x[k] = x[k]; |
| 3013 |
|
| |
| 3014 |
|
| // multi-class svm by Crammer and Singer |
| 3015 |
78 |
▶ | if(param->solver_type == MCSVM_CS) |
Branch: 1 branch
| Branch | True | False | Total |
| if(param->solver_type == MCSVM_CS) |
0 |
78 |
78 |
|
| 3016 |
|
| { |
| 3017 |
0 |
| model_->w=Malloc(double, n*nr_class); |
| 3018 |
0 |
▶ | for(i=0;i<nr_class;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<nr_class;i++) |
0 |
0 |
0 |
|
| 3019 |
0 |
▶ | for(j=start[i];j<start[i]+count[i];j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=start[i];j<start[i]+count[i];j++) |
0 |
0 |
0 |
|
| 3020 |
0 |
| sub_prob.y[j] = i; |
| 3021 |
0 |
▶ | Solver_MCSVM_CS Solver(&sub_prob, nr_class, weighted_C, param->eps); |
Branch: 1 branch
| Branch | True | False | Total |
| Solver_MCSVM_CS Solver(&sub_prob, nr_class, weighted_C, param->eps); |
0 |
0 |
0 |
|
| 3022 |
0 |
▶ | Solver.Solve(model_->w); |
Branch: 1 branch
| Branch | True | False | Total |
| Solver.Solve(model_->w); |
0 |
0 |
0 |
|
| 3023 |
0 |
| } |
| 3024 |
|
| else |
| 3025 |
|
| { |
| 3026 |
78 |
▶ | if(nr_class == 2) |
Branch: 1 branch
| Branch | True | False | Total |
| if(nr_class == 2) |
78 |
0 |
78 |
|
| 3027 |
|
| { |
| 3028 |
78 |
| model_->w=Malloc(double, w_size); |
| 3029 |
|
| |
| 3030 |
78 |
| int e0 = start[0]+count[0]; |
| 3031 |
78 |
| k=0; |
| 3032 |
7638 |
▶ | for(; k<e0; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(; k<e0; k++) |
7560 |
78 |
7638 |
|
| 3033 |
7560 |
| sub_prob.y[k] = +1; |
| 3034 |
9528 |
▶ | for(; k<sub_prob.l; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(; k<sub_prob.l; k++) |
9450 |
78 |
9528 |
|
| 3035 |
9450 |
| sub_prob.y[k] = -1; |
| 3036 |
|
| |
| 3037 |
78 |
▶ | if(param->init_sol != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(param->init_sol != NULL) |
60 |
18 |
78 |
|
| 3038 |
840 |
▶ | for(i=0;i<w_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<w_size;i++) |
780 |
60 |
840 |
|
| 3039 |
780 |
| model_->w[i] = param->init_sol[i]; |
| 3040 |
|
| else |
| 3041 |
252 |
▶ | for(i=0;i<w_size;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<w_size;i++) |
234 |
18 |
252 |
|
| 3042 |
234 |
| model_->w[i] = 0; |
| 3043 |
|
| |
| 3044 |
78 |
▶ | train_one(&sub_prob, param, model_->w, weighted_C[0], weighted_C[1]); |
Branch: 1 branch
| Branch | True | False | Total |
| train_one(&sub_prob, param, model_->w, weighted_C[0], weighted_C[1]); |
78 |
0 |
78 |
|
| 3045 |
|
| } |
| 3046 |
|
| else |
| 3047 |
|
| { |
| 3048 |
0 |
| model_->w=Malloc(double, w_size*nr_class); |
| 3049 |
0 |
| double *w=Malloc(double, w_size); |
| 3050 |
0 |
▶ | for(i=0;i<nr_class;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<nr_class;i++) |
0 |
0 |
0 |
|
| 3051 |
|
| { |
| 3052 |
0 |
| int si = start[i]; |
| 3053 |
0 |
| int ei = si+count[i]; |
| 3054 |
|
| |
| 3055 |
0 |
| k=0; |
| 3056 |
0 |
▶ | for(; k<si; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(; k<si; k++) |
0 |
0 |
0 |
|
| 3057 |
0 |
| sub_prob.y[k] = -1; |
| 3058 |
0 |
▶ | for(; k<ei; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(; k<ei; k++) |
0 |
0 |
0 |
|
| 3059 |
0 |
| sub_prob.y[k] = +1; |
| 3060 |
0 |
▶ | for(; k<sub_prob.l; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(; k<sub_prob.l; k++) |
0 |
0 |
0 |
|
| 3061 |
0 |
| sub_prob.y[k] = -1; |
| 3062 |
|
| |
| 3063 |
0 |
▶ | if(param->init_sol != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(param->init_sol != NULL) |
0 |
0 |
0 |
|
| 3064 |
0 |
▶ | for(j=0;j<w_size;j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0;j<w_size;j++) |
0 |
0 |
0 |
|
| 3065 |
0 |
| w[j] = param->init_sol[j*nr_class+i]; |
| 3066 |
|
| else |
| 3067 |
0 |
▶ | for(j=0;j<w_size;j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0;j<w_size;j++) |
0 |
0 |
0 |
|
| 3068 |
0 |
| w[j] = 0; |
| 3069 |
|
| |
| 3070 |
0 |
▶ | train_one(&sub_prob, param, w, weighted_C[i], param->C); |
Branch: 1 branch
| Branch | True | False | Total |
| train_one(&sub_prob, param, w, weighted_C[i], param->C); |
0 |
0 |
0 |
|
| 3071 |
|
| |
| 3072 |
0 |
▶ | for(j=0;j<w_size;j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0;j<w_size;j++) |
0 |
0 |
0 |
|
| 3073 |
0 |
| model_->w[j*nr_class+i] = w[j]; |
| 3074 |
|
| } |
| 3075 |
0 |
| free(w); |
| 3076 |
|
| } |
| 3077 |
|
| |
| 3078 |
|
| } |
| 3079 |
|
| |
| 3080 |
78 |
| free(x); |
| 3081 |
78 |
| free(label); |
| 3082 |
78 |
| free(start); |
| 3083 |
78 |
| free(count); |
| 3084 |
78 |
| free(perm); |
| 3085 |
78 |
| free(sub_prob.x); |
| 3086 |
78 |
| free(sub_prob.y); |
| 3087 |
78 |
| free(weighted_C); |
| 3088 |
|
| } |
| 3089 |
78 |
| return model_; |
| 3090 |
|
| } |
| 3091 |
|
| |
| 3092 |
1 |
| void cross_validation(const problem *prob, const parameter *param, int nr_fold, double *target) |
| 3093 |
|
| { |
| 3094 |
|
| int i; |
| 3095 |
|
| int *fold_start; |
| 3096 |
1 |
| int l = prob->l; |
| 3097 |
1 |
| int *perm = Malloc(int,l); |
| 3098 |
1 |
▶ | if (nr_fold > l) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nr_fold > l) |
0 |
1 |
1 |
|
| 3099 |
|
| { |
| 3100 |
0 |
| nr_fold = l; |
| 3101 |
0 |
| fprintf(stderr,"WARNING: # folds > # data. Will use # folds = # data instead (i.e., leave-one-out cross validation)\n"); |
| 3102 |
|
| } |
| 3103 |
1 |
| fold_start = Malloc(int,nr_fold+1); |
| 3104 |
271 |
▶ | for(i=0;i<l;i++) perm[i]=i; |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) perm[i]=i; |
270 |
1 |
271 |
|
| 3105 |
271 |
▶ | for(i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) |
270 |
1 |
271 |
|
| 3106 |
|
| { |
| 3107 |
270 |
| int j = i+rand()%(l-i); |
| 3108 |
270 |
| swap(perm[i],perm[j]); |
| 3109 |
|
| } |
| 3110 |
7 |
▶ | for(i=0;i<=nr_fold;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<=nr_fold;i++) |
6 |
1 |
7 |
|
| 3111 |
6 |
| fold_start[i]=i*l/nr_fold; |
| 3112 |
|
| |
| 3113 |
6 |
▶ | for(i=0;i<nr_fold;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<nr_fold;i++) |
5 |
1 |
6 |
|
| 3114 |
|
| { |
| 3115 |
5 |
| int begin = fold_start[i]; |
| 3116 |
5 |
| int end = fold_start[i+1]; |
| 3117 |
|
| int j,k; |
| 3118 |
|
| struct problem subprob; |
| 3119 |
|
| |
| 3120 |
5 |
| subprob.bias = prob->bias; |
| 3121 |
5 |
| subprob.n = prob->n; |
| 3122 |
5 |
| subprob.l = l-(end-begin); |
| 3123 |
5 |
| subprob.x = Malloc(struct feature_node*,subprob.l); |
| 3124 |
5 |
| subprob.y = Malloc(double,subprob.l); |
| 3125 |
|
| |
| 3126 |
5 |
| k=0; |
| 3127 |
545 |
▶ | for(j=0;j<begin;j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0;j<begin;j++) |
540 |
5 |
545 |
|
| 3128 |
|
| { |
| 3129 |
540 |
| subprob.x[k] = prob->x[perm[j]]; |
| 3130 |
540 |
| subprob.y[k] = prob->y[perm[j]]; |
| 3131 |
540 |
| ++k; |
| 3132 |
|
| } |
| 3133 |
545 |
▶ | for(j=end;j<l;j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=end;j<l;j++) |
540 |
5 |
545 |
|
| 3134 |
|
| { |
| 3135 |
540 |
| subprob.x[k] = prob->x[perm[j]]; |
| 3136 |
540 |
| subprob.y[k] = prob->y[perm[j]]; |
| 3137 |
540 |
| ++k; |
| 3138 |
|
| } |
| 3139 |
5 |
▶ | struct model *submodel = train(&subprob,param); |
Branch: 1 branch
| Branch | True | False | Total |
| struct model *submodel = train(&subprob,param); |
5 |
0 |
5 |
|
| 3140 |
275 |
▶ | for(j=begin;j<end;j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=begin;j<end;j++) |
270 |
5 |
275 |
|
| 3141 |
270 |
▶ | target[perm[j]] = predict(submodel,prob->x[perm[j]]); |
Branch: 1 branch
| Branch | True | False | Total |
| target[perm[j]] = predict(submodel,prob->x[perm[j]]); |
270 |
0 |
270 |
|
| 3142 |
5 |
▶ | free_and_destroy_model(&submodel); |
Branch: 1 branch
| Branch | True | False | Total |
| free_and_destroy_model(&submodel); |
5 |
0 |
5 |
|
| 3143 |
5 |
| free(subprob.x); |
| 3144 |
5 |
| free(subprob.y); |
| 3145 |
|
| } |
| 3146 |
1 |
| free(fold_start); |
| 3147 |
1 |
| free(perm); |
| 3148 |
1 |
| } |
| 3149 |
|
| |
| 3150 |
|
| |
| 3151 |
2 |
| void find_parameters(const problem *prob, const parameter *param, int nr_fold, double start_C, double start_p, double *best_C, double *best_p, double *best_score) |
| 3152 |
|
| { |
| 3153 |
|
| // prepare CV folds |
| 3154 |
|
| |
| 3155 |
|
| int i; |
| 3156 |
|
| int *fold_start; |
| 3157 |
2 |
| int l = prob->l; |
| 3158 |
2 |
| int *perm = Malloc(int, l); |
| 3159 |
2 |
| struct problem *subprob = Malloc(problem,nr_fold); |
| 3160 |
|
| |
| 3161 |
2 |
▶ | if (nr_fold > l) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nr_fold > l) |
0 |
2 |
2 |
|
| 3162 |
|
| { |
| 3163 |
0 |
| nr_fold = l; |
| 3164 |
0 |
▶ | fprintf(stderr,"WARNING: # folds > # data. Will use # folds = # data instead (i.e., leave-one-out cross validation)\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| fprintf(stderr,"WARNING: # folds > # data. Will use # folds = # data instead (i.e., leave-one-out cross validation)\n"); |
0 |
0 |
0 |
|
| 3165 |
|
| } |
| 3166 |
2 |
| fold_start = Malloc(int,nr_fold+1); |
| 3167 |
542 |
▶ | for(i=0;i<l;i++) perm[i]=i; |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) perm[i]=i; |
540 |
2 |
542 |
|
| 3168 |
542 |
▶ | for(i=0;i<l;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<l;i++) |
540 |
2 |
542 |
|
| 3169 |
|
| { |
| 3170 |
540 |
| int j = i+rand()%(l-i); |
| 3171 |
540 |
| swap(perm[i],perm[j]); |
| 3172 |
|
| } |
| 3173 |
14 |
▶ | for(i=0;i<=nr_fold;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<=nr_fold;i++) |
12 |
2 |
14 |
|
| 3174 |
12 |
| fold_start[i]=i*l/nr_fold; |
| 3175 |
|
| |
| 3176 |
12 |
▶ | for(i=0;i<nr_fold;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<nr_fold;i++) |
10 |
2 |
12 |
|
| 3177 |
|
| { |
| 3178 |
10 |
| int begin = fold_start[i]; |
| 3179 |
10 |
| int end = fold_start[i+1]; |
| 3180 |
|
| int j,k; |
| 3181 |
|
| |
| 3182 |
10 |
| subprob[i].bias = prob->bias; |
| 3183 |
10 |
| subprob[i].n = prob->n; |
| 3184 |
10 |
| subprob[i].l = l-(end-begin); |
| 3185 |
10 |
| subprob[i].x = Malloc(struct feature_node*,subprob[i].l); |
| 3186 |
10 |
| subprob[i].y = Malloc(double,subprob[i].l); |
| 3187 |
|
| |
| 3188 |
10 |
| k=0; |
| 3189 |
1090 |
▶ | for(j=0;j<begin;j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0;j<begin;j++) |
1080 |
10 |
1090 |
|
| 3190 |
|
| { |
| 3191 |
1080 |
| subprob[i].x[k] = prob->x[perm[j]]; |
| 3192 |
1080 |
| subprob[i].y[k] = prob->y[perm[j]]; |
| 3193 |
1080 |
| ++k; |
| 3194 |
|
| } |
| 3195 |
1090 |
▶ | for(j=end;j<l;j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=end;j<l;j++) |
1080 |
10 |
1090 |
|
| 3196 |
|
| { |
| 3197 |
1080 |
| subprob[i].x[k] = prob->x[perm[j]]; |
| 3198 |
1080 |
| subprob[i].y[k] = prob->y[perm[j]]; |
| 3199 |
1080 |
| ++k; |
| 3200 |
|
| } |
| 3201 |
|
| } |
| 3202 |
|
| |
| 3203 |
2 |
| struct parameter param_tmp = *param; |
| 3204 |
2 |
| *best_p = -1; |
| 3205 |
2 |
▶ | if(param->solver_type == L2R_LR || param->solver_type == L2R_L2LOSS_SVC) |
Branch: 2 branches
| Branch | True | False | Total |
| if(param->solver_type == L2R_LR || param->solver_type == L2R_L2LOSS_SVC) |
2 |
0 |
2 |
| if(param->solver_type == L2R_LR || param->solver_type == L2R_L2LOSS_SVC) |
2 |
0 |
2 |
|
| 3206 |
|
| { |
| 3207 |
2 |
▶ | if(start_C <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(start_C <= 0) |
0 |
2 |
2 |
|
| 3208 |
0 |
▶ | start_C = calc_start_C(prob, ¶m_tmp); |
Branch: 1 branch
| Branch | True | False | Total |
| start_C = calc_start_C(prob, ¶m_tmp); |
0 |
0 |
0 |
|
| 3209 |
2 |
| double max_C = 1024; |
| 3210 |
2 |
| start_C = min(start_C, max_C); |
| 3211 |
|
| double best_C_tmp, best_score_tmp; |
| 3212 |
|
| |
| 3213 |
2 |
▶ | find_parameter_C(prob, ¶m_tmp, start_C, max_C, &best_C_tmp, &best_score_tmp, fold_start, perm, subprob, nr_fold); |
Branch: 1 branch
| Branch | True | False | Total |
| find_parameter_C(prob, ¶m_tmp, start_C, max_C, &best_C_tmp, &best_score_tmp, fold_start, perm, subprob, nr_fold); |
2 |
0 |
2 |
|
| 3214 |
|
| |
| 3215 |
2 |
| *best_C = best_C_tmp; |
| 3216 |
2 |
| *best_score = best_score_tmp; |
| 3217 |
2 |
| } |
| 3218 |
0 |
▶ | else if(param->solver_type == L2R_L2LOSS_SVR) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(param->solver_type == L2R_L2LOSS_SVR) |
0 |
0 |
0 |
|
| 3219 |
|
| { |
| 3220 |
0 |
▶ | double max_p = calc_max_p(prob); |
Branch: 1 branch
| Branch | True | False | Total |
| double max_p = calc_max_p(prob); |
0 |
0 |
0 |
|
| 3221 |
0 |
| int num_p_steps = 20; |
| 3222 |
0 |
| double max_C = 1048576; |
| 3223 |
0 |
| *best_score = INF; |
| 3224 |
|
| |
| 3225 |
0 |
| i = num_p_steps-1; |
| 3226 |
0 |
▶ | if(start_p > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(start_p > 0) |
0 |
0 |
0 |
|
| 3227 |
0 |
| i = min((int)(start_p/(max_p/num_p_steps)), i); |
| 3228 |
0 |
▶ | for(; i >= 0; i--) |
Branch: 1 branch
| Branch | True | False | Total |
| for(; i >= 0; i--) |
0 |
0 |
0 |
|
| 3229 |
|
| { |
| 3230 |
0 |
| param_tmp.p = i*max_p/num_p_steps; |
| 3231 |
|
| double start_C_tmp; |
| 3232 |
0 |
▶ | if(start_C <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(start_C <= 0) |
0 |
0 |
0 |
|
| 3233 |
0 |
▶ | start_C_tmp = calc_start_C(prob, ¶m_tmp); |
Branch: 1 branch
| Branch | True | False | Total |
| start_C_tmp = calc_start_C(prob, ¶m_tmp); |
0 |
0 |
0 |
|
| 3234 |
|
| else |
| 3235 |
0 |
| start_C_tmp = start_C; |
| 3236 |
0 |
| start_C_tmp = min(start_C_tmp, max_C); |
| 3237 |
|
| double best_C_tmp, best_score_tmp; |
| 3238 |
|
| |
| 3239 |
0 |
▶ | find_parameter_C(prob, ¶m_tmp, start_C_tmp, max_C, &best_C_tmp, &best_score_tmp, fold_start, perm, subprob, nr_fold); |
Branch: 1 branch
| Branch | True | False | Total |
| find_parameter_C(prob, ¶m_tmp, start_C_tmp, max_C, &best_C_tmp, &best_score_tmp, fold_start, perm, subprob, nr_fold); |
0 |
0 |
0 |
|
| 3240 |
|
| |
| 3241 |
0 |
▶ | if(best_score_tmp < *best_score) |
Branch: 1 branch
| Branch | True | False | Total |
| if(best_score_tmp < *best_score) |
0 |
0 |
0 |
|
| 3242 |
|
| { |
| 3243 |
0 |
| *best_p = param_tmp.p; |
| 3244 |
0 |
| *best_C = best_C_tmp; |
| 3245 |
0 |
| *best_score = best_score_tmp; |
| 3246 |
|
| } |
| 3247 |
|
| } |
| 3248 |
|
| } |
| 3249 |
|
| |
| 3250 |
2 |
| free(fold_start); |
| 3251 |
2 |
| free(perm); |
| 3252 |
12 |
▶ | for(i=0; i<nr_fold; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<nr_fold; i++) |
10 |
2 |
12 |
|
| 3253 |
|
| { |
| 3254 |
10 |
| free(subprob[i].x); |
| 3255 |
10 |
| free(subprob[i].y); |
| 3256 |
|
| } |
| 3257 |
2 |
| free(subprob); |
| 3258 |
2 |
| } |
| 3259 |
|
| |
| 3260 |
4051 |
| double predict_values(const struct model *model_, const struct feature_node *x, double *dec_values) |
| 3261 |
|
| { |
| 3262 |
|
| int idx; |
| 3263 |
|
| int n; |
| 3264 |
4051 |
▶ | if(model_->bias>=0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(model_->bias>=0) |
0 |
4051 |
4051 |
|
| 3265 |
0 |
| n=model_->nr_feature+1; |
| 3266 |
|
| else |
| 3267 |
4051 |
| n=model_->nr_feature; |
| 3268 |
4051 |
| double *w=model_->w; |
| 3269 |
4051 |
| int nr_class=model_->nr_class; |
| 3270 |
|
| int i; |
| 3271 |
|
| int nr_w; |
| 3272 |
4051 |
▶ | if(nr_class==2 && model_->param.solver_type != MCSVM_CS) |
Branch: 2 branches
| Branch | True | False | Total |
| if(nr_class==2 && model_->param.solver_type != MCSVM_CS) |
4051 |
0 |
4051 |
| if(nr_class==2 && model_->param.solver_type != MCSVM_CS) |
4051 |
0 |
4051 |
|
| 3273 |
4051 |
| nr_w = 1; |
| 3274 |
|
| else |
| 3275 |
0 |
| nr_w = nr_class; |
| 3276 |
|
| |
| 3277 |
4051 |
| const feature_node *lx=x; |
| 3278 |
8102 |
▶ | for(i=0;i<nr_w;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<nr_w;i++) |
4051 |
4051 |
8102 |
|
| 3279 |
4051 |
| dec_values[i] = 0; |
| 3280 |
54733 |
▶ | for(; (idx=lx->index)!=-1; lx++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(; (idx=lx->index)!=-1; lx++) |
50682 |
4051 |
54733 |
|
| 3281 |
|
| { |
| 3282 |
|
| // the dimension of testing data may exceed that of training |
| 3283 |
50682 |
▶ | if(idx<=n) |
Branch: 1 branch
| Branch | True | False | Total |
| if(idx<=n) |
50682 |
0 |
50682 |
|
| 3284 |
101364 |
▶ | for(i=0;i<nr_w;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<nr_w;i++) |
50682 |
50682 |
101364 |
|
| 3285 |
50682 |
| dec_values[i] += w[(idx-1)*nr_w+i]*lx->value; |
| 3286 |
|
| } |
| 3287 |
4051 |
▶ | if(check_oneclass_model(model_)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(check_oneclass_model(model_)) |
0 |
4051 |
4051 |
|
| 3288 |
0 |
| dec_values[0] -= model_->rho; |
| 3289 |
|
| |
| 3290 |
4051 |
▶ | if(nr_class==2) |
Branch: 1 branch
| Branch | True | False | Total |
| if(nr_class==2) |
4051 |
0 |
4051 |
|
| 3291 |
|
| { |
| 3292 |
4051 |
▶ | if(check_regression_model(model_)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(check_regression_model(model_)) |
0 |
4051 |
4051 |
|
| 3293 |
0 |
| return dec_values[0]; |
| 3294 |
4051 |
▶ | else if(check_oneclass_model(model_)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(check_oneclass_model(model_)) |
0 |
4051 |
4051 |
|
| 3295 |
0 |
▶ | return (dec_values[0]>0)?1:-1; |
Branch: 1 branch
| Branch | True | False | Total |
| return (dec_values[0]>0)?1:-1; |
0 |
0 |
0 |
|
| 3296 |
|
| else |
| 3297 |
4051 |
▶ | return (dec_values[0]>0)?model_->label[0]:model_->label[1]; |
Branch: 1 branch
| Branch | True | False | Total |
| return (dec_values[0]>0)?model_->label[0]:model_->label[1]; |
1675 |
2376 |
4051 |
|
| 3298 |
|
| } |
| 3299 |
|
| else |
| 3300 |
|
| { |
| 3301 |
0 |
| int dec_max_idx = 0; |
| 3302 |
0 |
▶ | for(i=1;i<nr_class;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=1;i<nr_class;i++) |
0 |
0 |
0 |
|
| 3303 |
|
| { |
| 3304 |
0 |
▶ | if(dec_values[i] > dec_values[dec_max_idx]) |
Branch: 1 branch
| Branch | True | False | Total |
| if(dec_values[i] > dec_values[dec_max_idx]) |
0 |
0 |
0 |
|
| 3305 |
0 |
| dec_max_idx = i; |
| 3306 |
|
| } |
| 3307 |
0 |
| return model_->label[dec_max_idx]; |
| 3308 |
|
| } |
| 3309 |
|
| } |
| 3310 |
|
| |
| 3311 |
4051 |
| double predict(const model *model_, const feature_node *x) |
| 3312 |
|
| { |
| 3313 |
4051 |
| double *dec_values = Malloc(double, model_->nr_class); |
| 3314 |
4051 |
| double label=predict_values(model_, x, dec_values); |
| 3315 |
4051 |
| free(dec_values); |
| 3316 |
4051 |
| return label; |
| 3317 |
|
| } |
| 3318 |
|
| |
| 3319 |
0 |
| double predict_probability(const struct model *model_, const struct feature_node *x, double* prob_estimates) |
| 3320 |
|
| { |
| 3321 |
0 |
▶ | if(check_probability_model(model_)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(check_probability_model(model_)) |
0 |
0 |
0 |
|
| 3322 |
|
| { |
| 3323 |
|
| int i; |
| 3324 |
0 |
| int nr_class=model_->nr_class; |
| 3325 |
|
| int nr_w; |
| 3326 |
0 |
▶ | if(nr_class==2) |
Branch: 1 branch
| Branch | True | False | Total |
| if(nr_class==2) |
0 |
0 |
0 |
|
| 3327 |
0 |
| nr_w = 1; |
| 3328 |
|
| else |
| 3329 |
0 |
| nr_w = nr_class; |
| 3330 |
|
| |
| 3331 |
0 |
| double label=predict_values(model_, x, prob_estimates); |
| 3332 |
0 |
▶ | for(i=0;i<nr_w;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<nr_w;i++) |
0 |
0 |
0 |
|
| 3333 |
0 |
| prob_estimates[i]=1/(1+exp(-prob_estimates[i])); |
| 3334 |
|
| |
| 3335 |
0 |
▶ | if(nr_class==2) // for binary classification |
Branch: 1 branch
| Branch | True | False | Total |
| if(nr_class==2) // for binary classification |
0 |
0 |
0 |
|
| 3336 |
0 |
| prob_estimates[1]=1.-prob_estimates[0]; |
| 3337 |
|
| else |
| 3338 |
|
| { |
| 3339 |
0 |
| double sum=0; |
| 3340 |
0 |
▶ | for(i=0; i<nr_class; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<nr_class; i++) |
0 |
0 |
0 |
|
| 3341 |
0 |
| sum+=prob_estimates[i]; |
| 3342 |
|
| |
| 3343 |
0 |
▶ | for(i=0; i<nr_class; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<nr_class; i++) |
0 |
0 |
0 |
|
| 3344 |
0 |
| prob_estimates[i]=prob_estimates[i]/sum; |
| 3345 |
|
| } |
| 3346 |
|
| |
| 3347 |
0 |
| return label; |
| 3348 |
|
| } |
| 3349 |
|
| else |
| 3350 |
0 |
| return 0; |
| 3351 |
|
| } |
| 3352 |
|
| |
| 3353 |
|
| static const char *solver_type_table[]= |
| 3354 |
|
| { |
| 3355 |
|
| "L2R_LR", "L2R_L2LOSS_SVC_DUAL", "L2R_L2LOSS_SVC", "L2R_L1LOSS_SVC_DUAL", "MCSVM_CS", |
| 3356 |
|
| "L1R_L2LOSS_SVC", "L1R_LR", "L2R_LR_DUAL", |
| 3357 |
|
| "", "", "", |
| 3358 |
|
| "L2R_L2LOSS_SVR", "L2R_L2LOSS_SVR_DUAL", "L2R_L1LOSS_SVR_DUAL", |
| 3359 |
|
| "", "", "", "", "", "", "", |
| 3360 |
|
| "ONECLASS_SVM", NULL |
| 3361 |
|
| }; |
| 3362 |
|
| |
| 3363 |
1 |
| int save_model(const char *model_file_name, const struct model *model_) |
| 3364 |
|
| { |
| 3365 |
|
| int i; |
| 3366 |
1 |
| int nr_feature=model_->nr_feature; |
| 3367 |
|
| int n; |
| 3368 |
1 |
| const parameter& param = model_->param; |
| 3369 |
|
| |
| 3370 |
1 |
▶ | if(model_->bias>=0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(model_->bias>=0) |
0 |
1 |
1 |
|
| 3371 |
0 |
| n=nr_feature+1; |
| 3372 |
|
| else |
| 3373 |
1 |
| n=nr_feature; |
| 3374 |
1 |
| int w_size = n; |
| 3375 |
1 |
| FILE *fp = fopen(model_file_name,"w"); |
| 3376 |
1 |
▶ | if(fp==NULL) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if(fp==NULL) return -1; |
0 |
1 |
1 |
|
| 3377 |
|
| |
| 3378 |
1 |
| char *old_locale = setlocale(LC_ALL, NULL); |
| 3379 |
1 |
▶ | if (old_locale) |
Branch: 1 branch
| Branch | True | False | Total |
| if (old_locale) |
1 |
0 |
1 |
|
| 3380 |
|
| { |
| 3381 |
1 |
| old_locale = strdup(old_locale); |
| 3382 |
|
| } |
| 3383 |
1 |
| setlocale(LC_ALL, "C"); |
| 3384 |
|
| |
| 3385 |
|
| int nr_w; |
| 3386 |
1 |
▶ | if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS) |
Branch: 2 branches
| Branch | True | False | Total |
| if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS) |
1 |
0 |
1 |
| if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS) |
1 |
0 |
1 |
|
| 3387 |
1 |
| nr_w=1; |
| 3388 |
|
| else |
| 3389 |
0 |
| nr_w=model_->nr_class; |
| 3390 |
|
| |
| 3391 |
1 |
| fprintf(fp, "solver_type %s\n", solver_type_table[param.solver_type]); |
| 3392 |
1 |
| fprintf(fp, "nr_class %d\n", model_->nr_class); |
| 3393 |
|
| |
| 3394 |
1 |
▶ | if(model_->label) |
Branch: 1 branch
| Branch | True | False | Total |
| if(model_->label) |
1 |
0 |
1 |
|
| 3395 |
|
| { |
| 3396 |
1 |
| fprintf(fp, "label"); |
| 3397 |
3 |
▶ | for(i=0; i<model_->nr_class; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<model_->nr_class; i++) |
2 |
1 |
3 |
|
| 3398 |
2 |
| fprintf(fp, " %d", model_->label[i]); |
| 3399 |
1 |
| fprintf(fp, "\n"); |
| 3400 |
|
| } |
| 3401 |
|
| |
| 3402 |
1 |
| fprintf(fp, "nr_feature %d\n", nr_feature); |
| 3403 |
|
| |
| 3404 |
1 |
| fprintf(fp, "bias %.17g\n", model_->bias); |
| 3405 |
|
| |
| 3406 |
1 |
▶ | if(check_oneclass_model(model_)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(check_oneclass_model(model_)) |
0 |
1 |
1 |
|
| 3407 |
0 |
| fprintf(fp, "rho %.17g\n", model_->rho); |
| 3408 |
|
| |
| 3409 |
1 |
| fprintf(fp, "w\n"); |
| 3410 |
14 |
▶ | for(i=0; i<w_size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<w_size; i++) |
13 |
1 |
14 |
|
| 3411 |
|
| { |
| 3412 |
|
| int j; |
| 3413 |
26 |
▶ | for(j=0; j<nr_w; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<nr_w; j++) |
13 |
13 |
26 |
|
| 3414 |
13 |
| fprintf(fp, "%.17g ", model_->w[i*nr_w+j]); |
| 3415 |
13 |
| fprintf(fp, "\n"); |
| 3416 |
|
| } |
| 3417 |
|
| |
| 3418 |
1 |
| setlocale(LC_ALL, old_locale); |
| 3419 |
1 |
| free(old_locale); |
| 3420 |
|
| |
| 3421 |
1 |
▶ | if (ferror(fp) != 0 || fclose(fp) != 0) return -1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (ferror(fp) != 0 || fclose(fp) != 0) return -1; |
1 |
0 |
1 |
| if (ferror(fp) != 0 || fclose(fp) != 0) return -1; |
0 |
1 |
1 |
| if (ferror(fp) != 0 || fclose(fp) != 0) return -1; |
0 |
1 |
1 |
|
| 3422 |
1 |
| else return 0; |
| 3423 |
|
| } |
| 3424 |
|
| |
| 3425 |
|
| // |
| 3426 |
|
| // FSCANF helps to handle fscanf failures. |
| 3427 |
|
| // Its do-while block avoids the ambiguity when |
| 3428 |
|
| // if (...) |
| 3429 |
|
| // FSCANF(); |
| 3430 |
|
| // is used |
| 3431 |
|
| // |
| 3432 |
|
| #define FSCANF(_stream, _format, _var)do\ |
| 3433 |
|
| {\ |
| 3434 |
|
| if (fscanf(_stream, _format, _var) != 1)\ |
| 3435 |
|
| {\ |
| 3436 |
|
| fprintf(stderr, "ERROR: fscanf failed to read the model\n");\ |
| 3437 |
|
| EXIT_LOAD_MODEL()\ |
| 3438 |
|
| }\ |
| 3439 |
|
| }while(0) |
| 3440 |
|
| // EXIT_LOAD_MODEL should NOT end with a semicolon. |
| 3441 |
|
| #define EXIT_LOAD_MODEL()\ |
| 3442 |
|
| {\ |
| 3443 |
|
| setlocale(LC_ALL, old_locale);\ |
| 3444 |
|
| free(model_->label);\ |
| 3445 |
|
| free(model_);\ |
| 3446 |
|
| free(old_locale);\ |
| 3447 |
|
| return NULL;\ |
| 3448 |
|
| } |
| 3449 |
3 |
| struct model *load_model(const char *model_file_name) |
| 3450 |
|
| { |
| 3451 |
3 |
▶ | FILE *fp = fopen(model_file_name,"r"); |
Branch: 1 branch
| Branch | True | False | Total |
| FILE *fp = fopen(model_file_name,"r"); |
3 |
0 |
3 |
|
| 3452 |
3 |
▶ | if(fp==NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if(fp==NULL) return NULL; |
1 |
2 |
3 |
|
| 3453 |
|
| |
| 3454 |
|
| int i; |
| 3455 |
|
| int nr_feature; |
| 3456 |
|
| int n; |
| 3457 |
|
| int nr_class; |
| 3458 |
|
| double bias; |
| 3459 |
|
| double rho; |
| 3460 |
2 |
| model *model_ = Malloc(model,1); |
| 3461 |
2 |
| parameter& param = model_->param; |
| 3462 |
|
| // parameters for training only won't be assigned, but arrays are assigned as NULL for safety |
| 3463 |
2 |
| param.nr_weight = 0; |
| 3464 |
2 |
| param.weight_label = NULL; |
| 3465 |
2 |
| param.weight = NULL; |
| 3466 |
2 |
| param.init_sol = NULL; |
| 3467 |
|
| |
| 3468 |
2 |
| model_->label = NULL; |
| 3469 |
|
| |
| 3470 |
2 |
| char *old_locale = setlocale(LC_ALL, NULL); |
| 3471 |
2 |
▶ | if (old_locale) |
Branch: 1 branch
| Branch | True | False | Total |
| if (old_locale) |
2 |
0 |
2 |
|
| 3472 |
|
| { |
| 3473 |
2 |
| old_locale = strdup(old_locale); |
| 3474 |
|
| } |
| 3475 |
2 |
| setlocale(LC_ALL, "C"); |
| 3476 |
|
| |
| 3477 |
|
| char cmd[81]; |
| 3478 |
|
| while(1) |
| 3479 |
|
| { |
| 3480 |
12 |
▶ | FSCANF(fp,"%80s",cmd); |
Branch: 3 branches
| Branch | True | False | Total |
| FSCANF(fp,"%80s",cmd); |
12 |
0 |
12 |
| FSCANF(fp,"%80s",cmd); |
0 |
12 |
12 |
| FSCANF(fp,"%80s",cmd); |
0 |
0 |
0 |
|
| 3481 |
12 |
▶ | if(strcmp(cmd,"solver_type")==0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(strcmp(cmd,"solver_type")==0) |
2 |
10 |
12 |
|
| 3482 |
|
| { |
| 3483 |
2 |
▶ | FSCANF(fp,"%80s",cmd); |
Branch: 3 branches
| Branch | True | False | Total |
| FSCANF(fp,"%80s",cmd); |
2 |
0 |
2 |
| FSCANF(fp,"%80s",cmd); |
0 |
2 |
2 |
| FSCANF(fp,"%80s",cmd); |
0 |
0 |
0 |
|
| 3484 |
|
| int i; |
| 3485 |
5 |
▶ | for(i=0;solver_type_table[i];i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;solver_type_table[i];i++) |
5 |
0 |
5 |
|
| 3486 |
|
| { |
| 3487 |
5 |
▶ | if(strcmp(solver_type_table[i],cmd)==0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(strcmp(solver_type_table[i],cmd)==0) |
2 |
3 |
5 |
|
| 3488 |
|
| { |
| 3489 |
2 |
| param.solver_type=i; |
| 3490 |
2 |
| break; |
| 3491 |
|
| } |
| 3492 |
|
| } |
| 3493 |
2 |
▶ | if(solver_type_table[i] == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(solver_type_table[i] == NULL) |
0 |
2 |
2 |
|
| 3494 |
|
| { |
| 3495 |
0 |
▶ | fprintf(stderr,"unknown solver type.\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| fprintf(stderr,"unknown solver type.\n"); |
0 |
0 |
0 |
|
| 3496 |
0 |
| EXIT_LOAD_MODEL() |
| 3497 |
|
| } |
| 3498 |
|
| } |
| 3499 |
10 |
▶ | else if(strcmp(cmd,"nr_class")==0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(strcmp(cmd,"nr_class")==0) |
2 |
8 |
10 |
|
| 3500 |
|
| { |
| 3501 |
2 |
▶ | FSCANF(fp,"%d",&nr_class); |
Branch: 3 branches
| Branch | True | False | Total |
| FSCANF(fp,"%d",&nr_class); |
2 |
0 |
2 |
| FSCANF(fp,"%d",&nr_class); |
0 |
2 |
2 |
| FSCANF(fp,"%d",&nr_class); |
0 |
0 |
0 |
|
| 3502 |
2 |
| model_->nr_class=nr_class; |
| 3503 |
|
| } |
| 3504 |
8 |
▶ | else if(strcmp(cmd,"nr_feature")==0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(strcmp(cmd,"nr_feature")==0) |
2 |
6 |
8 |
|
| 3505 |
|
| { |
| 3506 |
2 |
▶ | FSCANF(fp,"%d",&nr_feature); |
Branch: 3 branches
| Branch | True | False | Total |
| FSCANF(fp,"%d",&nr_feature); |
2 |
0 |
2 |
| FSCANF(fp,"%d",&nr_feature); |
0 |
2 |
2 |
| FSCANF(fp,"%d",&nr_feature); |
0 |
0 |
0 |
|
| 3507 |
2 |
| model_->nr_feature=nr_feature; |
| 3508 |
|
| } |
| 3509 |
6 |
▶ | else if(strcmp(cmd,"bias")==0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(strcmp(cmd,"bias")==0) |
2 |
4 |
6 |
|
| 3510 |
|
| { |
| 3511 |
2 |
▶ | FSCANF(fp,"%lf",&bias); |
Branch: 3 branches
| Branch | True | False | Total |
| FSCANF(fp,"%lf",&bias); |
2 |
0 |
2 |
| FSCANF(fp,"%lf",&bias); |
0 |
2 |
2 |
| FSCANF(fp,"%lf",&bias); |
0 |
0 |
0 |
|
| 3512 |
2 |
| model_->bias=bias; |
| 3513 |
|
| } |
| 3514 |
4 |
▶ | else if(strcmp(cmd,"rho")==0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(strcmp(cmd,"rho")==0) |
0 |
4 |
4 |
|
| 3515 |
|
| { |
| 3516 |
0 |
▶ | FSCANF(fp,"%lf",&rho); |
Branch: 3 branches
| Branch | True | False | Total |
| FSCANF(fp,"%lf",&rho); |
0 |
0 |
0 |
| FSCANF(fp,"%lf",&rho); |
0 |
0 |
0 |
| FSCANF(fp,"%lf",&rho); |
0 |
0 |
0 |
|
| 3517 |
0 |
| model_->rho=rho; |
| 3518 |
|
| } |
| 3519 |
4 |
▶ | else if(strcmp(cmd,"w")==0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(strcmp(cmd,"w")==0) |
2 |
2 |
4 |
|
| 3520 |
|
| { |
| 3521 |
2 |
| break; |
| 3522 |
|
| } |
| 3523 |
2 |
▶ | else if(strcmp(cmd,"label")==0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(strcmp(cmd,"label")==0) |
2 |
0 |
2 |
|
| 3524 |
|
| { |
| 3525 |
2 |
| int nr_class = model_->nr_class; |
| 3526 |
2 |
| model_->label = Malloc(int,nr_class); |
| 3527 |
7 |
▶ | for(int i=0;i<nr_class;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i=0;i<nr_class;i++) |
5 |
2 |
7 |
|
| 3528 |
5 |
▶ | FSCANF(fp,"%d",&model_->label[i]); |
Branch: 3 branches
| Branch | True | False | Total |
| FSCANF(fp,"%d",&model_->label[i]); |
5 |
0 |
5 |
| FSCANF(fp,"%d",&model_->label[i]); |
0 |
5 |
5 |
| FSCANF(fp,"%d",&model_->label[i]); |
0 |
0 |
0 |
|
| 3529 |
|
| } |
| 3530 |
|
| else |
| 3531 |
|
| { |
| 3532 |
0 |
▶ | fprintf(stderr,"unknown text in model file: [%s]\n",cmd); |
Branch: 1 branch
| Branch | True | False | Total |
| fprintf(stderr,"unknown text in model file: [%s]\n",cmd); |
0 |
0 |
0 |
|
| 3533 |
0 |
| EXIT_LOAD_MODEL() |
| 3534 |
|
| } |
| 3535 |
10 |
| } |
| 3536 |
|
| |
| 3537 |
2 |
| nr_feature=model_->nr_feature; |
| 3538 |
2 |
▶ | if(model_->bias>=0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(model_->bias>=0) |
1 |
1 |
2 |
|
| 3539 |
1 |
| n=nr_feature+1; |
| 3540 |
|
| else |
| 3541 |
1 |
| n=nr_feature; |
| 3542 |
2 |
| int w_size = n; |
| 3543 |
|
| int nr_w; |
| 3544 |
2 |
▶ | if(nr_class==2 && param.solver_type != MCSVM_CS) |
Branch: 2 branches
| Branch | True | False | Total |
| if(nr_class==2 && param.solver_type != MCSVM_CS) |
1 |
1 |
2 |
| if(nr_class==2 && param.solver_type != MCSVM_CS) |
1 |
0 |
1 |
|
| 3545 |
1 |
| nr_w = 1; |
| 3546 |
|
| else |
| 3547 |
1 |
| nr_w = nr_class; |
| 3548 |
|
| |
| 3549 |
2 |
| model_->w=Malloc(double, w_size*nr_w); |
| 3550 |
20 |
▶ | for(i=0; i<w_size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<w_size; i++) |
18 |
2 |
20 |
|
| 3551 |
|
| { |
| 3552 |
|
| int j; |
| 3553 |
46 |
▶ | for(j=0; j<nr_w; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<nr_w; j++) |
28 |
18 |
46 |
|
| 3554 |
28 |
▶ | FSCANF(fp, "%lf ", &model_->w[i*nr_w+j]); |
Branch: 3 branches
| Branch | True | False | Total |
| FSCANF(fp, "%lf ", &model_->w[i*nr_w+j]); |
28 |
0 |
28 |
| FSCANF(fp, "%lf ", &model_->w[i*nr_w+j]); |
0 |
28 |
28 |
| FSCANF(fp, "%lf ", &model_->w[i*nr_w+j]); |
0 |
0 |
0 |
|
| 3555 |
|
| } |
| 3556 |
|
| |
| 3557 |
2 |
| setlocale(LC_ALL, old_locale); |
| 3558 |
2 |
| free(old_locale); |
| 3559 |
|
| |
| 3560 |
2 |
▶ | if (ferror(fp) != 0 || fclose(fp) != 0) return NULL; |
Branch: 4 branches
| Branch | True | False | Total |
| if (ferror(fp) != 0 || fclose(fp) != 0) return NULL; |
2 |
0 |
2 |
| if (ferror(fp) != 0 || fclose(fp) != 0) return NULL; |
2 |
0 |
2 |
| if (ferror(fp) != 0 || fclose(fp) != 0) return NULL; |
0 |
2 |
2 |
| if (ferror(fp) != 0 || fclose(fp) != 0) return NULL; |
0 |
2 |
2 |
|
| 3561 |
|
| |
| 3562 |
2 |
| return model_; |
| 3563 |
|
| } |
| 3564 |
|
| |
| 3565 |
3 |
| int get_nr_feature(const model *model_) |
| 3566 |
|
| { |
| 3567 |
3 |
| return model_->nr_feature; |
| 3568 |
|
| } |
| 3569 |
|
| |
| 3570 |
7 |
| int get_nr_class(const model *model_) |
| 3571 |
|
| { |
| 3572 |
7 |
| return model_->nr_class; |
| 3573 |
|
| } |
| 3574 |
|
| |
| 3575 |
0 |
| void get_labels(const model *model_, int* label) |
| 3576 |
|
| { |
| 3577 |
0 |
▶ | if (model_->label != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (model_->label != NULL) |
0 |
0 |
0 |
|
| 3578 |
0 |
▶ | for(int i=0;i<model_->nr_class;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i=0;i<model_->nr_class;i++) |
0 |
0 |
0 |
|
| 3579 |
0 |
| label[i] = model_->label[i]; |
| 3580 |
0 |
| } |
| 3581 |
|
| |
| 3582 |
|
| // use inline here for better performance (around 20% faster than the non-inline one) |
| 3583 |
15 |
| static inline double get_w_value(const struct model *model_, int idx, int label_idx) |
| 3584 |
|
| { |
| 3585 |
15 |
| int nr_class = model_->nr_class; |
| 3586 |
15 |
| int solver_type = model_->param.solver_type; |
| 3587 |
15 |
| const double *w = model_->w; |
| 3588 |
|
| |
| 3589 |
15 |
▶ | if(idx < 0 || idx > model_->nr_feature) |
Branch: 2 branches
| Branch | True | False | Total |
| if(idx < 0 || idx > model_->nr_feature) |
15 |
0 |
15 |
| if(idx < 0 || idx > model_->nr_feature) |
0 |
15 |
15 |
|
| 3590 |
0 |
| return 0; |
| 3591 |
15 |
▶ | if(check_regression_model(model_) || check_oneclass_model(model_)) |
Branch: 3 branches
| Branch | True | False | Total |
| if(check_regression_model(model_) || check_oneclass_model(model_)) |
15 |
0 |
15 |
| if(check_regression_model(model_) || check_oneclass_model(model_)) |
0 |
15 |
15 |
| if(check_regression_model(model_) || check_oneclass_model(model_)) |
0 |
15 |
15 |
|
| 3592 |
0 |
| return w[idx]; |
| 3593 |
|
| else |
| 3594 |
|
| { |
| 3595 |
15 |
▶ | if(label_idx < 0 || label_idx >= nr_class) |
Branch: 2 branches
| Branch | True | False | Total |
| if(label_idx < 0 || label_idx >= nr_class) |
15 |
0 |
15 |
| if(label_idx < 0 || label_idx >= nr_class) |
0 |
15 |
15 |
|
| 3596 |
0 |
| return 0; |
| 3597 |
15 |
▶ | if(nr_class == 2 && solver_type != MCSVM_CS) |
Branch: 2 branches
| Branch | True | False | Total |
| if(nr_class == 2 && solver_type != MCSVM_CS) |
0 |
15 |
15 |
| if(nr_class == 2 && solver_type != MCSVM_CS) |
0 |
0 |
0 |
|
| 3598 |
|
| { |
| 3599 |
0 |
▶ | if(label_idx == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(label_idx == 0) |
0 |
0 |
0 |
|
| 3600 |
0 |
| return w[idx]; |
| 3601 |
|
| else |
| 3602 |
0 |
| return -w[idx]; |
| 3603 |
|
| } |
| 3604 |
|
| else |
| 3605 |
15 |
| return w[idx*nr_class+label_idx]; |
| 3606 |
|
| } |
| 3607 |
|
| } |
| 3608 |
|
| |
| 3609 |
|
| // feat_idx: starting from 1 to nr_feature |
| 3610 |
|
| // label_idx: starting from 0 to nr_class-1 for classification models; |
| 3611 |
|
| // for regression and one-class SVM models, label_idx is |
| 3612 |
|
| // ignored. |
| 3613 |
12 |
| double get_decfun_coef(const struct model *model_, int feat_idx, int label_idx) |
| 3614 |
|
| { |
| 3615 |
12 |
▶ | if(feat_idx > model_->nr_feature) |
Branch: 1 branch
| Branch | True | False | Total |
| if(feat_idx > model_->nr_feature) |
0 |
12 |
12 |
|
| 3616 |
0 |
| return 0; |
| 3617 |
12 |
| return get_w_value(model_, feat_idx-1, label_idx); |
| 3618 |
|
| } |
| 3619 |
|
| |
| 3620 |
3 |
| double get_decfun_bias(const struct model *model_, int label_idx) |
| 3621 |
|
| { |
| 3622 |
3 |
▶ | if(check_oneclass_model(model_)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(check_oneclass_model(model_)) |
0 |
3 |
3 |
|
| 3623 |
|
| { |
| 3624 |
0 |
| fprintf(stderr, "ERROR: get_decfun_bias can not be called for a one-class SVM model\n"); |
| 3625 |
0 |
| return 0; |
| 3626 |
|
| } |
| 3627 |
3 |
| int bias_idx = model_->nr_feature; |
| 3628 |
3 |
| double bias = model_->bias; |
| 3629 |
3 |
▶ | if(bias <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(bias <= 0) |
0 |
3 |
3 |
|
| 3630 |
0 |
| return 0; |
| 3631 |
|
| else |
| 3632 |
3 |
| return bias*get_w_value(model_, bias_idx, label_idx); |
| 3633 |
|
| } |
| 3634 |
|
| |
| 3635 |
0 |
| double get_decfun_rho(const struct model *model_) |
| 3636 |
|
| { |
| 3637 |
0 |
▶ | if(check_oneclass_model(model_)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(check_oneclass_model(model_)) |
0 |
0 |
0 |
|
| 3638 |
0 |
| return model_->rho; |
| 3639 |
|
| else |
| 3640 |
|
| { |
| 3641 |
0 |
| fprintf(stderr, "ERROR: get_decfun_rho can be called only for a one-class SVM model\n"); |
| 3642 |
0 |
| return 0; |
| 3643 |
|
| } |
| 3644 |
|
| } |
| 3645 |
|
| |
| 3646 |
80 |
| void free_model_content(struct model *model_ptr) |
| 3647 |
|
| { |
| 3648 |
80 |
| free(model_ptr->w); |
| 3649 |
80 |
| model_ptr->w = NULL; |
| 3650 |
80 |
| free(model_ptr->label); |
| 3651 |
80 |
| model_ptr->label = NULL; |
| 3652 |
80 |
| } |
| 3653 |
|
| |
| 3654 |
80 |
| void free_and_destroy_model(struct model **model_ptr_ptr) |
| 3655 |
|
| { |
| 3656 |
80 |
| struct model *model_ptr = *model_ptr_ptr; |
| 3657 |
80 |
▶ | if(model_ptr != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(model_ptr != NULL) |
80 |
0 |
80 |
|
| 3658 |
|
| { |
| 3659 |
80 |
| free_model_content(model_ptr); |
| 3660 |
80 |
| free(model_ptr); |
| 3661 |
80 |
| *model_ptr_ptr = NULL; |
| 3662 |
|
| } |
| 3663 |
80 |
| } |
| 3664 |
|
| |
| 3665 |
0 |
| void destroy_param(parameter* param) |
| 3666 |
|
| { |
| 3667 |
0 |
| free(param->weight_label); |
| 3668 |
0 |
| param->weight_label = NULL; |
| 3669 |
0 |
| free(param->weight); |
| 3670 |
0 |
| param->weight = NULL; |
| 3671 |
0 |
| free(param->init_sol); |
| 3672 |
0 |
| param->init_sol = NULL; |
| 3673 |
0 |
| } |
| 3674 |
|
| |
| 3675 |
10 |
| const char *check_parameter(const problem *prob, const parameter *param) |
| 3676 |
|
| { |
| 3677 |
10 |
▶ | if(param->eps <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(param->eps <= 0) |
2 |
8 |
10 |
|
| 3678 |
2 |
| return "eps <= 0"; |
| 3679 |
|
| |
| 3680 |
8 |
▶ | if(param->C <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(param->C <= 0) |
2 |
6 |
8 |
|
| 3681 |
2 |
| return "C <= 0"; |
| 3682 |
|
| |
| 3683 |
6 |
▶ | if(param->p < 0 && param->solver_type == L2R_L2LOSS_SVR) |
Branch: 2 branches
| Branch | True | False | Total |
| if(param->p < 0 && param->solver_type == L2R_L2LOSS_SVR) |
2 |
4 |
6 |
| if(param->p < 0 && param->solver_type == L2R_L2LOSS_SVR) |
2 |
0 |
2 |
|
| 3684 |
2 |
| return "p < 0"; |
| 3685 |
|
| |
| 3686 |
4 |
▶ | if(prob->bias >= 0 && param->solver_type == ONECLASS_SVM) |
Branch: 2 branches
| Branch | True | False | Total |
| if(prob->bias >= 0 && param->solver_type == ONECLASS_SVM) |
0 |
4 |
4 |
| if(prob->bias >= 0 && param->solver_type == ONECLASS_SVM) |
0 |
0 |
0 |
|
| 3687 |
0 |
| return "prob->bias >=0, but this is ignored in ONECLASS_SVM"; |
| 3688 |
|
| |
| 3689 |
4 |
▶ | if(param->regularize_bias == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(param->regularize_bias == 0) |
0 |
4 |
4 |
|
| 3690 |
|
| { |
| 3691 |
0 |
▶ | if(prob->bias != 1.0) |
Branch: 1 branch
| Branch | True | False | Total |
| if(prob->bias != 1.0) |
0 |
0 |
0 |
|
| 3692 |
0 |
| return "To not regularize bias, must specify -B 1 along with -R"; |
| 3693 |
0 |
▶ | if(param->solver_type != L2R_LR |
Branch: 1 branch
| Branch | True | False | Total |
| if(param->solver_type != L2R_LR |
0 |
0 |
0 |
|
| 3694 |
0 |
▶ | && param->solver_type != L2R_L2LOSS_SVC |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != L2R_L2LOSS_SVC |
0 |
0 |
0 |
|
| 3695 |
0 |
▶ | && param->solver_type != L1R_L2LOSS_SVC |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != L1R_L2LOSS_SVC |
0 |
0 |
0 |
|
| 3696 |
0 |
▶ | && param->solver_type != L1R_LR |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != L1R_LR |
0 |
0 |
0 |
|
| 3697 |
0 |
▶ | && param->solver_type != L2R_L2LOSS_SVR) |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != L2R_L2LOSS_SVR) |
0 |
0 |
0 |
|
| 3698 |
0 |
| return "-R option supported only for solver L2R_LR, L2R_L2LOSS_SVC, L1R_L2LOSS_SVC, L1R_LR, and L2R_L2LOSS_SVR"; |
| 3699 |
|
| } |
| 3700 |
|
| |
| 3701 |
4 |
▶ | if(param->solver_type != L2R_LR |
Branch: 1 branch
| Branch | True | False | Total |
| if(param->solver_type != L2R_LR |
4 |
0 |
4 |
|
| 3702 |
4 |
▶ | && param->solver_type != L2R_L2LOSS_SVC_DUAL |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != L2R_L2LOSS_SVC_DUAL |
2 |
2 |
4 |
|
| 3703 |
2 |
▶ | && param->solver_type != L2R_L2LOSS_SVC |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != L2R_L2LOSS_SVC |
0 |
2 |
2 |
|
| 3704 |
0 |
▶ | && param->solver_type != L2R_L1LOSS_SVC_DUAL |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != L2R_L1LOSS_SVC_DUAL |
0 |
0 |
0 |
|
| 3705 |
0 |
▶ | && param->solver_type != MCSVM_CS |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != MCSVM_CS |
0 |
0 |
0 |
|
| 3706 |
0 |
▶ | && param->solver_type != L1R_L2LOSS_SVC |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != L1R_L2LOSS_SVC |
0 |
0 |
0 |
|
| 3707 |
0 |
▶ | && param->solver_type != L1R_LR |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != L1R_LR |
0 |
0 |
0 |
|
| 3708 |
0 |
▶ | && param->solver_type != L2R_LR_DUAL |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != L2R_LR_DUAL |
0 |
0 |
0 |
|
| 3709 |
0 |
▶ | && param->solver_type != L2R_L2LOSS_SVR |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != L2R_L2LOSS_SVR |
0 |
0 |
0 |
|
| 3710 |
0 |
▶ | && param->solver_type != L2R_L2LOSS_SVR_DUAL |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != L2R_L2LOSS_SVR_DUAL |
0 |
0 |
0 |
|
| 3711 |
0 |
▶ | && param->solver_type != L2R_L1LOSS_SVR_DUAL |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != L2R_L1LOSS_SVR_DUAL |
0 |
0 |
0 |
|
| 3712 |
0 |
▶ | && param->solver_type != ONECLASS_SVM) |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != ONECLASS_SVM) |
0 |
0 |
0 |
|
| 3713 |
0 |
| return "unknown solver type"; |
| 3714 |
|
| |
| 3715 |
4 |
▶ | if(param->init_sol != NULL |
Branch: 1 branch
| Branch | True | False | Total |
| if(param->init_sol != NULL |
0 |
4 |
4 |
|
| 3716 |
0 |
▶ | && param->solver_type != L2R_LR |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != L2R_LR |
0 |
0 |
0 |
|
| 3717 |
0 |
▶ | && param->solver_type != L2R_L2LOSS_SVC |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != L2R_L2LOSS_SVC |
0 |
0 |
0 |
|
| 3718 |
0 |
▶ | && param->solver_type != L2R_L2LOSS_SVR) |
Branch: 1 branch
| Branch | True | False | Total |
| && param->solver_type != L2R_L2LOSS_SVR) |
0 |
0 |
0 |
|
| 3719 |
0 |
| return "Initial-solution specification supported only for solvers L2R_LR, L2R_L2LOSS_SVC, and L2R_L2LOSS_SVR"; |
| 3720 |
|
| |
| 3721 |
4 |
| return NULL; |
| 3722 |
|
| } |
| 3723 |
|
| |
| 3724 |
2 |
| int check_probability_model(const struct model *model_) |
| 3725 |
|
| { |
| 3726 |
4 |
| return (model_->param.solver_type==L2R_LR || |
| 3727 |
4 |
▶ | model_->param.solver_type==L2R_LR_DUAL || |
Branch: 2 branches
| Branch | True | False | Total |
| model_->param.solver_type==L2R_LR_DUAL || |
2 |
0 |
2 |
| model_->param.solver_type==L2R_LR_DUAL || |
2 |
0 |
2 |
|
| 3728 |
4 |
▶ | model_->param.solver_type==L1R_LR); |
Branch: 1 branch
| Branch | True | False | Total |
| model_->param.solver_type==L1R_LR); |
0 |
2 |
2 |
|
| 3729 |
|
| } |
| 3730 |
|
| |
| 3731 |
4145 |
| int check_regression_model(const struct model *model_) |
| 3732 |
|
| { |
| 3733 |
8290 |
| return (model_->param.solver_type==L2R_L2LOSS_SVR || |
| 3734 |
8290 |
▶ | model_->param.solver_type==L2R_L1LOSS_SVR_DUAL || |
Branch: 2 branches
| Branch | True | False | Total |
| model_->param.solver_type==L2R_L1LOSS_SVR_DUAL || |
4145 |
0 |
4145 |
| model_->param.solver_type==L2R_L1LOSS_SVR_DUAL || |
4145 |
0 |
4145 |
|
| 3735 |
8290 |
▶ | model_->param.solver_type==L2R_L2LOSS_SVR_DUAL); |
Branch: 1 branch
| Branch | True | False | Total |
| model_->param.solver_type==L2R_L2LOSS_SVR_DUAL); |
0 |
4145 |
4145 |
|
| 3736 |
|
| } |
| 3737 |
|
| |
| 3738 |
8203 |
| int check_oneclass_model(const struct model *model_) |
| 3739 |
|
| { |
| 3740 |
8203 |
| return model_->param.solver_type == ONECLASS_SVM; |
| 3741 |
|
| } |
| 3742 |
|
| |
| 3743 |
34 |
| void set_print_string_function(void (*print_func)(const char*)) |
| 3744 |
|
| { |
| 3745 |
34 |
▶ | if (print_func == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (print_func == NULL) |
0 |
34 |
34 |
|
| 3746 |
0 |
| liblinear_print_string = &print_string_stdout; |
| 3747 |
|
| else |
| 3748 |
34 |
| liblinear_print_string = print_func; |
| 3749 |
34 |
| } |
| 3750 |
|
| |