15 template <
class T>
static inline T
min(T
x, T y)
17 return (x < y) ? x : y;
21 template <
class T>
static inline T
max(T
x, T y)
23 return (x > y) ? x : y;
26 template <
class T>
static inline void swap(T&
x, T& y)
32 template <
class S,
class T>
static inline void clone(T*& dst, S* src,
int n)
35 memcpy((
void *)dst, (
void *)src,
sizeof(T)*n);
37 static inline double powi(
double base,
int times)
39 double tmp = base, ret = 1.0;
41 for (
int t = times; t > 0; t /= 2)
43 if (t % 2 == 1) ret *= tmp;
50 #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 59 static void info(
const char *fmt, ...)
64 vsprintf(buf, fmt, ap);
66 (*svm_print_string)(buf);
69 static void info(
const char *fmt, ...) {}
141 int more = len - h->
len;
178 if (i > j)
swap(i, j);
184 swap(h->data[i], h->data[j]);
208 virtual Qfloat *get_Q(
int column,
int len)
const = 0;
209 virtual double *get_QD()
const = 0;
210 virtual void swap_index(
int i,
int j)
const = 0;
222 virtual Qfloat *get_Q(
int column,
int len)
const = 0;
223 virtual double *get_QD()
const = 0;
227 if (x_square)
swap(x_square[i], x_square[j]);
231 double (
Kernel::*kernel_function)(
int i,
int j)
const;
246 return dot(x[i], x[j]);
250 return powi(gamma * dot(x[i], x[j]) + coef0, degree);
254 return exp(-gamma * (x_square[i] + x_square[j] - 2 * dot(x[i], x[j])));
258 return tanh(gamma * dot(x[i], x[j]) + coef0);
262 return x[i][(int)(x[j][0].value)].
value;
267 : kernel_type(param.kernel_type), degree(param.degree),
268 gamma(param.gamma), coef0(param.coef0)
294 for (
int i = 0; i < l; i++)
365 while (x->
index != -1)
371 while (y->
index != -1)
377 return exp(-param.
gamma * sum);
421 void Solve(
int l,
const QMatrix& Q,
const double *p_,
const schar *y_,
422 double *alpha_,
double Cp,
double Cn,
double eps,
443 return (y[i] > 0) ? Cp : Cn;
447 if (alpha[i] >= get_C(i))
448 alpha_status[i] = UPPER_BOUND;
449 else if (alpha[i] <= 0)
450 alpha_status[i] = LOWER_BOUND;
451 else alpha_status[i] = FREE;
455 return alpha_status[i] == UPPER_BOUND;
459 return alpha_status[i] == LOWER_BOUND;
463 return alpha_status[i] == FREE;
466 void reconstruct_gradient();
467 virtual int select_working_set(
int &i,
int &j);
468 virtual double calculate_rho();
469 virtual void do_shrinking();
471 bool be_shrunk(
int i,
double Gmax1,
double Gmax2);
479 swap(alpha_status[i], alpha_status[j]);
480 swap(alpha[i], alpha[j]);
482 swap(active_set[i], active_set[j]);
483 swap(G_bar[i], G_bar[j]);
490 if (active_size == l)
return;
495 for (j = active_size; j < l; j++)
496 G[j] = G_bar[j] + p[j];
498 for (j = 0; j < active_size; j++)
502 if (2 * nr_free < active_size)
503 info(
"\nWARNING: using -h 0 may be faster\n");
505 if (nr_free * l > 2 * active_size * (l - active_size))
507 for (i = active_size; i < l; i++)
509 const Qfloat *Q_i = Q->get_Q(i, active_size);
510 for (j = 0; j < active_size; j++)
512 G[i] += alpha[j] * Q_i[j];
517 for (i = 0; i < active_size; i++)
520 const Qfloat *Q_i = Q->get_Q(i, l);
521 double alpha_i = alpha[i];
522 for (j = active_size; j < l; j++)
523 G[j] += alpha_i * Q_i[j];
529 double *alpha_,
double Cp,
double Cn,
double eps,
537 clone(alpha, alpha_, l);
545 alpha_status =
new char[l];
546 for (
int i = 0; i < l; i++)
547 update_alpha_status(i);
552 active_set =
new int[l];
553 for (
int i = 0; i < l; i++)
561 G_bar =
new double[l];
563 for (i = 0; i < l; i++)
568 for (i = 0; i < l; i++)
569 if (!is_lower_bound(i))
572 double alpha_i = alpha[i];
574 for (j = 0; j < l; j++)
575 G[j] += alpha_i * Q_i[j];
576 if (is_upper_bound(i))
577 for (j = 0; j < l; j++)
578 G_bar[j] += get_C(i) * Q_i[j];
585 int max_iter =
max(10000000, l > INT_MAX / 100 ? INT_MAX : 100 * l);
586 int counter =
min(l, 1000) + 1;
588 while (iter < max_iter)
594 counter =
min(l, 1000);
595 if (shrinking) do_shrinking();
600 if (select_working_set(i, j) != 0)
603 reconstruct_gradient();
607 if (select_working_set(i, j) != 0)
620 double C_i = get_C(i);
621 double C_j = get_C(j);
623 double old_alpha_i = alpha[i];
624 double old_alpha_j = alpha[j];
628 double quad_coef = QD[i] + QD[j] + 2 * Q_i[j];
631 double delta = (-G[i] - G[j]) / quad_coef;
632 double diff = alpha[i] - alpha[j];
652 if (diff > C_i - C_j)
657 alpha[j] = C_i - diff;
665 alpha[i] = C_j + diff;
671 double quad_coef = QD[i] + QD[j] - 2 * Q_i[j];
674 double delta = (G[i] - G[j]) / quad_coef;
675 double sum = alpha[i] + alpha[j];
684 alpha[j] = sum - C_i;
700 alpha[i] = sum - C_j;
715 double delta_alpha_i = alpha[i] - old_alpha_i;
716 double delta_alpha_j = alpha[j] - old_alpha_j;
718 for (
int k = 0; k < active_size; k++)
720 G[k] += Q_i[k] * delta_alpha_i + Q_j[k] * delta_alpha_j;
726 bool ui = is_upper_bound(i);
727 bool uj = is_upper_bound(j);
728 update_alpha_status(i);
729 update_alpha_status(j);
731 if (ui != is_upper_bound(i))
735 for (k = 0; k < l; k++)
736 G_bar[k] -= C_i * Q_i[k];
738 for (k = 0; k < l; k++)
739 G_bar[k] += C_i * Q_i[k];
742 if (uj != is_upper_bound(j))
746 for (k = 0; k < l; k++)
747 G_bar[k] -= C_j * Q_j[k];
749 for (k = 0; k < l; k++)
750 G_bar[k] += C_j * Q_j[k];
755 if (iter >= max_iter)
760 reconstruct_gradient();
764 fprintf(stderr,
"\nWARNING: reaching max number of iterations\n");
769 si->
rho = calculate_rho();
775 for (i = 0; i < l; i++)
776 v += alpha[i] * (G[i] + p[i]);
783 for (
int i = 0; i < l; i++)
784 alpha_[active_set[i]] = alpha[i];
803 delete[] alpha_status;
822 double obj_diff_min =
INF;
824 for (
int t = 0; t < active_size; t++)
827 if (!is_upper_bound(t))
836 if (!is_lower_bound(t))
847 Q_i = Q->get_Q(i, active_size);
849 for (
int j = 0; j < active_size; j++)
853 if (!is_lower_bound(j))
855 double grad_diff = Gmax + G[j];
861 double quad_coef = QD[i] + QD[j] - 2.0 * y[i] * Q_i[j];
863 obj_diff = -(grad_diff * grad_diff) / quad_coef;
865 obj_diff = -(grad_diff * grad_diff) /
TAU;
867 if (obj_diff <= obj_diff_min)
870 obj_diff_min = obj_diff;
877 if (!is_upper_bound(j))
879 double grad_diff = Gmax - G[j];
885 double quad_coef = QD[i] + QD[j] + 2.0 * y[i] * Q_i[j];
887 obj_diff = -(grad_diff * grad_diff) / quad_coef;
889 obj_diff = -(grad_diff * grad_diff) /
TAU;
891 if (obj_diff <= obj_diff_min)
894 obj_diff_min = obj_diff;
901 if (Gmax + Gmax2 < eps)
911 if (is_upper_bound(i))
914 return (-G[i] > Gmax1);
916 return (-G[i] > Gmax2);
918 else if (is_lower_bound(i))
921 return (G[i] > Gmax2);
923 return (G[i] > Gmax1);
936 for (i = 0; i < active_size; i++)
940 if (!is_upper_bound(i))
945 if (!is_lower_bound(i))
953 if (!is_upper_bound(i))
958 if (!is_lower_bound(i))
966 if (unshrink ==
false && Gmax1 + Gmax2 <= eps * 10)
969 reconstruct_gradient();
974 for (i = 0; i < active_size; i++)
975 if (be_shrunk(i, Gmax1, Gmax2))
978 while (active_size > i)
980 if (!be_shrunk(active_size, Gmax1, Gmax2))
994 double ub =
INF, lb = -
INF, sum_free = 0;
995 for (
int i = 0; i < active_size; i++)
997 double yG = y[i] * G[i];
999 if (is_upper_bound(i))
1006 else if (is_lower_bound(i))
1021 r = sum_free / nr_free;
1038 double *alpha,
double Cp,
double Cn,
double eps,
1042 Solver::Solve(l, Q, p, y, alpha, Cp, Cn, eps, si, shrinking);
1046 int select_working_set(
int &i,
int &j);
1047 double calculate_rho();
1048 bool be_shrunk(
int i,
double Gmax1,
double Gmax2,
double Gmax3,
double Gmax4);
1049 void do_shrinking();
1061 double Gmaxp = -
INF;
1062 double Gmaxp2 = -
INF;
1065 double Gmaxn = -
INF;
1066 double Gmaxn2 = -
INF;
1070 double obj_diff_min =
INF;
1072 for (
int t = 0; t < active_size; t++)
1075 if (!is_upper_bound(t))
1084 if (!is_lower_bound(t))
1094 const Qfloat *Q_ip = NULL;
1095 const Qfloat *Q_in = NULL;
1097 Q_ip = Q->get_Q(ip, active_size);
1099 Q_in = Q->get_Q(in, active_size);
1101 for (
int j = 0; j < active_size; j++)
1105 if (!is_lower_bound(j))
1107 double grad_diff = Gmaxp + G[j];
1113 double quad_coef = QD[ip] + QD[j] - 2 * Q_ip[j];
1115 obj_diff = -(grad_diff * grad_diff) / quad_coef;
1117 obj_diff = -(grad_diff * grad_diff) /
TAU;
1119 if (obj_diff <= obj_diff_min)
1122 obj_diff_min = obj_diff;
1129 if (!is_upper_bound(j))
1131 double grad_diff = Gmaxn - G[j];
1132 if (-G[j] >= Gmaxn2)
1137 double quad_coef = QD[in] + QD[j] - 2 * Q_in[j];
1139 obj_diff = -(grad_diff * grad_diff) / quad_coef;
1141 obj_diff = -(grad_diff * grad_diff) /
TAU;
1143 if (obj_diff <= obj_diff_min)
1146 obj_diff_min = obj_diff;
1153 if (
max(Gmaxp + Gmaxp2, Gmaxn + Gmaxn2) < eps)
1156 if (y[Gmin_idx] == +1)
1167 if (is_upper_bound(i))
1170 return (-G[i] > Gmax1);
1172 return (-G[i] > Gmax4);
1174 else if (is_lower_bound(i))
1177 return (G[i] > Gmax2);
1179 return (G[i] > Gmax3);
1187 double Gmax1 = -
INF;
1188 double Gmax2 = -
INF;
1189 double Gmax3 = -
INF;
1190 double Gmax4 = -
INF;
1194 for (i = 0; i < active_size; i++)
1196 if (!is_upper_bound(i))
1200 if (-G[i] > Gmax1) Gmax1 = -G[i];
1202 else if (-G[i] > Gmax4) Gmax4 = -G[i];
1204 if (!is_lower_bound(i))
1208 if (G[i] > Gmax2) Gmax2 = G[i];
1210 else if (G[i] > Gmax3) Gmax3 = G[i];
1214 if (unshrink ==
false &&
max(Gmax1 + Gmax2, Gmax3 + Gmax4) <= eps * 10)
1217 reconstruct_gradient();
1221 for (i = 0; i < active_size; i++)
1222 if (be_shrunk(i, Gmax1, Gmax2, Gmax3, Gmax4))
1225 while (active_size > i)
1227 if (!be_shrunk(active_size, Gmax1, Gmax2, Gmax3, Gmax4))
1239 int nr_free1 = 0, nr_free2 = 0;
1240 double ub1 =
INF, ub2 =
INF;
1241 double lb1 = -
INF, lb2 = -
INF;
1242 double sum_free1 = 0, sum_free2 = 0;
1244 for (
int i = 0; i < active_size; i++)
1248 if (is_upper_bound(i))
1249 lb1 =
max(lb1, G[i]);
1250 else if (is_lower_bound(i))
1251 ub1 =
min(ub1, G[i]);
1260 if (is_upper_bound(i))
1261 lb2 =
max(lb2, G[i]);
1262 else if (is_lower_bound(i))
1263 ub2 =
min(ub2, G[i]);
1274 r1 = sum_free1 / nr_free1;
1276 r1 = (ub1 + lb1) / 2;
1279 r2 = sum_free2 / nr_free2;
1281 r2 = (ub2 + lb2) / 2;
1283 si->r = (r1 + r2) / 2;
1284 return (r1 - r2) / 2;
1294 :
Kernel(prob.l, prob.
x, param)
1298 QD =
new double[prob.
l];
1299 for (
int i = 0; i < prob.
l; i++)
1307 if ((start = cache->get_data(i, &data, len)) < len)
1309 for (j = start; j < len; j++)
1322 cache->swap_index(i, j);
1344 :
Kernel(prob.l, prob.
x, param)
1347 QD =
new double[prob.
l];
1348 for (
int i = 0; i < prob.
l; i++)
1356 if ((start = cache->get_data(i, &data, len)) < len)
1358 for (j = start; j < len; j++)
1371 cache->swap_index(i, j);
1390 :
Kernel(prob.l, prob.
x, param)
1394 QD =
new double[2 * l];
1395 sign =
new schar[2 * l];
1396 index =
new int[2 * l];
1397 for (
int k = 0; k < l; k++)
1413 swap(sign[i], sign[j]);
1414 swap(index[i], index[j]);
1421 int j, real_i = index[i];
1422 if (cache->get_data(real_i, &data, l) < l)
1424 for (j = 0; j < l; j++)
1430 next_buffer = 1 - next_buffer;
1432 for (j = 0; j < len; j++)
1433 buf[j] = (
Qfloat) si * (
Qfloat) sign[j] * data[index[j]];
1469 double *minus_ones =
new double[l];
1474 for (i = 0; i < l; i++)
1478 if (prob->
y[i] > 0) y[i] = +1;
1483 s.
Solve(l,
SVC_Q(*prob, *param, y), minus_ones, y,
1486 double sum_alpha = 0;
1487 for (i = 0; i < l; i++)
1488 sum_alpha += alpha[i];
1493 for (i = 0; i < l; i++)
1496 delete[] minus_ones;
1506 double nu = param->
nu;
1510 for (i = 0; i < l; i++)
1516 double sum_pos = nu * l / 2;
1517 double sum_neg = nu * l / 2;
1519 for (i = 0; i < l; i++)
1522 alpha[i] =
min(1.0, sum_pos);
1523 sum_pos -= alpha[i];
1527 alpha[i] =
min(1.0, sum_neg);
1528 sum_neg -= alpha[i];
1531 double *zeros =
new double[l];
1533 for (i = 0; i < l; i++)
1537 s.
Solve(l,
SVC_Q(*prob, *param, y), zeros, y,
1543 for (i = 0; i < l; i++)
1544 alpha[i] *= y[i] / r;
1560 double *zeros =
new double[l];
1564 int n = (int)(param->
nu * prob->
l);
1566 for (i = 0; i < n; i++)
1569 alpha[n] = param->
nu * prob->
l - n;
1570 for (i = n + 1; i < l; i++)
1573 for (i = 0; i < l; i++)
1592 double *alpha2 =
new double[2 * l];
1593 double *linear_term =
new double[2 * l];
1597 for (i = 0; i < l; i++)
1600 linear_term[i] = param->
p - prob->
y[i];
1604 linear_term[i + l] = param->
p + prob->
y[i];
1609 s.
Solve(2 * l,
SVR_Q(*prob, *param), linear_term, y,
1612 double sum_alpha = 0;
1613 for (i = 0; i < l; i++)
1615 alpha[i] = alpha2[i] - alpha2[i + l];
1616 sum_alpha += fabs(alpha[i]);
1621 delete[] linear_term;
1630 double C = param->
C;
1631 double *alpha2 =
new double[2 * l];
1632 double *linear_term =
new double[2 * l];
1636 double sum = C * param->
nu * l / 2;
1637 for (i = 0; i < l; i++)
1639 alpha2[i] = alpha2[i + l] =
min(sum, C);
1642 linear_term[i] = - prob->
y[i];
1645 linear_term[i + l] = prob->
y[i];
1650 s.
Solve(2 * l,
SVR_Q(*prob, *param), linear_term, y,
1655 for (i = 0; i < l; i++)
1656 alpha[i] = alpha2[i] - alpha2[i + l];
1659 delete[] linear_term;
1674 double Cp,
double Cn)
1676 double *alpha =
Malloc(
double, prob->
l);
1703 for (
int i = 0; i < prob->
l; i++)
1705 if (fabs(alpha[i]) > 0)
1731 int l,
const double *dec_values,
const double *labels,
1732 double& A,
double& B)
1734 double prior1 = 0, prior0 = 0;
1737 for (i = 0; i < l; i++)
1738 if (labels[i] > 0) prior1 += 1;
1742 double min_step = 1e-10;
1743 double sigma = 1e-12;
1745 double hiTarget = (prior1 + 1.0) / (prior1 + 2.0);
1746 double loTarget = 1 / (prior0 + 2.0);
1747 double *t =
Malloc(
double, l);
1748 double fApB, p, q, h11, h22, h21, g1, g2, det, dA, dB, gd, stepsize;
1749 double newA, newB, newf, d1, d2;
1754 B = log((prior0 + 1.0) / (prior1 + 1.0));
1757 for (i = 0; i < l; i++)
1759 if (labels[i] > 0) t[i] = hiTarget;
1760 else t[i] = loTarget;
1761 fApB = dec_values[i] * A + B;
1763 fval += t[i] * fApB + log(1 + exp(-fApB));
1765 fval += (t[i] - 1) * fApB + log(1 + exp(fApB));
1767 for (iter = 0; iter < max_iter; iter++)
1775 for (i = 0; i < l; i++)
1777 fApB = dec_values[i] * A + B;
1780 p = exp(-fApB) / (1.0 + exp(-fApB));
1781 q = 1.0 / (1.0 + exp(-fApB));
1785 p = 1.0 / (1.0 + exp(fApB));
1786 q = exp(fApB) / (1.0 + exp(fApB));
1789 h11 += dec_values[i] * dec_values[i] * d2;
1791 h21 += dec_values[i] * d2;
1793 g1 += dec_values[i] * d1;
1798 if (fabs(g1) < eps && fabs(g2) < eps)
1802 det = h11 * h22 - h21 * h21;
1803 dA = -(h22 * g1 - h21 * g2) / det;
1804 dB = -(-h21 * g1 + h11 * g2) / det;
1805 gd = g1 * dA + g2 * dB;
1809 while (stepsize >= min_step)
1811 newA = A + stepsize * dA;
1812 newB = B + stepsize * dB;
1816 for (i = 0; i < l; i++)
1818 fApB = dec_values[i] * newA + newB;
1820 newf += t[i] * fApB + log(1 + exp(-fApB));
1822 newf += (t[i] - 1) * fApB + log(1 + exp(fApB));
1825 if (newf < fval + 0.0001 * stepsize * gd)
1833 stepsize = stepsize / 2.0;
1836 if (stepsize < min_step)
1843 if (iter >= max_iter)
1844 info(
"Reaching maximal iterations in two-class probability estimates\n");
1850 double fApB = decision_value * A + B;
1853 return exp(-fApB) / (1.0 + exp(-fApB));
1855 return 1.0 / (1 + exp(fApB)) ;
1862 int iter = 0, max_iter =
max(100, k);
1863 double **Q =
Malloc(
double *, k);
1864 double *Qp =
Malloc(
double, k);
1865 double pQp, eps = 0.005 / k;
1867 for (t = 0; t < k; t++)
1870 Q[t] =
Malloc(
double, k);
1872 for (j = 0; j < t; j++)
1874 Q[t][t] += r[j][t] * r[j][t];
1877 for (j = t + 1; j < k; j++)
1879 Q[t][t] += r[j][t] * r[j][t];
1880 Q[t][j] = -r[j][t] * r[t][j];
1883 for (iter = 0; iter < max_iter; iter++)
1887 for (t = 0; t < k; t++)
1890 for (j = 0; j < k; j++)
1891 Qp[t] += Q[t][j] * p[j];
1892 pQp += p[t] * Qp[t];
1894 double max_error = 0;
1895 for (t = 0; t < k; t++)
1897 double error = fabs(Qp[t] - pQp);
1898 if (error > max_error)
1901 if (max_error < eps)
break;
1903 for (t = 0; t < k; t++)
1905 double diff = (-Qp[t] + pQp) / Q[t][t];
1907 pQp = (pQp + diff * (diff * Q[t][t] + 2 * Qp[t])) / (1 + diff) / (1 + diff);
1908 for (j = 0; j < k; j++)
1910 Qp[j] = (Qp[j] + diff * Q[t][j]) / (1 + diff);
1915 if (iter >= max_iter)
1916 info(
"Exceeds max_iter in multiclass_prob\n");
1917 for (t = 0; t < k; t++) free(Q[t]);
1925 double Cp,
double Cn,
double& probA,
double& probB)
1929 int *perm =
Malloc(
int, prob->
l);
1930 double *dec_values =
Malloc(
double, prob->
l);
1933 for (i = 0; i < prob->
l; i++) perm[i] = i;
1934 for (i = 0; i < prob->
l; i++)
1936 int j = i + rand() % (prob->
l - i);
1937 swap(perm[i], perm[j]);
1942 int end = (i + 1) * prob->
l / nr_fold;
1946 subprob.
l = prob->
l - (end - begin);
1948 subprob.y =
Malloc(
double, subprob.l);
1951 for (j = 0; j < begin; j++)
1953 subprob.x[k] = prob->
x[perm[j]];
1954 subprob.y[k] = prob->
y[perm[j]];
1957 for (j = end; j < prob->
l; j++)
1959 subprob.x[k] = prob->
x[perm[j]];
1960 subprob.y[k] = prob->
y[perm[j]];
1963 int p_count = 0, n_count = 0;
1964 for (j = 0; j < k; j++)
1965 if (subprob.y[j] > 0)
1970 if (p_count == 0 && n_count == 0)
1971 for (j = begin; j < end; j++)
1972 dec_values[perm[j]] = 0;
1973 else if (p_count > 0 && n_count == 0)
1974 for (j = begin; j < end; j++)
1975 dec_values[perm[j]] = 1;
1976 else if (p_count == 0 && n_count > 0)
1977 for (j = begin; j < end; j++)
1978 dec_values[perm[j]] = -1;
1992 for (j = begin; j < end; j++)
1996 dec_values[perm[j]] *= submodel->
label[0];
2015 double *ymv =
Malloc(
double, prob->
l);
2021 for (i = 0; i < prob->
l; i++)
2023 ymv[i] = prob->
y[i] - ymv[i];
2024 mae += fabs(ymv[i]);
2027 double std = sqrt(2 * mae * mae);
2030 for (i = 0; i < prob->
l; i++)
2031 if (fabs(ymv[i]) > 5 * std)
2034 mae += fabs(ymv[i]);
2035 mae /= (prob->
l - count);
2036 info(
"Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma= %g\n", mae);
2047 int max_nr_class = 16;
2050 int *count =
Malloc(
int, max_nr_class);
2051 int *data_label =
Malloc(
int, l);
2054 for (i = 0; i <
l; i++)
2056 int this_label = (int)prob->
y[i];
2060 if (this_label == label[j])
2069 if (nr_class == max_nr_class)
2072 label = (
int *)realloc(label, max_nr_class *
sizeof(
int));
2073 count = (
int *)realloc(count, max_nr_class *
sizeof(
int));
2084 start[i] = start[i - 1] + count[i - 1];
2085 for (i = 0; i <
l; i++)
2087 perm[start[data_label[i]]] = i;
2088 ++start[data_label[i]];
2092 start[i] = start[i - 1] + count[i - 1];
2116 model->
label = NULL;
2118 model->
probA = NULL;
2119 model->
probB = NULL;
2136 for (i = 0; i < prob->
l; i++)
2143 for (i = 0; i < prob->
l; i++)
2144 if (fabs(f.
alpha[i]) > 0)
2146 model->
SV[j] = prob->
x[i];
2162 int *perm =
Malloc(
int, l);
2171 for (i = 0; i <
l; i++)
2172 x[i] = prob->
x[perm[i]];
2176 double *weighted_C =
Malloc(
double, nr_class);
2178 weighted_C[i] = param->
C;
2186 fprintf(stderr,
"WARNING: class label %d specified in weight is not found\n", param->
weight_label[i]);
2188 weighted_C[j] *= param->
weight[i];
2193 bool *nonzero =
Malloc(
bool, l);
2194 for (i = 0; i <
l; i++)
2201 probA =
Malloc(
double, nr_class * (nr_class - 1) / 2);
2202 probB =
Malloc(
double, nr_class * (nr_class - 1) / 2);
2207 for (
int j = i + 1; j <
nr_class; j++)
2210 int si = start[i], sj = start[j];
2211 int ci = count[i], cj = count[j];
2212 sub_prob.
l = ci + cj;
2214 sub_prob.
y =
Malloc(
double, sub_prob.
l);
2216 for (k = 0; k < ci; k++)
2218 sub_prob.
x[k] = x[si + k];
2221 for (k = 0; k < cj; k++)
2223 sub_prob.
x[ci + k] = x[sj + k];
2224 sub_prob.
y[ci + k] = -1;
2230 f[p] =
svm_train_one(&sub_prob, param, weighted_C[i], weighted_C[j]);
2231 for (k = 0; k < ci; k++)
2232 if (!nonzero[si + k] && fabs(f[p].alpha[k]) > 0)
2233 nonzero[si + k] =
true;
2234 for (k = 0; k < cj; k++)
2235 if (!nonzero[sj + k] && fabs(f[p].alpha[ci + k]) > 0)
2236 nonzero[sj + k] =
true;
2248 model->
label[i] = label[i];
2250 model->
rho =
Malloc(
double, nr_class * (nr_class - 1) / 2);
2251 for (i = 0; i < nr_class * (nr_class - 1) / 2; i++)
2252 model->
rho[i] = f[i].
rho;
2256 model->
probA =
Malloc(
double, nr_class * (nr_class - 1) / 2);
2257 model->
probB =
Malloc(
double, nr_class * (nr_class - 1) / 2);
2258 for (i = 0; i < nr_class * (nr_class - 1) / 2; i++)
2260 model->
probA[i] = probA[i];
2261 model->
probB[i] = probB[i];
2266 model->
probA = NULL;
2267 model->
probB = NULL;
2271 int *nz_count =
Malloc(
int, nr_class);
2276 for (
int j = 0; j < count[i]; j++)
2277 if (nonzero[start[i] + j])
2288 model->
l = total_sv;
2292 for (i = 0; i <
l; i++)
2295 model->
SV[p] = x[i];
2299 int *nz_start =
Malloc(
int, nr_class);
2302 nz_start[i] = nz_start[i - 1] + nz_count[i - 1];
2305 for (i = 0; i < nr_class - 1; i++)
2310 for (
int j = i + 1; j <
nr_class; j++)
2321 int q = nz_start[i];
2323 for (k = 0; k < ci; k++)
2324 if (nonzero[si + k])
2327 for (k = 0; k < cj; k++)
2328 if (nonzero[sj + k])
2342 for (i = 0; i < nr_class * (nr_class - 1) / 2; i++)
2355 int *fold_start =
Malloc(
int, nr_fold + 1);
2357 int *perm =
Malloc(
int, l);
2371 int *fold_count =
Malloc(
int, nr_fold);
2373 int *index =
Malloc(
int, l);
2374 for (i = 0; i <
l; i++)
2377 for (i = 0; i < count[
c]; i++)
2379 int j = i + rand() % (count[
c] - i);
2380 swap(index[start[c] + j], index[start[c] + i]);
2386 fold_count[i] += (i + 1) * count[
c] / nr_fold - i * count[
c] /
nr_fold;
2389 for (i = 1; i <=
nr_fold; i++)
2390 fold_start[i] = fold_start[i - 1] + fold_count[i - 1];
2394 int begin = start[
c] + i * count[
c] /
nr_fold;
2395 int end = start[
c] + (i + 1) * count[c] / nr_fold;
2396 for (
int j = begin; j < end; j++)
2398 perm[fold_start[i]] = index[j];
2403 for (i = 1; i <=
nr_fold; i++)
2404 fold_start[i] = fold_start[i - 1] + fold_count[i - 1];
2413 for (i = 0; i <
l; i++) perm[i] = i;
2414 for (i = 0; i <
l; i++)
2416 int j = i + rand() % (l - i);
2417 swap(perm[i], perm[j]);
2419 for (i = 0; i <=
nr_fold; i++)
2420 fold_start[i] = i * l / nr_fold;
2425 int begin = fold_start[i];
2426 int end = fold_start[i + 1];
2430 subprob.
l = l - (end - begin);
2432 subprob.
y =
Malloc(
double, subprob.
l);
2435 for (j = 0; j < begin; j++)
2437 subprob.
x[k] = prob->
x[perm[j]];
2438 subprob.
y[k] = prob->
y[perm[j]];
2441 for (j = end; j <
l; j++)
2443 subprob.
x[k] = prob->
x[perm[j]];
2444 subprob.
y[k] = prob->
y[perm[j]];
2452 for (j = begin; j < end; j++)
2454 free(prob_estimates);
2457 for (j = begin; j < end; j++)
2459 target[perm[j]] =
svm_predict(submodel, prob->
x[perm[j]]);
2482 if (model->
label != NULL)
2483 for (
int i = 0; i < model->
nr_class; i++)
2484 label[i] = model->
label[i];
2490 for (
int i = 0; i < model->
l; i++)
2502 model->
probA != NULL)
2503 return model->
probA[0];
2506 fprintf(stderr,
"Model doesn't contain information for SVR probability inference\n");
2520 for (i = 0; i < model->
l; i++)
2522 sum -= model->
rho[0];
2526 return (sum > 0) ? 1 : -1;
2535 double *kvalue =
Malloc(
double, l);
2536 for (i = 0; i <
l; i++)
2542 start[i] = start[i - 1] + model->
nSV[i - 1];
2544 int *vote =
Malloc(
int, nr_class);
2550 for (
int j = i + 1; j <
nr_class; j++)
2555 int ci = model->
nSV[i];
2556 int cj = model->
nSV[j];
2559 double *coef1 = model->
sv_coef[j - 1];
2560 double *coef2 = model->
sv_coef[i];
2561 for (k = 0; k < ci; k++)
2562 sum += coef1[si + k] * kvalue[si + k];
2563 for (k = 0; k < cj; k++)
2564 sum += coef2[sj + k] * kvalue[sj + k];
2565 sum -= model->
rho[p];
2566 dec_values[p] = sum;
2568 if (dec_values[p] > 0)
2575 int vote_max_idx = 0;
2577 if (vote[i] > vote[vote_max_idx])
2583 return model->
label[vote_max_idx];
2594 dec_values =
Malloc(
double, 1);
2596 dec_values =
Malloc(
double, nr_class * (nr_class - 1) / 2);
2606 model->
probA != NULL && model->
probB != NULL)
2610 double *dec_values =
Malloc(
double, nr_class * (nr_class - 1) / 2);
2613 double min_prob = 1e-7;
2614 double **pairwise_prob =
Malloc(
double *, nr_class);
2616 pairwise_prob[i] =
Malloc(
double, nr_class);
2619 for (
int j = i + 1; j <
nr_class; j++)
2622 pairwise_prob[j][i] = 1 - pairwise_prob[i][j];
2627 int prob_max_idx = 0;
2629 if (prob_estimates[i] > prob_estimates[prob_max_idx])
2632 free(pairwise_prob[i]);
2634 free(pairwise_prob);
2635 return model->
label[prob_max_idx];
2643 "c_svc",
"nu_svc",
"one_class",
"epsilon_svr",
"nu_svr", NULL
2648 "linear",
"polynomial",
"rbf",
"sigmoid",
"precomputed", NULL
2653 FILE *fp = fopen(model_file_name,
"w");
2654 if (fp == NULL)
return -1;
2656 char *old_locale = strdup(setlocale(LC_ALL, NULL));
2657 setlocale(LC_ALL,
"C");
2665 fprintf(fp,
"degree %d\n", param.
degree);
2668 fprintf(fp,
"gamma %g\n", param.
gamma);
2671 fprintf(fp,
"coef0 %g\n", param.
coef0);
2675 fprintf(fp,
"nr_class %d\n", nr_class);
2676 fprintf(fp,
"total_sv %d\n", l);
2680 for (
int i = 0; i < nr_class * (nr_class - 1) / 2; i++)
2681 fprintf(fp,
" %g", model->
rho[i]);
2687 fprintf(fp,
"label");
2689 fprintf(fp,
" %d", model->
label[i]);
2695 fprintf(fp,
"probA");
2696 for (
int i = 0; i < nr_class * (nr_class - 1) / 2; i++)
2697 fprintf(fp,
" %g", model->
probA[i]);
2702 fprintf(fp,
"probB");
2703 for (
int i = 0; i < nr_class * (nr_class - 1) / 2; i++)
2704 fprintf(fp,
" %g", model->
probB[i]);
2710 fprintf(fp,
"nr_sv");
2712 fprintf(fp,
" %d", model->
nSV[i]);
2716 fprintf(fp,
"SV\n");
2720 for (
int i = 0; i <
l; i++)
2722 for (
int j = 0; j < nr_class - 1; j++)
2723 fprintf(fp,
"%.16g ", sv_coef[j][i]);
2728 fprintf(fp,
"0:%d ", (
int)(p->
value));
2730 while (p->
index != -1)
2738 setlocale(LC_ALL, old_locale);
2741 if (ferror(fp) != 0 || fclose(fp) != 0)
return -1;
2755 while (strrchr(
line,
'\n') == NULL)
2759 len = (int) strlen(
line);
2768 FILE *fp = fopen(model_file_name,
"rb");
2769 if (fp == NULL)
return NULL;
2771 char *old_locale = strdup(setlocale(LC_ALL, NULL));
2772 setlocale(LC_ALL,
"C");
2779 model->
probA = NULL;
2780 model->
probB = NULL;
2781 model->
label = NULL;
2787 fscanf(fp,
"%80s", cmd);
2789 if (strcmp(cmd,
"svm_type") == 0)
2791 fscanf(fp,
"%80s", cmd);
2803 fprintf(stderr,
"unknown svm type.\n");
2805 setlocale(LC_ALL, old_locale);
2814 else if (strcmp(cmd,
"kernel_type") == 0)
2816 fscanf(fp,
"%80s", cmd);
2828 fprintf(stderr,
"unknown kernel function.\n");
2830 setlocale(LC_ALL, old_locale);
2839 else if (strcmp(cmd,
"degree") == 0)
2840 fscanf(fp,
"%d", ¶m.
degree);
2841 else if (strcmp(cmd,
"gamma") == 0)
2842 fscanf(fp,
"%lf", ¶m.
gamma);
2843 else if (strcmp(cmd,
"coef0") == 0)
2844 fscanf(fp,
"%lf", ¶m.
coef0);
2845 else if (strcmp(cmd,
"nr_class") == 0)
2846 fscanf(fp,
"%d", &model->
nr_class);
2847 else if (strcmp(cmd,
"total_sv") == 0)
2848 fscanf(fp,
"%d", &model->
l);
2849 else if (strcmp(cmd,
"rho") == 0)
2853 for (
int i = 0; i < n; i++)
2854 fscanf(fp,
"%lf", &model->
rho[i]);
2856 else if (strcmp(cmd,
"label") == 0)
2860 for (
int i = 0; i < n; i++)
2861 fscanf(fp,
"%d", &model->
label[i]);
2863 else if (strcmp(cmd,
"probA") == 0)
2867 for (
int i = 0; i < n; i++)
2868 fscanf(fp,
"%lf", &model->
probA[i]);
2870 else if (strcmp(cmd,
"probB") == 0)
2874 for (
int i = 0; i < n; i++)
2875 fscanf(fp,
"%lf", &model->
probB[i]);
2877 else if (strcmp(cmd,
"nr_sv") == 0)
2881 for (
int i = 0; i < n; i++)
2882 fscanf(fp,
"%d", &model->
nSV[i]);
2884 else if (strcmp(cmd,
"SV") == 0)
2889 if (c == EOF || c ==
'\n')
break;
2895 fprintf(stderr,
"unknown text in model file: [%s]\n", cmd);
2897 setlocale(LC_ALL, old_locale);
2910 long pos = ftell(fp);
2914 char *p, *endptr, *idx, *val;
2918 p = strtok(
line,
":");
2921 p = strtok(NULL,
":");
2927 elements += model->
l;
2929 fseek(fp, pos, SEEK_SET);
2935 for (i = 0; i < m; i++)
2942 for (i = 0; i <
l; i++)
2945 model->
SV[i] = &x_space[j];
2947 p = strtok(
line,
" \t");
2948 model->
sv_coef[0][i] = strtod(p, &endptr);
2949 for (
int k = 1; k < m; k++)
2951 p = strtok(NULL,
" \t");
2952 model->
sv_coef[k][i] = strtod(p, &endptr);
2957 idx = strtok(NULL,
":");
2958 val = strtok(NULL,
" \t");
2962 x_space[j].
index = (int) strtol(idx, &endptr, 10);
2963 x_space[j].
value = strtod(val, &endptr);
2967 x_space[j++].
index = -1;
2971 setlocale(LC_ALL, old_locale);
2974 if (ferror(fp) != 0 || fclose(fp) != 0)
2983 if (model_ptr->
free_sv && model_ptr->
l > 0 && model_ptr->
SV != NULL)
2984 free((
void *)(model_ptr->
SV[0]));
2987 for (
int i = 0; i < model_ptr->
nr_class - 1; i++)
2991 free(model_ptr->
SV);
2992 model_ptr->
SV = NULL;
2997 free(model_ptr->
rho);
2998 model_ptr->
rho = NULL;
3000 free(model_ptr->
label);
3001 model_ptr->
label = NULL;
3003 free(model_ptr->
probA);
3004 model_ptr->
probA = NULL;
3006 free(model_ptr->
probB);
3007 model_ptr->
probB = NULL;
3009 free(model_ptr->
nSV);
3010 model_ptr->
nSV = NULL;
3015 if (model_ptr_ptr != NULL && *model_ptr_ptr != NULL)
3018 free(*model_ptr_ptr);
3019 *model_ptr_ptr = NULL;
3034 if (svm_type !=
C_SVC &&
3039 return "unknown svm type";
3044 if (kernel_type !=
LINEAR &&
3045 kernel_type !=
POLY &&
3046 kernel_type !=
RBF &&
3049 return "unknown kernel type";
3051 if (param->
gamma < 0)
3055 return "degree of polynomial kernel < 0";
3060 return "cache_size <= 0";
3062 if (param->
eps <= 0)
3065 if (svm_type ==
C_SVC ||
3071 if (svm_type ==
NU_SVC ||
3074 if (param->
nu <= 0 || param->
nu > 1)
3075 return "nu <= 0 or nu > 1";
3083 return "shrinking != 0 and shrinking != 1";
3087 return "probability != 0 and probability != 1";
3091 return "one-class SVM probability output not supported yet";
3099 int max_nr_class = 16;
3102 int *count =
Malloc(
int, max_nr_class);
3105 for (i = 0; i <
l; i++)
3107 int this_label = (int)prob->
y[i];
3110 if (this_label == label[j])
3117 if (nr_class == max_nr_class)
3120 label = (
int *)realloc(label, max_nr_class *
sizeof(
int));
3121 count = (
int *)realloc(count, max_nr_class *
sizeof(
int));
3132 for (
int j = i + 1; j <
nr_class; j++)
3135 if (param->
nu * (n1 + n2) / 2 >
min(n1, n2))
3139 return "specified nu is infeasible";
3153 model->
probA != NULL && model->
probB != NULL) ||
3155 model->
probA != NULL);
3160 if (print_func == NULL)
int svm_save_model(const char *model_file_name, const svm_model *model)
svm_model * svm_train(const svm_problem *prob, const svm_parameter *param)
Qfloat * get_Q(int i, int len) const
static void svm_group_classes(const svm_problem *prob, int *nr_class_ret, int **label_ret, int **start_ret, int **count_ret, int *perm)
static void solve_one_class(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
int get_data(const int index, Qfloat **data, int len)
void swap_index(int i, int j) const
void svm_free_model_content(svm_model *model_ptr)
void Solve(int l, const QMatrix &Q, const double *p, const schar *y, double *alpha, double Cp, double Cn, double eps, SolutionInfo *si, int shrinking)
const char * svm_check_parameter(const svm_problem *prob, const svm_parameter *param)
static double dot(const svm_node *px, const svm_node *py)
struct svm_parameter param
void swap_index(int i, int j)
ONE_CLASS_Q(const svm_problem &prob, const svm_parameter ¶m)
double kernel_sigmoid(int i, int j) const
virtual Qfloat * get_Q(int column, int len) const =0
static double sigmoid_predict(double decision_value, double A, double B)
int select_working_set(int &i, int &j)
static void solve_epsilon_svr(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
int svm_get_nr_sv(const svm_model *model)
Qfloat * get_Q(int i, int len) const
static const char * kernel_type_table[]
void svm_cross_validation(const svm_problem *prob, const svm_parameter *param, int nr_fold, double *target)
static void sigmoid_train(int l, const double *dec_values, const double *labels, double &A, double &B)
double svm_get_svr_probability(const svm_model *model)
virtual int select_working_set(int &i, int &j)
double svm_predict(const svm_model *model, const svm_node *x)
static void info(const char *fmt,...)
static void print_string_stdout(const char *s)
int svm_get_nr_class(const svm_model *model)
Kernel(int l, svm_node *const *x, const svm_parameter ¶m)
int svm_check_probability_model(const svm_model *model)
static void solve_nu_svr(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
static void clone(T *&dst, S *src, int n)
bool is_upper_bound(int i)
void swap_index(int i, int j) const
static void(* svm_print_string)(const char *)
static void svm_binary_svc_probability(const svm_problem *prob, const svm_parameter *param, double Cp, double Cn, double &probA, double &probB)
void lru_insert(head_t *h)
static char * readline(FILE *input)
static double powi(double base, int times)
double(Kernel::* kernel_function)(int i, int j) const
double kernel_linear(int i, int j) const
virtual void swap_index(int i, int j) const
void svm_free_and_destroy_model(svm_model **model_ptr_ptr)
double svm_predict_values(const svm_model *model, const svm_node *x, double *dec_values)
double svm_predict_probability(const svm_model *model, const svm_node *x, double *prob_estimates)
void update_alpha_status(int i)
struct svm_parameter param
static double svm_svr_probability(const svm_problem *prob, const svm_parameter *param)
SVR_Q(const svm_problem &prob, const svm_parameter ¶m)
void svm_get_sv_indices(const svm_model *model, int *indices)
void lru_delete(head_t *h)
void swap_index(int i, int j) const
virtual double * get_QD() const =0
static decision_function svm_train_one(const svm_problem *prob, const svm_parameter *param, double Cp, double Cn)
void swap_index(int i, int j)
SVC_Q(const svm_problem &prob, const svm_parameter ¶m, const schar *y_)
static void multiclass_probability(int k, double **r, double *p)
static void swap(T &x, T &y)
int svm_get_svm_type(const svm_model *model)
virtual void do_shrinking()
double kernel_rbf(int i, int j) const
static double k_function(const svm_node *x, const svm_node *y, const svm_parameter ¶m)
double kernel_poly(int i, int j) const
static void solve_nu_svc(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
bool is_lower_bound(int i)
Qfloat * get_Q(int i, int len) const
static const char * svm_type_table[]
void Solve(int l, const QMatrix &Q, const double *p_, const schar *y_, double *alpha_, double Cp, double Cn, double eps, SolutionInfo *si, int shrinking)
bool be_shrunk(int i, double Gmax1, double Gmax2)
virtual double calculate_rho()
static void solve_c_svc(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, double Cp, double Cn)
svm_model * svm_load_model(const char *model_file_name)
void svm_destroy_param(svm_parameter *param)
struct svm_node * x_space
double kernel_precomputed(int i, int j) const
void svm_get_labels(const svm_model *model, int *label)
bool be_shrunk(int i, double Gmax1, double Gmax2, double Gmax3, double Gmax4)
void reconstruct_gradient()
Cache(int l, long int size)
void svm_set_print_string_function(void(*print_func)(const char *))