@@ -1244,8 +1244,9 @@ static int solve_l2r_l1l2_svr(
12441244// See Algorithm 5 of Yu et al., MLJ 2010
12451245
12461246
1247- #define GETC (i ) upper_bound[y[i]+1 ]*sample_weight[i]
1248- // To support weights for instances (sample_weight*class_weight), use GETC(i) (i)
1247+ #define SAMPLE_WEIGHT (i ) upper_bound[y[i]+1 ]*sample_weight[i]
1248+ // To support weights for instances, use SAMPLE_WEIGHT(i)
1249+ // Each instance is weighted by sample_weight*class_weight)
12491250
12501251int solve_l2r_lr_dual (const problem *prob, double *w, double eps, double Cp, double Cn,
12511252 int max_iter)
@@ -1276,12 +1277,12 @@ int solve_l2r_lr_dual(const problem *prob, double *w, double eps, double Cp, dou
12761277}
12771278
12781279// Initial alpha can be set here. Note that
1279- // 0 < alpha[i] < GETC (i)
1280- // alpha[2*i] + alpha[2*i+1] = GETC (i)
1280+ // 0 < alpha[i] < SAMPLE_WEIGHT (i)
1281+ // alpha[2*i] + alpha[2*i+1] = SAMPLE_WEIGHT (i)
12811282for (i=0 ; i<l; i++)
12821283{
1283- alpha[2 *i] = min (0.001 *GETC (i), 1e-8 );
1284- alpha[2 *i+1 ] = GETC (i) - alpha[2 *i];
1284+ alpha[2 *i] = min (0.001 *SAMPLE_WEIGHT (i), 1e-8 );
1285+ alpha[2 *i+1 ] = SAMPLE_WEIGHT (i) - alpha[2 *i];
12851286}
12861287
12871288for (i=0 ; i<w_size; i++)
@@ -1313,7 +1314,7 @@ int solve_l2r_lr_dual(const problem *prob, double *w, double eps, double Cp, dou
13131314{
13141315i = index[s];
13151316schar yi = y[i];
1316- double C = GETC (i);
1317+ double C = SAMPLE_WEIGHT (i);
13171318double ywTx = 0 , xisq = xTx[i];
13181319feature_node *xi = prob->x [i];
13191320while (xi->index != -1 )
@@ -1396,7 +1397,7 @@ int solve_l2r_lr_dual(const problem *prob, double *w, double eps, double Cp, dou
13961397v *= 0.5 ;
13971398for (i=0 ; i<l; i++)
13981399v += alpha[2 *i] * log (alpha[2 *i]) + alpha[2 *i+1 ] * log (alpha[2 *i+1 ])
1399- - GETC (i) * log (GETC (i));
1400+ - SAMPLE_WEIGHT (i) * log (SAMPLE_WEIGHT (i));
14001401info (" Objective value = %lf\n " , v);
14011402
14021403delete [] xTx;
@@ -1705,9 +1706,10 @@ static int solve_l1r_l2_svc(
17051706// solution will be put in w
17061707//
17071708// See Yuan et al. (2011) and appendix of LIBLINEAR paper, Fan et al. (2008)
1708- #undef GETC
1709- #define GETC (i ) C[y[i]+1 ]*sample_weight[i]
1710- // To support weights (sample_weight*class_weight) for instances, use GETC(i) (i)
1709+ #undef SAMPLE_WEIGHT
1710+ #define SAMPLE_WEIGHT (i ) C[y[i]+1 ]*sample_weight[i]
1711+ // To support weights for instances, use SAMPLE_WEIGHT(i)
1712+ // Each instance is weighted by (class_weight*sample_weight)
17111713
17121714static int solve_l1r_lr (
17131715const problem *prob_col, double *w, double eps,
@@ -1777,16 +1779,16 @@ static int solve_l1r_lr(
17771779double val = x->value ;
17781780exp_wTx[ind] += w[j]*val;
17791781if (y[ind] == -1 )
1780- xjneg_sum[j] += GETC (ind)*val;
1782+ xjneg_sum[j] += SAMPLE_WEIGHT (ind)*val;
17811783x++;
17821784}
17831785}
17841786for (j=0 ; j<l; j++)
17851787{
17861788exp_wTx[j] = exp (exp_wTx[j]);
17871789double tau_tmp = 1 /(1 +exp_wTx[j]);
1788- tau[j] = GETC (j)*tau_tmp;
1789- D[j] = GETC (j)*exp_wTx[j]*tau_tmp*tau_tmp;
1790+ tau[j] = SAMPLE_WEIGHT (j)*tau_tmp;
1791+ D[j] = SAMPLE_WEIGHT (j)*exp_wTx[j]*tau_tmp*tau_tmp;
17901792}
17911793
17921794while (newton_iter < max_newton_iter)
@@ -1962,7 +1964,7 @@ static int solve_l1r_lr(
19621964negsum_xTd = 0 ;
19631965for (int i=0 ; i<l; i++)
19641966if (y[i] == -1 )
1965- negsum_xTd += GETC (i)*xTd[i];
1967+ negsum_xTd += SAMPLE_WEIGHT (i)*xTd[i];
19661968
19671969int num_linesearch;
19681970for (num_linesearch=0 ; num_linesearch < max_num_linesearch; num_linesearch++)
@@ -1973,7 +1975,7 @@ static int solve_l1r_lr(
19731975{
19741976double exp_xTd = exp (xTd[i]);
19751977exp_wTx_new[i] = exp_wTx[i]*exp_xTd;
1976- cond += GETC (i)*log ((1 +exp_wTx_new[i])/(exp_xTd+exp_wTx_new[i]));
1978+ cond += SAMPLE_WEIGHT (i)*log ((1 +exp_wTx_new[i])/(exp_xTd+exp_wTx_new[i]));
19771979}
19781980
19791981if (cond <= 0 )
@@ -1985,8 +1987,8 @@ static int solve_l1r_lr(
19851987{
19861988exp_wTx[i] = exp_wTx_new[i];
19871989double tau_tmp = 1 /(1 +exp_wTx[i]);
1988- tau[i] = GETC (i)*tau_tmp;
1989- D[i] = GETC (i)*exp_wTx[i]*tau_tmp*tau_tmp;
1990+ tau[i] = SAMPLE_WEIGHT (i)*tau_tmp;
1991+ D[i] = SAMPLE_WEIGHT (i)*exp_wTx[i]*tau_tmp*tau_tmp;
19901992}
19911993break ;
19921994}
@@ -2053,9 +2055,9 @@ static int solve_l1r_lr(
20532055}
20542056for (j=0 ; j<l; j++)
20552057if (y[j] == 1 )
2056- v += GETC (j)*log (1 +1 /exp_wTx[j]);
2058+ v += SAMPLE_WEIGHT (j)*log (1 +1 /exp_wTx[j]);
20572059else
2058- v += GETC (j)*log (1 +exp_wTx[j]);
2060+ v += SAMPLE_WEIGHT (j)*log (1 +exp_wTx[j]);
20592061
20602062info (" Objective value = %lf\n " , v);
20612063info (" #nonzeros/#features = %d/%d\n " , nnz, w_size);
@@ -2496,6 +2498,7 @@ model* train(const problem *prob, const parameter *param)
24962498free (sub_prob.x );
24972499free (sub_prob.y );
24982500free (weighted_C);
2501+ delete[] sample_weight;
24992502}
25002503return model_;
25012504}
0 commit comments