void selection_rank_fun::XTv(double *v, double *XTv) { int i; int l = prob->l; int w_size=get_nr_variable(); /*//#pragma omp parallel for default(shared) private(i) for(i=0;i<w_size;i++) { XTv[i] = 0.0; } //#pragma omp parallel for default(shared) private(i) schedule(CSCHED) for(i=0;i<l;i++) { feature_node *sx=prob->x[i]; while(sx->index!=-1) { //#pragma omp atomic XTv[sx->index-1] += v[i]*sx->value; sx++; } }*/ #pragma omp parallel for private(i) for(i=0;i<w_size;i++) XTv[i]=0.0; if(BLAS_dusmv(blas_no_trans, 1, spr_XT, v, 1, XTv, 1)) printf("dusmv XTv error\n"); }
void selection_rank_fun::grad(double *w, double *g) { int i; int l=prob->l; double *ATAXw; ATAXw = new double[l]; int w_size = get_nr_variable(); double begin, end; #pragma omp parallel for default(shared) private(i) for (i=0;i<l;i++) ATAXw[i]=(double)l_plus[i]-(double)l_minus[i]+((double)l_plus[i]+(double)l_minus[i])*z[i]-alpha_plus[i]-alpha_minus[i]; begin = omp_get_wtime(); XTv(ATAXw, g); end = omp_get_wtime(); XTviter++; XTvtime += end-begin; #pragma omp parallel for default(shared) private(i) for(i=0;i<w_size;i++) g[i] = w[i] + 2*C*g[i]; delete[] ATAXw; }
void selection_rank_fun::sample_Hv(double *s, double *Hs, long int *num_data) { int i,j,k; int w_size=get_nr_variable(); double *wa = new double[totalS]; selectiontree *T; double* alpha_plus_minus; alpha_plus_minus = new double[totalS]; sample_subXv(s, wa); *num_data += totalS; for (i=0;i<nr_subset;i++) { T=new selectiontree(nr_class[i]); k=0; for (j=0;j<sizeS[i];j++) { while (k<count[i]&&(1-Spi[i][j].value+Spi[i][k].value>0)) { T->insert_node(int_y[Spi[i][k].id],wa[startS[i]+j]); k++; } alpha_plus_minus[startS[i]+j]=T->vector_sum_smaller(int_y[Spi[i][j].id]); } delete T; k=sizeS[i]-1; T = new selectiontree(nr_class[i]); for (j=sizeS[i]-1;j>=0;j--) { while (k>=0&&(1+Spi[i][j].value-Spi[i][k].value>0)) { T->insert_node(int_y[Spi[i][k].id],wa[startS[i]+j]); k--; } alpha_plus_minus[startS[i]+j]+=T->vector_sum_larger(int_y[Spi[i][j].id]); } delete T; } for(i=0;i<nr_subset;i++) { for(j=0;j<sizeS[i];j++) { wa[startS[i]+j]=wa[startS[i]+j]*((double)l_plus[Spi[i][j].id]+(double)l_minus[Spi[i][j].id])-alpha_plus_minus[startS[i]+j]; } } delete[] alpha_plus_minus; sample_subXTv(wa, Hs); *num_data += totalS; delete[] wa; for(i=0;i<w_size;i++) Hs[i] = s[i] + 2*C*Hs[i]; }
double l2r_l2_ranksvm_fun::fun(double *w) { int q,i,j; double f = 0; int l = prob->l; int w_size = get_nr_variable(); selectiontree *T; Xv(w,z); for (q=0;q<nr_query;q++) { int *perm_q = &perm[start[q]]; id_and_value *order_perm_q = order_perm[q]; for (i=0;i<count[q];i++) { order_perm_q[i].id = perm_q[i]; order_perm_q[i].value = z[perm_q[i]]; } qsort(order_perm_q, count[q], sizeof(id_and_value), compare_values); T = new selectiontree(nr_class[q]); j = 0; for (i=0;i<count[q];i++) { while (j<count[q] && (1 - order_perm_q[j].value + order_perm_q[i].value>0)) { T->insert_node(int_y[order_perm_q[j].id], order_perm_q[j].value); j++; } T->larger(int_y[order_perm_q[i].id], &l_plus[order_perm_q[i].id], &alpha_plus[order_perm_q[i].id]); } delete T; j = count[q] - 1; T = new selectiontree(nr_class[q]); for (i=count[q]-1;i>=0;i--) { while (j>=0 && (1 - order_perm_q[i].value + order_perm_q[j].value>0)) { T->insert_node(int_y[order_perm_q[j].id], order_perm_q[j].value); j--; } T->smaller(int_y[order_perm_q[i].id], &l_minus[order_perm_q[i].id], &alpha_minus[order_perm_q[i].id]); } delete T; } for(i=0;i<w_size;i++) f += w[i] * w[i]; f /= 2.0; for(i=0;i<l;i++) f += C * (z[i] * ((l_plus[i] + l_minus[i]) * z[i] - alpha_minus[i] - alpha_plus[i] - 2 * (l_minus[i] - l_plus[i])) + l_minus[i]); return(f); }
void l2r_l2_ranksvm_fun::grad(double *w, double *g) { int i; int l = prob->l; double *ATAXw; ATAXw = new double[l]; int w_size = get_nr_variable(); for (i=0;i<l;i++) ATAXw[i] = l_plus[i] - l_minus[i] + (l_plus[i] + l_minus[i]) * z[i] - alpha_plus[i] - alpha_minus[i]; XTv(ATAXw, g); for(i=0;i<w_size;i++) g[i] = w[i] + 2 * C * g[i]; delete[] ATAXw; }
void l2r_l2_svc_fun::Hv(double *s, double *Hs) { int i; int l=prob->l; int w_size=get_nr_variable(); double *wa = new double[l]; subXv(s, wa); for(i=0;i<sizeI;i++) wa[i] = C[I[i]]*wa[i]; subXTv(wa, Hs); for(i=0;i<w_size;i++) Hs[i] = s[i] + 2*Hs[i]; delete[] wa; }
void l2r_lr_fun::Hv(double *s, double *Hs) { int i; int l=prob->l; int w_size=get_nr_variable(); double *wa = new double[l]; Xv(s, wa); for(i=0;i<l;i++) wa[i] = C[i]*D[i]*wa[i]; XTv(wa, Hs); for(i=0;i<w_size;i++) Hs[i] = s[i] + Hs[i]; delete[] wa; }
void l2r_l2_ranksvm_fun::Hv(double *s, double *Hs) { int q,i,j; int w_size = get_nr_variable(); int l = prob->l; double *wa = new double[l]; selectiontree *T; double* alpha_plus_minus; alpha_plus_minus = new double[l]; Xv(s, wa); for (q=0;q<nr_query;q++) { id_and_value *order_perm_q = order_perm[q]; T = new selectiontree(nr_class[q]); j = 0; for (i=0;i<count[q];i++) { while (j<count[q] && (1 - order_perm_q[j].value + order_perm_q[i].value>0)) { T->insert_node(int_y[order_perm_q[j].id],wa[order_perm_q[j].id]); j++; } alpha_plus_minus[order_perm_q[i].id] = T->xv_larger(int_y[order_perm_q[i].id]); } delete T; j = count[q] - 1; T = new selectiontree(nr_class[q]); for (i=count[q]-1;i>=0;i--) { while (j>=0 && (1 - order_perm_q[i].value + order_perm_q[j].value>0)) { T->insert_node(int_y[order_perm_q[j].id], wa[order_perm_q[j].id]); j--; } alpha_plus_minus[order_perm_q[i].id] += T->xv_smaller(int_y[order_perm_q[i].id]); } delete T; } for (i=0;i<l;i++) wa[i] = wa[i] * (l_plus[i] + l_minus[i]) - alpha_plus_minus[i]; delete[] alpha_plus_minus; XTv(wa, Hs); delete[] wa; for(i=0;i<w_size;i++) Hs[i] = s[i] + 2 * C * Hs[i]; }
void selection_rank_fun::Hv(double *s, double *Hs) { int i,j,k; int w_size=get_nr_variable(); int l=prob->l; double *wa = new double[l]; selectiontree *T; double* alpha_plus_minus; alpha_plus_minus = new double[l]; Xv(s, wa); for (i=0;i<nr_subset;i++) { T=new selectiontree(nr_class[i]); k=0; for (j=0;j<count[i];j++) { while (k<count[i]&&(1-pi[i][j].value+pi[i][k].value>0)) { T->insert_node(int_y[pi[i][k].id],wa[pi[i][k].id]); k++; } alpha_plus_minus[pi[i][j].id]=T->vector_sum_smaller(int_y[pi[i][j].id]); } delete T; k=count[i]-1; T = new selectiontree(nr_class[i]); for (j=count[i]-1;j>=0;j--) { while (k>=0&&(1+pi[i][j].value-pi[i][k].value>0)) { T->insert_node(int_y[pi[i][k].id],wa[pi[i][k].id]); k--; } alpha_plus_minus[pi[i][j].id]+=T->vector_sum_larger(int_y[pi[i][j].id]); } delete T; } for (i=0;i<l;i++) wa[i]=wa[i]*((double)l_plus[i]+(double)l_minus[i])-alpha_plus_minus[i]; delete[] alpha_plus_minus; XTv(wa, Hs); delete[] wa; for(i=0;i<w_size;i++) Hs[i] = s[i] + 2*C*Hs[i]; }
void l2r_lr_fun::grad(double *w, double *g) { int i; int *y=prob->y; int l=prob->l; int w_size=get_nr_variable(); for(i=0;i<l;i++) { z[i] = 1/(1 + exp(-y[i]*z[i])); D[i] = z[i]*(1-z[i]); z[i] = C[i]*(z[i]-1)*y[i]; } XTv(z, g); for(i=0;i<w_size;i++) g[i] = w[i] + g[i]; }
void l2r_l2_svc_fun::subXTv(double *v, double *XTv) { int i; int w_size=get_nr_variable(); feature_node **x=prob->x; for(i=0;i<w_size;i++) XTv[i]=0; for(i=0;i<sizeI;i++) { feature_node *s=x[I[i]]; while(s->index!=-1) { XTv[s->index-1]+=v[i]*s->value; s++; } } }
void l2r_l2_ranksvm_fun::XTv(double *v, double *XTv) { int i; int l=prob->l; int w_size=get_nr_variable(); feature_node **x=prob->x; for(i=0;i<w_size;i++) XTv[i]=0; for(i=0;i<l;i++) { feature_node *s=x[i]; while(s->index!=-1) { XTv[s->index-1]+=v[i]*s->value; s++; } } }
void l2r_l2_svc_fun::grad(double *w, double *g) { int i; int *y=prob->y; int l=prob->l; int w_size=get_nr_variable(); sizeI = 0; for (i=0;i<l;i++) if (z[i] < 1) { z[sizeI] = C[i]*y[i]*(z[i]-1); I[sizeI] = i; sizeI++; } subXTv(z, g); for(i=0;i<w_size;i++) g[i] = w[i] + 2*g[i]; }
void selection_rank_fun::sample_subXTv(double *v, double *XTv) { int i, j; int w_size=get_nr_variable(); feature_node **x=prob->x; for(i=0;i<w_size;i++) XTv[i]=0; for(i=0;i<nr_subset;i++) { for(j=0;j<sizeS[i];j++) { feature_node *s=x[Spi[i][j].id]; while(s->index!=-1) { XTv[s->index-1]+=v[startS[i]+j]*s->value; s++; } } } }
void selection_rank_fun::grad(double *w, double *g, int *Hsample, int now_iter, int num_sample, long int *num_data ) { int i, j, k; int l=prob->l; double *ATAXw; ATAXw = new double[l]; int w_size=get_nr_variable(); for(i=0;i<nr_subset;i++)//do sampling { sizeS[i]=0; for(j=0;j<count[i];j++) { k = pi[i][j].id; if(Hsample[k]==now_iter%num_sample) { Spi[i][sizeS[i]].id = pi[i][j].id; Spi[i][sizeS[i]].value = pi[i][j].value; Is[i][sizeS[i]] = j; sizeS[i]++; } } } startS[0] = 0; totalS = sizeS[0]; for(i=1;i<nr_subset;i++) { totalS += sizeS[i]; startS[i] = startS[i-1] + sizeS[i-1]; } for (i=0;i<l;i++) { ATAXw[i]=(double)l_plus[i]-(double)l_minus[i]+((double)l_plus[i]+(double)l_minus[i])*z[i]-alpha_plus[i]-alpha_minus[i]; } XTv(ATAXw, g); for(i=0;i<w_size;i++) g[i] = w[i] + 2*C*g[i]; delete[] ATAXw; }
double l2r_l2_svc_fun::fun(double *w) { int i; double f=0; int *y=prob->y; int l=prob->l; int w_size=get_nr_variable(); Xv(w, z); for(i=0;i<l;i++) { z[i] = y[i]*z[i]; double d = 1-z[i]; if (d > 0) f += C[i]*d*d; } f = 2*f; for(i=0;i<w_size;i++) f += w[i]*w[i]; f /= 2.0; return(f); }
double l2r_lr_fun::fun(double *w) { int i; double f=0; int *y=prob->y; int l=prob->l; int w_size=get_nr_variable(); Xv(w, z); for(i=0;i<l;i++) { double yz = y[i]*z[i]; if (yz >= 0) f += C[i]*log(1 + exp(-yz)); else f += C[i]*(-yz+log(1 + exp(yz))); } f = 2*f; for(i=0;i<w_size;i++) f += w[i]*w[i]; f /= 2.0; return(f); }
double selection_rank_fun::fun(double *w, long int *num_data) { int i,j,k; double f=0.0; int l=prob->l; int w_size=get_nr_variable(); selectiontree *T; Xv(w,z); *num_data += l; for (i=0;i<nr_subset;i++) { for (j=0;j<count[i];j++) { pi[i][j].id = perm[j+start[i]]; pi[i][j].value = z[perm[j+start[i]]]; } qsort(pi[i], count[i], sizeof(id_and_value), compare_id_and_value); T=new selectiontree(nr_class[i]); k=0; for (j=0;j<count[i];j++) { while (k<count[i]&&(1-pi[i][j].value+pi[i][k].value>0)) { T->insert_node(int_y[pi[i][k].id],pi[i][k].value); k++; } T->count_smaller(int_y[pi[i][j].id],&l_minus[pi[i][j].id], &alpha_minus[pi[i][j].id]); } delete T; k=count[i]-1; T = new selectiontree(nr_class[i]); for (j=count[i]-1;j>=0;j--) { while (k>=0&&(1+pi[i][j].value-pi[i][k].value>0)) { T->insert_node(int_y[pi[i][k].id],pi[i][k].value); k--; } T->count_larger(int_y[pi[i][j].id],&l_plus[pi[i][j].id], &alpha_plus[pi[i][j].id]); } delete T; } long long nSV = 0; for (i=0;i<l;i++) nSV += l_plus[i]; //info("nSV = %ld\n",nSV); for(i=0;i<w_size;i++) { f += w[i]*w[i]; } f /= 2.0; for(i=0;i<l;i++) { f += C*(z[i]*((l_plus[i]+l_minus[i])*z[i]-alpha_minus[i]-alpha_plus[i]-2*(l_minus[i]-l_plus[i]))+l_minus[i]); } return (f); }
void selection_rank_fun::Hv(double *s, double *Hs) { int i,j,k; int w_size=get_nr_variable(); int l=prob->l; double *wa = new double[l]; selectiontree *T; double* alpha_plus_minus; alpha_plus_minus = new double[l]; double begin, end; begin = omp_get_wtime(); Xv(s, wa); end = omp_get_wtime(); Xviter++; Xvtime += end-begin; begin = omp_get_wtime(); #pragma omp parallel for default(shared) private(i, j, k, T) for (i=0;i<nr_subset;i++) { T=new selectiontree(nr_class[i]);// nr_class[i]is the i-th query's number of different label k=0; for (j=0;j<count[i];j++) { while (k<count[i]&&(1-pi[i][j].value+pi[i][k].value>0)) { T->insert_node(int_y[pi[i][k].id],wa[pi[i][k].id]); k++; } alpha_plus_minus[pi[i][j].id]=T->vector_sum_smaller(int_y[pi[i][j].id]); } delete T; k=count[i]-1; T = new selectiontree(nr_class[i]); for (j=count[i]-1;j>=0;j--) { while (k>=0&&(1+pi[i][j].value-pi[i][k].value>0)) { T->insert_node(int_y[pi[i][k].id],wa[pi[i][k].id]); k--; } alpha_plus_minus[pi[i][j].id]+=T->vector_sum_larger(int_y[pi[i][j].id]); } delete T; } end = omp_get_wtime(); AViter++; AVtime += end-begin; #pragma omp parallel for default(shared) private(i) for (i=0;i<l;i++) wa[i]=wa[i]*((double)l_plus[i]+(double)l_minus[i])-alpha_plus_minus[i]; delete[] alpha_plus_minus; begin = omp_get_wtime(); XTv(wa, Hs); end = omp_get_wtime(); XTviter++; XTvtime += end-begin; delete[] wa; #pragma omp parallel for default(shared) private(i) for(i=0;i<w_size;i++) Hs[i] = s[i] + 2*C*Hs[i]; }
double selection_rank_fun::fun(double *w) { int i,j,k; double f = 0.0; int l=prob->l; int w_size=get_nr_variable(); selectiontree *T; double begin,end; begin = omp_get_wtime(); Xv(w,z); end = omp_get_wtime(); Xviter++; Xvtime += end-begin; begin = omp_get_wtime(); #pragma omp parallel for default(shared) private(i, j, k, T) //construct the OST for (i=0;i<nr_subset;i++) { for (j=0;j<count[i];j++) { pi[i][j].id = perm[j+start[i]]; pi[i][j].value = z[perm[j+start[i]]]; } qsort(pi[i], count[i], sizeof(id_and_value), compare_id_and_value); T=new selectiontree(nr_class[i]); k=0; for (j=0;j<count[i];j++) { while (k<count[i]&&(1-pi[i][j].value+pi[i][k].value>0)) { T->insert_node(int_y[pi[i][k].id],pi[i][k].value); k++; } T->count_smaller(int_y[pi[i][j].id],&l_minus[pi[i][j].id], &alpha_minus[pi[i][j].id]); } delete T; k=count[i]-1; T = new selectiontree(nr_class[i]); for (j=count[i]-1;j>=0;j--) { while (k>=0&&(1+pi[i][j].value-pi[i][k].value>0)) { T->insert_node(int_y[pi[i][k].id],pi[i][k].value); k--; } T->count_larger(int_y[pi[i][j].id],&l_plus[pi[i][j].id], &alpha_plus[pi[i][j].id]); } delete T; } end = omp_get_wtime(); AViter++; AVtime += end-begin; long long nSV = 0; #pragma omp parallel for default(shared) reduction(+:nSV) private(i) for (i=0;i<l;i++) nSV += l_plus[i]; //info("nSV = %ld\n",nSV); for(i=0;i<w_size;i++) { f += w[i]*w[i]; } f /= 2.0; #pragma omp parallel for default(shared) private(i) reduction(+:f) for(i=0;i<l;i++) { f += C*(z[i]*((l_plus[i]+l_minus[i])*z[i]-alpha_minus[i]-alpha_plus[i]-2.0*(l_minus[i]-l_plus[i]))+l_minus[i]); } return (f); }