34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
48 using namespace shogun;
50 l2r_lr_fun::l2r_lr_fun(
const problem *p,
float64_t* Cs)
61 l2r_lr_fun::~l2r_lr_fun()
68 double l2r_lr_fun::fun(
double *w)
79 double yz = y[i]*z[i];
81 f += C[i]*log(1 + exp(-yz));
83 f += C[i]*(-yz+log(1 + exp(yz)));
90 void l2r_lr_fun::grad(
double *w,
double *g)
95 int w_size=get_nr_variable();
99 z[i] = 1/(1 + exp(-y[i]*z[i]));
100 D[i] = z[i]*(1-z[i]);
101 z[i] = C[i]*(z[i]-1)*y[i];
105 for(i=0;i<w_size;i++)
109 int l2r_lr_fun::get_nr_variable()
114 void l2r_lr_fun::Hv(
double *s,
double *Hs)
118 int w_size=get_nr_variable();
123 wa[i] = C[i]*D[i]*wa[i];
126 for(i=0;i<w_size;i++)
127 Hs[i] = s[i] + Hs[i];
131 void l2r_lr_fun::Xv(
double *v,
double *res_Xv)
137 if (m_prob->use_bias)
143 m_prob->x->dense_dot_range(res_Xv, 0, l, NULL, v, n, bias);
146 void l2r_lr_fun::XTv(
double *v,
double *res_XTv)
151 memset(res_XTv, 0,
sizeof(
double)*m_prob->n);
153 if (m_prob->use_bias)
156 for (int32_t i=0;i<l;i++)
158 m_prob->x->add_to_dense_vec(v[i], i, res_XTv, n);
160 if (m_prob->use_bias)
165 l2r_l2_svc_fun::l2r_l2_svc_fun(
const problem *p,
double* Cs)
178 l2r_l2_svc_fun::~l2r_l2_svc_fun()
185 double l2r_l2_svc_fun::fun(
double *w)
191 int w_size=get_nr_variable();
206 void l2r_l2_svc_fun::grad(
double *w,
double *g)
211 int w_size=get_nr_variable();
217 z[sizeI] = C[i]*y[i]*(z[i]-1);
223 for(i=0;i<w_size;i++)
224 g[i] = w[i] + 2*g[i];
227 int l2r_l2_svc_fun::get_nr_variable()
232 void l2r_l2_svc_fun::Hv(
double *s,
double *Hs)
236 int w_size=get_nr_variable();
241 wa[i] = C[I[i]]*wa[i];
244 for(i=0;i<w_size;i++)
245 Hs[i] = s[i] + 2*Hs[i];
249 void l2r_l2_svc_fun::Xv(
double *v,
double *res_Xv)
255 if (m_prob->use_bias)
261 m_prob->x->dense_dot_range(res_Xv, 0, l, NULL, v, n, bias);
264 void l2r_l2_svc_fun::subXv(
double *v,
double *res_Xv)
269 if (m_prob->use_bias)
275 m_prob->x->dense_dot_range_subset(I, sizeI, res_Xv, NULL, v, n, bias);
286 void l2r_l2_svc_fun::subXTv(
double *v,
double *XTv)
290 if (m_prob->use_bias)
293 memset(XTv, 0,
sizeof(
float64_t)*m_prob->n);
294 for (int32_t i=0;i<sizeI;i++)
296 m_prob->x->add_to_dense_vec(v[i], I[i], XTv, n);
298 if (m_prob->use_bias)
303 l2r_l2_svr_fun::l2r_l2_svr_fun(
const problem *prob,
double *Cs,
double p):
304 l2r_l2_svc_fun(prob, Cs)
309 double l2r_l2_svr_fun::fun(
double *w)
315 int w_size=get_nr_variable();
320 for(i=0;i<w_size;i++)
327 f += C[i]*(d+m_p)*(d+m_p);
329 f += C[i]*(d-m_p)*(d-m_p);
335 void l2r_l2_svr_fun::grad(
double *w,
double *g)
340 int w_size=get_nr_variable();
351 z[sizeI] = C[i]*(d+m_p);
357 z[sizeI] = C[i]*(d-m_p);
365 for(i=0;i<w_size;i++)
366 g[i] = w[i] + 2*g[i];
388 #define GETI(i) (prob->y[i])
391 Solver_MCSVM_CS::Solver_MCSVM_CS(
const problem *p,
int n_class,
392 double *weighted_C,
double *w0_reg,
393 double epsilon,
int max_it,
double max_time,
394 mcsvm_state* given_state)
398 this->nr_class = n_class;
400 this->max_iter = max_it;
402 this->C = weighted_C;
404 this->max_train_time = max_time;
405 this->state = given_state;
408 Solver_MCSVM_CS::~Solver_MCSVM_CS()
412 int compare_double(
const void *a,
const void *b)
414 if(*(
double *)a > *(
double *)b)
416 if(*(
double *)a < *(
double *)b)
421 void Solver_MCSVM_CS::solve_sub_problem(
double A_i,
int yi,
double C_yi,
int active_i,
double *alpha_new)
428 qsort(D, active_i,
sizeof(
double), compare_double);
430 double beta = D[0] - A_i*C_yi;
431 for(r=1;r<active_i && beta<r*D[r];r++)
435 for(r=0;r<active_i;r++)
438 alpha_new[r] =
CMath::min(C_yi, (beta-state->B[r])/A_i);
440 alpha_new[r] =
CMath::min((
double)0, (beta - state->B[r])/A_i);
445 bool Solver_MCSVM_CS::be_shrunk(
int i,
int m,
int yi,
double alpha_i,
double minG)
449 bound = C[int32_t(
GETI(i))];
450 if(alpha_i == bound && state->G[m] < minG)
455 void Solver_MCSVM_CS::solve()
459 double *w,*B,*G,*alpha,*alpha_new,*QD,*d_val;
460 int *index,*d_ind,*alpha_index,*y_index,*active_size_i;
462 if (!state->allocated)
464 state->w =
SG_CALLOC(
double, nr_class*w_size);
467 state->alpha =
SG_CALLOC(
double, l*nr_class);
468 state->alpha_new =
SG_CALLOC(
double, nr_class);
472 state->d_val =
SG_CALLOC(
double, nr_class);
473 state->alpha_index =
SG_CALLOC(
int, nr_class*l);
475 state->active_size_i =
SG_CALLOC(
int, l);
476 state->allocated =
true;
481 alpha = state->alpha;
482 alpha_new = state->alpha_new;
483 index = state->index;
485 d_ind = state->d_ind;
486 d_val = state->d_val;
487 alpha_index = state->alpha_index;
488 y_index = state->y_index;
489 active_size_i = state->active_size_i;
492 int dim = prob->x->get_dim_feature_space();
495 double eps_shrink =
CMath::max(10.0*eps, 1.0);
496 bool start_from_all =
true;
503 for(m=0;m<nr_class;m++)
504 alpha_index[i*nr_class+m] = m;
506 QD[i] = prob->x->dot(i, prob->x,i);
510 active_size_i[i] = nr_class;
511 y_index[i] = prob->y[i];
514 state->inited =
true;
520 for(i=0;i<active_size;i++)
522 int j = i+rand()%(active_size-i);
525 for(s=0;s<active_size;s++)
529 double *alpha_i = &alpha[i*nr_class];
530 int *alpha_index_i = &alpha_index[i*nr_class];
534 for(m=0;m<active_size_i[i];m++)
536 if(y_index[i] < active_size_i[i])
539 memset(tx,0,dim*
sizeof(
double));
540 prob->x->add_to_dense_vec(1.0,i,tx,dim);
541 for (k=0; k<dim; k++)
546 double* w_i = &w[k*nr_class];
547 for (m=0; m<active_size_i[i]; m++)
548 G[m] += w_i[alpha_index_i[m]]*tx[k];
555 double *w_i = &w[(w_size-1)*nr_class];
556 for(m=0; m<active_size_i[i]; m++)
557 G[m] += w_i[alpha_index_i[m]];
561 for (k=0; k<dim; k++)
563 double *w0_i = &w0[k*nr_class];
564 for(m=0; m<active_size_i[i]; m++)
565 G[m] += w0_i[alpha_index_i[m]];
572 for(m=0;m<active_size_i[i];m++)
574 if(alpha_i[alpha_index_i[m]] < 0 && G[m] < minG)
579 if(y_index[i] < active_size_i[i])
580 if(alpha_i[int32_t(prob->y[i])] < C[int32_t(
GETI(i))] && G[y_index[i]] < minG)
581 minG = G[y_index[i]];
583 for(m=0;m<active_size_i[i];m++)
585 if(be_shrunk(i, m, y_index[i], alpha_i[alpha_index_i[m]], minG))
588 while(active_size_i[i]>m)
590 if(!be_shrunk(i, active_size_i[i], y_index[i],
591 alpha_i[alpha_index_i[active_size_i[i]]], minG))
593 CMath::swap(alpha_index_i[m], alpha_index_i[active_size_i[i]]);
595 if(y_index[i] == active_size_i[i])
597 else if(y_index[i] == m)
598 y_index[i] = active_size_i[i];
606 if(active_size_i[i] <= 1)
614 if(maxG-minG <= 1e-12)
619 for(m=0;m<active_size_i[i];m++)
620 B[m] = G[m] - Ai*alpha_i[alpha_index_i[m]] ;
622 solve_sub_problem(Ai, y_index[i], C[int32_t(
GETI(i))], active_size_i[i], alpha_new);
624 for(m=0;m<active_size_i[i];m++)
626 double d = alpha_new[m] - alpha_i[alpha_index_i[m]];
627 alpha_i[alpha_index_i[m]] = alpha_new[m];
630 d_ind[nz_d] = alpha_index_i[m];
636 memset(tx,0,dim*
sizeof(
double));
637 prob->x->add_to_dense_vec(1.0,i,tx,dim);
638 for (k=0; k<dim; k++)
643 double* w_i = &w[k*nr_class];
644 for (m=0; m<nz_d; m++)
645 w_i[d_ind[m]] += d_val[m]*tx[k];
651 double *w_i = &w[(w_size-1)*nr_class];
653 w_i[d_ind[m]] += d_val[m];
667 if(stopping < eps_shrink)
669 if(stopping < eps && start_from_all ==
true)
675 active_size_i[i] = nr_class;
678 start_from_all =
true;
682 start_from_all =
false;
684 if (max_train_time!=0.0 && max_train_time < start_time.
cur_time_diff())
688 SG_SINFO(
"\noptimization finished, #iter = %d\n",iter);
689 if (iter >= max_iter)
690 SG_SINFO(
"Warning: reaching max number of iterations\n");
699 void destroy_model(
struct model *model_)
706 void destroy_param(parameter* param)
712 #endif // DOXYGEN_SHOULD_SKIP_THIS