KSquare Utilities
SVM289_BFS Namespace Reference

Namespace used to wrap implementation of libSVM version 2.89 to be used as a pair-wise SVM. More...

Classes

class  Cache
 
struct  decision_function
 
class  Kernel
 
class  ONE_CLASS_Q
 
class  QMatrix
 
class  Solver
 
class  Solver_NU
 
class  SVC_Q
 
struct  svm_model
 
struct  svm_parameter
 
struct  svm_problem
 
class  SVR_Q
 

Typedefs

typedef float Qfloat
 
typedef signed char schar
 

Enumerations

enum  Kernel_Type {
  Kernel_NULL, LINEAR, POLY, RBF,
  SIGMOID, PRECOMPUTED
}
 
enum  SVM_Type {
  SVM_Type::SVM_NULL, SVM_Type::C_SVC, SVM_Type::NU_SVC, SVM_Type::ONE_CLASS,
  SVM_Type::EPSILON_SVR, SVM_Type::NU_SVR
}
 

Functions

template<class S , class T >
void clone (T *&dst, S *src, kkint32 n)
 
template<class T >
T * GrowAllocation (T *src, kkint32 origSize, kkint32 newSize)
 
Kernel_Type Kernel_Type_FromStr (KKStr s)
 
KKStr Kernel_Type_ToStr (Kernel_Type kernelType)
 
void multiclass_probability (kkint32 k, double **r, double *p)
 
double powi (double base, kkint32 times)
 
double sigmoid_predict (double decision_value, double A, double B)
 
void sigmoid_train (kkint32 l, const double *dec_values, const double *labels, double &A, double &B)
 
void solve_c_svc (const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, double Cp, double Cn, RunLog &_log)
 
void solve_epsilon_svr (const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, RunLog &_log)
 
void solve_nu_svc (const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, RunLog &_log)
 
void solve_one_class (const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, RunLog &_log)
 
const char * svm_check_parameter (const struct svm_problem *prob, const struct svm_parameter *param)
 
kkint32 svm_check_probability_model (const struct svm_model *model)
 
void svm_cross_validation (const svm_problem &prob, const svm_parameter &param, kkint32 nr_fold, double *target, RunLog &log)
 
void svm_destroy_model (struct svm_model *&model)
 
void svm_destroy_param (struct svm_parameter *&param)
 
void svm_get_labels (const svm_model *model, kkint32 *label)
 
kkint32 svm_get_nr_class (const svm_model *model)
 
SVM_Type svm_get_svm_type (const svm_model *model)
 
double svm_get_svr_probability (const svm_model *model)
 
double svm_predict (const struct svm_model *model, const FeatureVector &x)
 
double svm_predict_probability (svm_model *model, const FeatureVector &x, double *prob_estimates, kkint32 *votes)
 
void svm_predict_values (const svm_model *model, const FeatureVector &x, double *dec_values)
 
svm_modelsvm_train (const svm_problem &prob, const svm_parameter &param, RunLog &log)
 
decision_function svm_train_one (const svm_problem &prob, const svm_parameter &param, double Cp, double Cn, RunLog &_log)
 
SVM_Type SVM_Type_FromStr (KKStr s)
 
KKStr SVM_Type_ToStr (SVM_Type svmType)
 
template<class T >
void swap (T &x, T &y)
 

Variables

kkint32 libsvm_version
 
void(* svm_print_string )(const char *) = &print_string_stdout
 

Detailed Description

Namespace used to wrap implementation of libSVM version 2.89 to be used as a pair-wise SVM.

There is more than one version of libSVM implemented in the library. To prevent name conflicts between them each one was wrapped in their own namespace.
libSVM is a Support Vector Machine implemented done by "Chih-Chung Chang" and "Chih-Jen Lin". It was downloaded from http://www.csie.ntu.edu.tw/~cjlin/libsvm/. The source code was modified by Kurt Kramer. The primary changes to this implementation involves the replacement of the sparse data-structure in the original implementation with fixed length array implemented through the "FeatureVector" class and the ability to specify a sub-set of features to be utilized via the "FeatureNumList" class. This allows us to load in a single set of training data with all its features that can then be used for multiple Support Vector Machine instances where each instance utilizes a different set of features. This particular implementation SVM289_BFS was meant to work with the Binary-Feature-Selection (Pair-Wise) version of the support vector machine as described by "Increased classification accuracy and speedup through pair-wise feature selection for support vector machines."

Typedef Documentation

typedef float SVM289_BFS::Qfloat

Definition at line 269 of file svm289_BFS.h.

typedef signed char SVM289_BFS::schar

Definition at line 271 of file svm289_BFS.h.

Enumeration Type Documentation

Enumerator
Kernel_NULL 
LINEAR 
POLY 
RBF 
SIGMOID 
PRECOMPUTED 

Definition at line 77 of file svm289_BFS.h.

enum SVM289_BFS::SVM_Type
strong
Enumerator
SVM_NULL 
C_SVC 
NU_SVC 
ONE_CLASS 
EPSILON_SVR 
NU_SVR 

Definition at line 67 of file svm289_BFS.h.

68  {
69  SVM_NULL,
70  C_SVC,
71  NU_SVC,
72  ONE_CLASS,
74  NU_SVR
75  }; /* svm_type */

Function Documentation

template<class S , class T >
void SVM289_BFS::clone ( T *&  dst,
S *  src,
kkint32  n 
)
inline

Definition at line 274 of file svm289_BFS.h.

275  {
276  dst = new T[n];
277  memcpy((void *)dst,(void *)src,sizeof(T)*n);
278  }
template<class T >
T* SVM289_BFS::GrowAllocation ( T *  src,
kkint32  origSize,
kkint32  newSize 
)
inline

Definition at line 78 of file svm289_BFS.cpp.

82  {
83  kkint32 zed = 0;
84  T* dest = new T[newSize];
85  while (zed < origSize) {dest[zed] = src[zed]; zed++;}
86  while (zed < newSize) {dest[zed] = (T)0; zed++;}
87  delete src;
88  return dest;
89  } /* GrowAllocation */
__int32 kkint32
Definition: KKBaseTypes.h:88
Kernel_Type SVM289_BFS::Kernel_Type_FromStr ( KKStr  s)

Definition at line 575 of file svm289_BFS.cpp.

References KKB::KKStr::EqualIgnoreCase(), Kernel_NULL, LINEAR, KKB::KKStr::operator==(), POLY, PRECOMPUTED, RBF, SIGMOID, and KKB::KKStr::Upper().

Referenced by SVM289_BFS::svm_parameter::ParseTabDelStr(), and SVM289_BFS::svm_parameter::ProcessSvmParameter().

576  {
577  s.Upper ();
578  if ((s.EqualIgnoreCase ("LINEAR")) || (s == "0"))
579  return LINEAR;
580 
581  if ((s.EqualIgnoreCase ("POLYNOMIAL")) || (s.EqualIgnoreCase ("POLY")) || (s == "1"))
582  return POLY;
583 
584  if ((s.EqualIgnoreCase ("RBF")) || (s == "2"))
585  return RBF;
586 
587  if ((s.EqualIgnoreCase ("SIGMOID")) || (s == "3"))
588  return SIGMOID;
589 
590  if ((s.EqualIgnoreCase ("PRECOMPUTED")) || (s == "4"))
591  return PRECOMPUTED;
592 
593  return Kernel_NULL;
594  }
bool EqualIgnoreCase(const KKStr &s2) const
Definition: KKStr.cpp:1250
#define LINEAR
Definition: UsfCasCor.h:221
void Upper()
Converts all characters in string to their Upper case equivalents via &#39;toupper&#39;.
Definition: KKStr.cpp:2461
#define SIGMOID
Definition: UsfCasCor.h:219
KKStr SVM289_BFS::Kernel_Type_ToStr ( Kernel_Type  kernelType)

Definition at line 600 of file svm289_BFS.cpp.

References LINEAR, POLY, PRECOMPUTED, RBF, and SIGMOID.

Referenced by SVM289_BFS::svm_parameter::ToTabDelStr().

601 {
602  switch (kernelType)
603  {
604  case LINEAR: return "linear";
605  case POLY: return "polynomial";
606  case RBF: return "rbf";
607  case SIGMOID: return "sigmoid";
608  case PRECOMPUTED: return "precomputed";
609  }
610 
611  return "";
612 }
#define LINEAR
Definition: UsfCasCor.h:221
#define SIGMOID
Definition: UsfCasCor.h:219
void SVM289_BFS::multiclass_probability ( kkint32  k,
double **  r,
double *  p 
)
Parameters
kNumber of Classes.
rPairwise Probabilities.
pClass Probability

Definition at line 3051 of file svm289_BFS.cpp.

References info().

3055 {
3056  kkint32 t,j;
3057  kkint32 iter = 0;
3058  kkint32 max_iter = Max (100, k);
3059 
3060  double** Q = new double*[k];
3061  double* Qp = new double[k];
3062  double pQp;
3063  double eps = 0.005 / k;
3064 
3065  for (t = 0; t < k; t++)
3066  {
3067  p[t] = 1.0 / k; // Valid if k = 1
3068  Q[t] = new double[k];
3069  Q[t][t] = 0;
3070  for (j = 0; j < t; j++)
3071  {
3072  Q[t][t] += r[j][t] * r[j][t];
3073  Q[t][j] = Q[j][t];
3074  }
3075 
3076  for (j = t + 1; j < k; j++)
3077  {
3078  Q[t][t] += r[j][t] * r[j][t];
3079  Q[t][j] =- r[j][t] * r[t][j];
3080  }
3081  }
3082 
3083  for (iter = 0; iter <max_iter; iter++)
3084  {
3085  // stopping condition, recalculate QP,pQP for numerical accuracy
3086  pQp = 0;
3087  for (t = 0; t < k; t++)
3088  {
3089  Qp[t] = 0;
3090  for (j = 0; j < k; j++)
3091  Qp[t] += Q[t][j] * p[j];
3092  pQp += p[t] * Qp[t];
3093  }
3094  double max_error = 0;
3095  for (t = 0; t < k; t++)
3096  {
3097  double error = fabs (Qp[t] - pQp);
3098  if (error > max_error)
3099  max_error = error;
3100  }
3101 
3102  if (max_error < eps)
3103  break;
3104 
3105  for (t = 0; t < k; t++)
3106  {
3107  double diff = (-Qp[t] +pQp) / Q[t][t];
3108  p[t] += diff;
3109  pQp = (pQp + diff * (diff * Q[t][t] + 2 * Qp[t])) / (1 + diff) / (1 + diff);
3110  for (j = 0; j < k; j++)
3111  {
3112  Qp[j] = (Qp[j] + diff * Q[t][j]) / (1 + diff);
3113  p[j] /= (1 + diff);
3114  }
3115  }
3116  }
3117  if (iter >= max_iter)
3118  info ("Exceeds max_iter in multiclass_prob\n");
3119 
3120  for (t = 0; t < k; t++)
3121  {delete Q[t]; Q[t] = NULL;}
3122 
3123  delete Q; Q = NULL;
3124  delete Qp; Qp = NULL;
3125 } /* multiclass_probability */
__int32 kkint32
Definition: KKBaseTypes.h:88
static void info(const char *fmt,...)
Definition: svm289_BFS.cpp:626
T Max(T a, T b)
generic Max function, Both parameters must be of the same type.
Definition: KKBaseTypes.h:181
double SVM289_BFS::powi ( double  base,
kkint32  times 
)
inline

Definition at line 93 of file svm289_BFS.cpp.

Referenced by SVM289_BFS::Kernel::k_function().

94  {
95  double tmp = base, ret = 1.0;
96 
97  for (kkint32 t = times; t > 0; t /= 2)
98  {
99  if ((t % 2) == 1)
100  ret *= tmp;
101  tmp = tmp * tmp;
102  }
103  return ret;
104  }
__int32 kkint32
Definition: KKBaseTypes.h:88
double SVM289_BFS::sigmoid_predict ( double  decision_value,
double  A,
double  B 
)

Definition at line 3034 of file svm289_BFS.cpp.

3038 {
3039  double fApB = decision_value * A + B;
3040  if (fApB >= 0)
3041  return exp (-fApB) / (1.0 + exp (-fApB));
3042  else
3043  return 1.0 / (1 + exp (fApB));
3044 } /* sigmoid_predict */
void SVM289_BFS::sigmoid_train ( kkint32  l,
const double *  dec_values,
const double *  labels,
double &  A,
double &  B 
)

Definition at line 2899 of file svm289_BFS.cpp.

References info().

Referenced by svm_binary_svc_probability().

2905 {
2906  double prior1 = 0;
2907  double prior0 = 0;
2908  kkint32 i;
2909 
2910  for (i=0;i<l;i++)
2911  {
2912  if (labels[i] > 0)
2913  prior1 += 1;
2914  else
2915  prior0 += 1;
2916  }
2917 
2918  kkint32 max_iter = 100; // Maximal number of iterations
2919  double min_step = 1e-10; // Minimal step taken in line search
2920  double sigma = 1e-12; // For numerically strict PD of Hessian
2921  double eps = 1e-5;
2922  double hiTarget = (prior1 + 1.0) / (prior1 + 2.0);
2923  double loTarget = 1 / (prior0 + 2.0);
2924  double* t = new double[l];
2925  double fApB, p, q, h11, h22, h21, g1, g2, det, dA, dB, gd, stepsize;
2926  double newA, newB, newf, d1, d2;
2927  kkint32 iter;
2928 
2929  // Initial Point and Initial Fun Value
2930  A = 0.0;
2931  B = log ((prior0 + 1.0) / (prior1 + 1.0));
2932  double fval = 0.0;
2933 
2934  for (i = 0; i < l; i++)
2935  {
2936  if (labels[i] > 0)
2937  t[i] = hiTarget;
2938  else
2939  t[i] = loTarget;
2940 
2941  fApB = dec_values[i] * A + B;
2942 
2943  if (fApB >= 0)
2944  fval += t[i] * fApB + log (1 + exp (-fApB));
2945  else
2946  fval += (t[i] - 1) * fApB + log (1 + exp (fApB));
2947  }
2948 
2949  for (iter=0; iter < max_iter; iter++)
2950  {
2951  // Update Gradient and Hessian (use H' = H + sigma I)
2952  h11 = sigma; // numerically ensures strict PD
2953  h22 = sigma;
2954  h21 = 0.0;
2955  g1 = 0.0;
2956  g2 = 0.0;
2957  for (i = 0; i < l; i++)
2958  {
2959  fApB = dec_values[i] * A + B;
2960  if (fApB >= 0)
2961  {
2962  p = exp (-fApB) / (1.0 + exp(-fApB));
2963  q = 1.0 / (1.0 + exp(-fApB));
2964  }
2965  else
2966  {
2967  p = 1.0 / (1.0 + exp (fApB));
2968  q = exp (fApB) / (1.0 + exp (fApB));
2969  }
2970 
2971  d2 = p * q;
2972  h11 += dec_values[i] * dec_values[i] * d2;
2973  h22 += d2;
2974  h21 += dec_values[i] * d2;
2975  d1 = t[i] - p;
2976  g1 += dec_values[i] * d1;
2977  g2 += d1;
2978  }
2979 
2980  // Stopping Criteria
2981  if ((fabs (g1) < eps) && (fabs(g2) < eps))
2982  break;
2983 
2984  // Finding Newton direction: -inv(H') * g
2985  det = h11 * h22 - h21 * h21;
2986  dA = -(h22*g1 - h21 * g2) / det;
2987  dB = -(-h21 * g1 + h11 * g2) / det;
2988  gd = g1 * dA + g2 * dB;
2989 
2990 
2991  stepsize = 1; // Line Search
2992  while (stepsize >= min_step)
2993  {
2994  newA = A + stepsize * dA;
2995  newB = B + stepsize * dB;
2996 
2997  // New function value
2998  newf = 0.0;
2999  for (i = 0; i < l; i++)
3000  {
3001  fApB = dec_values[i] * newA + newB;
3002  if (fApB >= 0)
3003  newf += t[i] * fApB + log (1 + exp (-fApB));
3004  else
3005  newf += (t[i] - 1) * fApB + log (1 + exp (fApB));
3006  }
3007  // Check sufficient decrease
3008  if (newf < fval + 0.0001 * stepsize * gd)
3009  {
3010  A = newA;
3011  B = newB;
3012  fval = newf;
3013  break;
3014  }
3015  else
3016  stepsize = stepsize / 2.0;
3017  }
3018 
3019  if (stepsize < min_step)
3020  {
3021  info("Line search fails in two-class probability estimates\n");
3022  break;
3023  }
3024  }
3025 
3026  if (iter >= max_iter)
3027  info ("Reaching maximal iterations in two-class probability estimates\n");
3028 
3029  delete[] t; t = NULL;
3030 } /* sigmoid_train */
__int32 kkint32
Definition: KKBaseTypes.h:88
static void info(const char *fmt,...)
Definition: svm289_BFS.cpp:626
void SVM289_BFS::solve_c_svc ( const svm_problem prob,
const svm_parameter param,
double *  alpha,
Solver::SolutionInfo si,
double  Cp,
double  Cn,
RunLog _log 
)

Definition at line 2451 of file svm289_BFS.cpp.

References SVM289_BFS::svm_parameter::eps, SVM289_BFS::svm_problem::l, SVM289_BFS::svm_parameter::shrinking, SVM289_BFS::Solver::Solve(), SVM289_BFS::SVC_Q::SVC_Q(), and SVM289_BFS::svm_problem::y.

Referenced by svm_train_one().

2459 {
2460 
2461  kkint32 l = prob->l;
2462  double* minus_ones = new double[l];
2463  schar* y = new schar[l];
2464 
2465  kkint32 i;
2466 
2467  for (i = 0; i < l; i++)
2468  {
2469  alpha[i] = 0;
2470  minus_ones[i] = -1;
2471  if (prob->y[i] > 0)
2472  y[i] = +1;
2473  else
2474  y[i] = -1;
2475  }
2476 
2477  Solver s;
2478 
2479  SVC_Q* jester = new SVC_Q (*prob, *param, y, _log);
2480 
2481  s.Solve (l,
2482  *jester,
2483  minus_ones,
2484  y,
2485  alpha,
2486  Cp,
2487  Cn,
2488  param->eps,
2489  si,
2490  param->shrinking
2491  );
2492  delete jester;
2493  jester = NULL;
2494 
2495  double sum_alpha =0;
2496 
2497  for (i = 0; i < l; i++)
2498  sum_alpha += alpha[i];
2499 
2500  //if (Cp == Cn)
2501  // info ("nu = %f\n", sum_alpha / (Cp * prob->l));
2502 
2503  for (i = 0; i < l; i++)
2504  alpha[i] *= y[i];
2505 
2506  delete[] minus_ones;
2507  delete[] y;
2508 } /* solve_c_svc */
__int32 kkint32
Definition: KKBaseTypes.h:88
signed char schar
Definition: svm289_BFS.h:271
void Solve(kkint32 l, QMatrix &Q, const double *p_, const schar *y_, double *alpha_, double Cp, double Cn, double eps, SolutionInfo *si, kkint32 shrinking)
void SVM289_BFS::solve_epsilon_svr ( const svm_problem prob,
const svm_parameter param,
double *  alpha,
Solver::SolutionInfo si,
RunLog _log 
)

Definition at line 2650 of file svm289_BFS.cpp.

References SVM289_BFS::svm_parameter::C, SVM289_BFS::svm_parameter::eps, info(), SVM289_BFS::svm_problem::l, SVM289_BFS::svm_parameter::p, SVM289_BFS::svm_parameter::shrinking, SVM289_BFS::Solver::Solve(), SVM289_BFS::SVR_Q::SVR_Q(), and SVM289_BFS::svm_problem::y.

Referenced by svm_train_one().

2656 {
2657  kkint32 l = prob->l;
2658  double* alpha2 = new double [2 * l];
2659  double* linear_term = new double [2 * l];
2660  schar* y = new schar[2*l];
2661  kkint32 i;
2662 
2663  for (i = 0; i < l; i++)
2664  {
2665  alpha2[i] = 0;
2666  linear_term[i] = param->p - prob->y[i];
2667  y[i] = 1;
2668 
2669  alpha2 [i + l] = 0;
2670  linear_term [i + l] = param->p + prob->y[i];
2671  y [i + l] = -1;
2672  }
2673 
2674 
2675  SVR_Q* jester = new SVR_Q (*prob, *param, _log);
2676  Solver s;
2677  s.Solve (2 * l,
2678  *jester,
2679  linear_term,
2680  y,
2681  alpha2,
2682  param->C,
2683  param->C,
2684  param->eps,
2685  si,
2686  param->shrinking
2687  );
2688 
2689  delete jester;
2690  jester = NULL;
2691 
2692  double sum_alpha = 0;
2693  for (i = 0; i < l; i++)
2694  {
2695  alpha[i] = alpha2[i] - alpha2[i+l];
2696  sum_alpha += fabs (alpha[i]);
2697  }
2698 
2699  info ("nu = %f\n", sum_alpha / (param->C * l));
2700 
2701  delete[] alpha2;
2702  delete[] linear_term;
2703  delete[] y;
2704 } /* solve_epsilon_svr */
__int32 kkint32
Definition: KKBaseTypes.h:88
static void info(const char *fmt,...)
Definition: svm289_BFS.cpp:626
signed char schar
Definition: svm289_BFS.h:271
void Solve(kkint32 l, QMatrix &Q, const double *p_, const schar *y_, double *alpha_, double Cp, double Cn, double eps, SolutionInfo *si, kkint32 shrinking)
void SVM289_BFS::solve_nu_svc ( const svm_problem prob,
const svm_parameter param,
double *  alpha,
Solver::SolutionInfo si,
RunLog _log 
)

Definition at line 2513 of file svm289_BFS.cpp.

References SVM289_BFS::svm_parameter::eps, info(), SVM289_BFS::svm_problem::l, SVM289_BFS::svm_parameter::nu, SVM289_BFS::Solver::SolutionInfo::obj, SVM289_BFS::Solver::SolutionInfo::r, SVM289_BFS::Solver::SolutionInfo::rho, SVM289_BFS::svm_parameter::shrinking, SVM289_BFS::Solver_NU::Solve(), SVM289_BFS::SVC_Q::SVC_Q(), SVM289_BFS::Solver::SolutionInfo::upper_bound_n, SVM289_BFS::Solver::SolutionInfo::upper_bound_p, and SVM289_BFS::svm_problem::y.

Referenced by svm_train_one().

2519 {
2520  kkint32 i;
2521  kkint32 l = prob->l;
2522  double nu = param->nu;
2523 
2524  schar *y = new schar[l];
2525 
2526  for (i = 0; i < l; i++)
2527  {
2528  if (prob->y[i] > 0)
2529  y[i] = +1;
2530  else
2531  y[i] = -1;
2532  }
2533 
2534 
2535  double sum_pos = nu * l / 2;
2536  double sum_neg = nu * l / 2;
2537 
2538  for (i = 0; i < l; i++)
2539  {
2540  if (y[i] == +1)
2541  {
2542  alpha[i] = Min(1.0, sum_pos);
2543  sum_pos -= alpha[i];
2544  }
2545  else
2546  {
2547  alpha[i] = Min(1.0,sum_neg);
2548  sum_neg -= alpha[i];
2549  }
2550  }
2551 
2552  double *zeros = new double[l];
2553 
2554  for (i = 0; i < l; i++)
2555  zeros[i] = 0;
2556 
2558 
2559  SVC_Q* jester = new SVC_Q (*prob, *param, y, _log);
2560 
2561  s.Solve (l,
2562  *jester,
2563  zeros,
2564  y,
2565  alpha,
2566  1.0,
2567  1.0,
2568  param->eps,
2569  si,
2570  param->shrinking
2571  );
2572 
2573  delete jester;
2574  jester = NULL;
2575 
2576  double r = si->r;
2577 
2578  info ("C = %f\n",1/r);
2579 
2580  for (i = 0; i < l; i++)
2581  alpha[i] *= y[i] / r;
2582 
2583  si->rho /= r;
2584  si->obj /= (r * r);
2585  si->upper_bound_p = 1 / r;
2586  si->upper_bound_n = 1 / r;
2587 
2588  delete[] y;
2589  delete[] zeros;
2590 } /* solve_nu_svc */
__int32 kkint32
Definition: KKBaseTypes.h:88
static void info(const char *fmt,...)
Definition: svm289_BFS.cpp:626
void Solve(kkint32 l, QMatrix &Q, const double *p, const schar *y, double *alpha, double Cp, double Cn, double eps, SolutionInfo *si, kkint32 shrinking)
signed char schar
Definition: svm289_BFS.h:271
kkint32 Min(kkint32 x1, kkint32 x2)
Definition: Raster.cpp:229
void SVM289_BFS::solve_one_class ( const svm_problem prob,
const svm_parameter param,
double *  alpha,
Solver::SolutionInfo si,
RunLog _log 
)

Definition at line 2595 of file svm289_BFS.cpp.

References SVM289_BFS::svm_parameter::eps, SVM289_BFS::svm_problem::l, SVM289_BFS::svm_parameter::nu, SVM289_BFS::ONE_CLASS_Q::ONE_CLASS_Q(), SVM289_BFS::svm_parameter::shrinking, and SVM289_BFS::Solver::Solve().

Referenced by svm_train_one().

2601 {
2602  kkint32 l = prob->l;
2603 
2604  double* zeros = new double[l];
2605  schar* ones = new schar[l];
2606  kkint32 i;
2607 
2608  kkint32 n = (kkint32)(param->nu * prob->l); // # of alpha's at upper bound
2609 
2610  for (i = 0; i < n; i++)
2611  alpha[i] = 1;
2612 
2613  if (n < prob->l)
2614  alpha[n] = param->nu * prob->l - n;
2615 
2616  for (i = n + 1; i < l; i++)
2617  alpha[i] = 0;
2618 
2619  for (i = 0; i < l; i++)
2620  {
2621  zeros[i] = 0;
2622  ones [i] = 1;
2623  }
2624 
2625  ONE_CLASS_Q* jester = new ONE_CLASS_Q (*prob, *param, _log);
2626 
2627  Solver s;
2628  s.Solve (l,
2629  *jester,
2630  zeros,
2631  ones,
2632  alpha,
2633  1.0,
2634  1.0,
2635  param->eps,
2636  si,
2637  param->shrinking
2638  );
2639 
2640  delete jester;
2641  jester = NULL;
2642 
2643  delete[] zeros;
2644  delete[] ones;
2645 } /* solve_one_class */
__int32 kkint32
Definition: KKBaseTypes.h:88
signed char schar
Definition: svm289_BFS.h:271
void Solve(kkint32 l, QMatrix &Q, const double *p_, const schar *y_, double *alpha_, double Cp, double Cn, double eps, SolutionInfo *si, kkint32 shrinking)
const char* SVM289_BFS::svm_check_parameter ( const struct svm_problem prob,
const struct svm_parameter param 
)
kkint32 SVM289_BFS::svm_check_probability_model ( const struct svm_model model)
void SVM289_BFS::svm_cross_validation ( const svm_problem prob,
const svm_parameter param,
kkint32  nr_fold,
double *  target,
RunLog log 
)

Definition at line 3701 of file svm289_BFS.cpp.

References C_SVC, SVM289_BFS::svm_problem::FileDesc(), SVM289_BFS::svm_problem::l, NU_SVC, SVM289_BFS::svm_parameter::probability, SVM289_BFS::svm_problem::SelFeatures(), svm_destroy_model(), svm_get_nr_class(), svm_group_classes(), SVM289_BFS::svm_problem::svm_problem(), svm_train(), SVM289_BFS::svm_parameter::svm_type, and SVM289_BFS::svm_problem::y.

Referenced by svm_svr_probability().

3707 {
3708  kkint32 i;
3709  kkint32 *fold_start = new kkint32[nr_fold + 1];
3710  kkint32 l = prob.l;
3711  kkint32 *perm = new kkint32[l];
3712  kkint32 nr_class;
3713 
3714  // stratified cv may not give leave-one-out rate
3715  // Each class to l folds -> some folds may have zero elements
3716  if ((param.svm_type == SVM_Type::C_SVC || param.svm_type == SVM_Type::NU_SVC) &&
3717  (nr_fold < l)
3718  )
3719  {
3720  kkint32 *start = NULL;
3721  kkint32 *label = NULL;
3722  kkint32 *count = NULL;
3723  svm_group_classes (&prob, &nr_class, &label, &start, &count, perm);
3724 
3725  // random shuffle and then data grouped by fold using the array perm
3726  kkint32 *fold_count = new kkint32[nr_fold];
3727  kkint32 c;
3728  kkint32 *index = new kkint32[l];
3729  for (i = 0; i < l; i++)
3730  index[i]=perm[i];
3731 
3732  for (c = 0; c < nr_class; c++)
3733  {
3734  for (i = 0; i < count[c]; i++)
3735  {
3736  kkint32 j = i + rand() % (count[c]-i);
3737  SVM289_BFS::swap (index[start[c]+j], index[start[c]+i]);
3738  }
3739  }
3740 
3741  for (i = 0; i < nr_fold; i++)
3742  {
3743  fold_count[i] = 0;
3744  for (c = 0; c < nr_class; c++)
3745  fold_count[i] += (i + 1) * count[c] / nr_fold - i * count[c] / nr_fold;
3746  }
3747 
3748  fold_start[0] = 0;
3749  for (i = 1; i <= nr_fold; i++)
3750  fold_start[i] = fold_start[i-1] + fold_count[i-1];
3751 
3752  for (c=0; c<nr_class;c++)
3753  {
3754  for(i=0;i<nr_fold;i++)
3755  {
3756  kkint32 begin = start[c]+i*count[c]/nr_fold;
3757  kkint32 end = start[c]+(i+1)*count[c]/nr_fold;
3758  for(kkint32 j=begin;j<end;j++)
3759  {
3760  perm[fold_start[i]] = index[j];
3761  fold_start[i]++;
3762  }
3763  }
3764  }
3765 
3766  fold_start[0]=0;
3767  for (i=1;i<=nr_fold;i++)
3768  fold_start[i] = fold_start[i-1]+fold_count[i-1];
3769 
3770  delete[] start; start = NULL;
3771  delete[] label; label = NULL;
3772  delete[] count; count = NULL;
3773  delete[] index; index = NULL;
3774  delete[] fold_count; fold_count = NULL;
3775  }
3776  else
3777  {
3778  for (i = 0; i < l; i++)
3779  perm[i]=i;
3780 
3781  for (i = 0; i < l; i++)
3782  {
3783  kkint32 j = i + rand() % (l - i);
3784  SVM289_BFS::swap (perm[i], perm[j]);
3785  }
3786  for (i = 0; i <= nr_fold; i++)
3787  fold_start[i] = i * l / nr_fold;
3788  }
3789 
3790  for (i = 0; i < nr_fold; i++)
3791  {
3792  kkint32 begin = fold_start[i];
3793  kkint32 end = fold_start[i+1];
3794  kkint32 j,k;
3795 
3796  svm_problem subprob (prob.SelFeatures (), prob.FileDesc (), log);
3797 
3798  subprob.l = l - (end - begin);
3799  //subprob.x = Malloc(struct svm_node*,subprob.l);
3800  // subprob.x will be initilized to an empty FeatureVectorList
3801  subprob.y = new double[subprob.l];
3802 
3803  k = 0;
3804  for (j = 0; j < begin; j++)
3805  {
3806  //subprob.x[k] = prob->x[perm[j]];
3807  subprob.x.PushOnBack (prob.x.IdxToPtr (perm[j]));
3808  subprob.y[k] = prob.y[perm[j]];
3809  ++k;
3810  }
3811 
3812  for (j = end; j < l; j++)
3813  {
3814  //subprob.x[k] = prob->x[perm[j]];
3815  subprob.x.PushOnBack (prob.x.IdxToPtr (perm[j]));
3816  subprob.y[k] = prob.y[perm[j]];
3817  ++k;
3818  }
3819 
3820  svm_model* submodel = svm_train (subprob, param, log);
3821  if (param.probability &&
3822  (param.svm_type == SVM_Type::C_SVC || param.svm_type == SVM_Type::NU_SVC))
3823  {
3824  double *prob_estimates = new double[svm_get_nr_class (submodel)];
3825  kkint32 *votes = new kkint32 [svm_get_nr_class (submodel)];
3826  for (j = begin; j < end; j++)
3827  target[perm[j]] = svm_predict_probability (submodel, prob.x[perm[j]], prob_estimates, votes);
3828  delete[] prob_estimates; prob_estimates = NULL;
3829  delete[] votes; votes = NULL;
3830  }
3831  else
3832  {
3833  for (j = begin; j < end; j++)
3834  target[perm[j]] = svm_predict (submodel, prob.x[perm[j]]);
3835  }
3836 
3837  svm_destroy_model (submodel);
3838  delete submodel;
3839  submodel = NULL;
3840 
3841  //free(subprob.x);
3842  delete subprob.y; subprob.y = NULL;
3843  }
3844 
3845  delete[] fold_start; fold_start = NULL;
3846  delete[] perm; perm = NULL;
3847 } /* svm_cross_validation */
double svm_predict_probability(Svm_Model *model, const FeatureVector &x, double *prob_estimates, kkint32 *votes)
Definition: svm2.cpp:3988
__int32 kkint32
Definition: KKBaseTypes.h:88
EntryPtr IdxToPtr(kkuint32 idx) const
Definition: KKQueue.h:732
void svm_group_classes(const svm_problem *prob, kkint32 *nr_class_ret, kkint32 **label_ret, kkint32 **start_ret, kkint32 **count_ret, kkint32 *perm)
struct SvmModel233 * svm_train(const struct svm_problem *prob, const struct svm_parameter *param)
FeatureVectorList x
Definition: svm289_BFS.h:61
FileDescPtr FileDesc() const
Definition: svm289_BFS.cpp:172
double svm_predict(const struct SvmModel233 *model, const struct svm_node *x)
kkint32 svm_get_nr_class(const SvmModel233 *model)
Definition: svm.cpp:3672
void swap(T &x, T &y)
Definition: svm289_BFS.h:265
void svm_destroy_model(struct SvmModel233 *model)
Definition: svm.cpp:4442
const FeatureNumList & SelFeatures() const
Definition: svm289_BFS.h:56
void SVM289_BFS::svm_destroy_model ( struct svm_model *&  model)

Definition at line 4649 of file svm289_BFS.cpp.

Referenced by svm_cross_validation().

4650 {
4651  //if (model->weOwnSupportVectors && (model->l > 0))
4652  // free ((void *)(model->SV[0]));
4653  if (model->weOwnSupportVectors)
4654  model->SV.Owner (true);
4655  else
4656  model->SV.Owner (false);
4657 
4658  delete model;
4659  model = NULL;
4660 }
FeatureVectorList SV
Definition: svm289_BFS.h:187
bool Owner() const
Definition: KKQueue.h:305
void SVM289_BFS::svm_destroy_param ( struct svm_parameter *&  param)

Definition at line 4664 of file svm289_BFS.cpp.

4665 {
4666  delete param;
4667  param = NULL;
4668 }
void SVM289_BFS::svm_get_labels ( const svm_model model,
kkint32 label 
)

Definition at line 3869 of file svm289_BFS.cpp.

References SVM289_BFS::svm_model::label, and SVM289_BFS::svm_model::nr_class.

3872 {
3873  if (model->label != NULL)
3874  for(kkint32 i=0;i<model->nr_class;i++)
3875  label[i] = model->label[i];
3876 }
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 SVM289_BFS::svm_get_nr_class ( const svm_model model)

Definition at line 3861 of file svm289_BFS.cpp.

References SVM289_BFS::svm_model::nr_class.

Referenced by svm_cross_validation().

3862 {
3863  return model->nr_class;
3864 }
SVM_Type SVM289_BFS::svm_get_svm_type ( const svm_model model)

Definition at line 3854 of file svm289_BFS.cpp.

References SVM289_BFS::svm_model::param, and SVM289_BFS::svm_parameter::svm_type.

3855 {
3856  return model->param.svm_type;
3857 }
svm_parameter param
Definition: svm289_BFS.h:184
double SVM289_BFS::svm_get_svr_probability ( const svm_model model)

Definition at line 3880 of file svm289_BFS.cpp.

References EPSILON_SVR, NU_SVR, SVM289_BFS::svm_model::param, SVM289_BFS::svm_model::probA, and SVM289_BFS::svm_parameter::svm_type.

3881 {
3882  if ((model->param.svm_type == SVM_Type::EPSILON_SVR || model->param.svm_type == SVM_Type::NU_SVR) &&
3883  model->probA!=NULL)
3884  return model->probA[0];
3885  else
3886  {
3887  fprintf(stderr,"Model doesn't contain information for SVR probability inference\n");
3888  return 0;
3889  }
3890 }
svm_parameter param
Definition: svm289_BFS.h:184
double SVM289_BFS::svm_predict ( const struct svm_model model,
const FeatureVector x 
)

Referenced by svm_predict_probability().

double SVM289_BFS::svm_predict_probability ( svm_model model,
const FeatureVector x,
double *  prob_estimates,
kkint32 votes 
)

Definition at line 4027 of file svm289_BFS.cpp.

References C_SVC, SVM289_BFS::svm_model::DecValues(), SVM289_BFS::svm_model::label, SVM289_BFS::svm_model::NormalizeProbability(), SVM289_BFS::svm_model::nr_class, NU_SVC, SVM289_BFS::svm_model::PairwiseProb(), SVM289_BFS::svm_model::param, SVM289_BFS::svm_model::probA, SVM289_BFS::svm_model::probB, SVM289_BFS::svm_model::ProbEstimates(), SVM289_BFS::svm_parameter::probParam, svm_predict(), svm_predict_values(), and SVM289_BFS::svm_parameter::svm_type.

4032 {
4033  double probParam = model->param.probParam;
4034 
4035  if ((model->param.svm_type == SVM_Type::C_SVC || model->param.svm_type == SVM_Type::NU_SVC) &&
4036  ((model->probA != NULL && model->probB != NULL) || (probParam > 0.0))
4037  )
4038  {
4039  kkint32 i;
4040  kkint32 nr_class = model->nr_class;
4041 
4042  double* prob_estimates = model->ProbEstimates ();
4043  double* dec_values = model->DecValues ();
4044  double** pairwise_prob = model->PairwiseProb ();
4045 
4046  for (i = 0; i < nr_class; i++)
4047  votes[i] = 0;
4048 
4049  svm_predict_values (model, x, dec_values);
4050 
4051  double min_prob = 1e-7;
4052 
4053  kkint32 k=0;
4054  for (i = 0; i < nr_class; i++)
4055  {
4056  for (kkint32 j = i + 1; j < nr_class; j++)
4057  {
4058  if (probParam > 0.0)
4059  {
4060  double probability = (double)(1.0 / (1.0 + exp (-1.0 * probParam * dec_values[k])));
4061  pairwise_prob[i][j] = Min (Max (probability, min_prob), 1 - min_prob);
4062  pairwise_prob[j][i] = 1 - pairwise_prob[i][j];
4063  }
4064  else
4065  {
4066  pairwise_prob[i][j] = Min (Max (sigmoid_predict (dec_values[k], model->probA[k], model->probB[k]), min_prob), 1 - min_prob);
4067  pairwise_prob[j][i] = 1 - pairwise_prob[i][j];
4068  }
4069 
4070  if (pairwise_prob[i][j] > 0.5)
4071  votes[model->label[i]]++;
4072  else
4073  votes[model->label[j]]++;
4074 
4075  k++;
4076  }
4077  }
4078 
4079  // The 'pairwise_prob' and 'prob_estimates' variables are actually located
4080  // in 'model'. So by calling 'NormalizeProbability' we normalize
4081  // 'prob_estimates'.
4082  model->NormalizeProbability ();
4083 
4084  //multiclass_probability (nr_class, pairwise_prob, prob_estimates);
4085 
4086  kkint32 prob_max_idx = 0;
4087  for (i = 1; i < nr_class; i++)
4088  {
4089  if (prob_estimates[i] > prob_estimates[prob_max_idx])
4090  prob_max_idx = i;
4091  }
4092 
4093  for (i = 0; i < nr_class; i++)
4094  classProbabilities[model->label[i]] = prob_estimates[i];
4095 
4096  return model->label[prob_max_idx];
4097  }
4098  else
4099  {
4100  return svm_predict (model, x);
4101  }
4102 } /* svm_predict_probability */
__int32 kkint32
Definition: KKBaseTypes.h:88
double sigmoid_predict(double decision_value, double A, double B)
void NormalizeProbability()
Derining multiclass probability as done in "Recognizing Plankton Images From the SIPPER".
double svm_predict(const struct SvmModel233 *model, const struct svm_node *x)
T Max(T a, T b)
generic Max function, Both parameters must be of the same type.
Definition: KKBaseTypes.h:181
double ** PairwiseProb()
svm_parameter param
Definition: svm289_BFS.h:184
kkint32 Min(kkint32 x1, kkint32 x2)
Definition: Raster.cpp:229
void svm_predict_values(const Svm_Model *model, const FeatureVector &x, double *dec_values)
Definition: svm2.cpp:3856
void SVM289_BFS::svm_predict_values ( const svm_model model,
const FeatureVector x,
double *  dec_values 
)

Definition at line 3895 of file svm289_BFS.cpp.

References EPSILON_SVR, SVM289_BFS::svm_model::l, SVM289_BFS::svm_model::nr_class, SVM289_BFS::svm_model::nSV, NU_SVR, ONE_CLASS, SVM289_BFS::svm_model::param, SVM289_BFS::svm_model::rho, SVM289_BFS::svm_model::sv_coef, and SVM289_BFS::svm_parameter::svm_type.

Referenced by svm_predict_probability().

3899 {
3900  if (model->param.svm_type == SVM_Type::ONE_CLASS ||
3901  model->param.svm_type == SVM_Type::EPSILON_SVR ||
3902  model->param.svm_type == SVM_Type::NU_SVR
3903  )
3904  {
3905  double *sv_coef = model->sv_coef[0];
3906  double sum = 0;
3907  for (kkint32 i = 0; i < model->l; i++)
3908  sum += sv_coef[i] * Kernel::k_function (x,
3909  model->SV[i],
3910  model->param,
3911  model->selFeatures
3912  );
3913  sum -= model->rho[0];
3914  *dec_values = sum;
3915  }
3916  else
3917  {
3918  kkint32 i;
3919  kkint32 nr_class = model->nr_class;
3920  kkint32 l = model->l;
3921 
3922  double *kvalue = new double[l];
3923  for (i = 0; i < l; i++)
3924  kvalue[i] = Kernel::k_function (x, model->SV[i], model->param, model->selFeatures);
3925 
3926  kkint32 *start = new kkint32[nr_class];
3927  start[0] = 0;
3928  for (i = 1; i < nr_class; i++)
3929  start[i] = start[i-1]+model->nSV[i-1];
3930 
3931  kkint32 p=0;
3932  for (i = 0; i < nr_class; i++)
3933  {
3934  for (kkint32 j = i + 1; j < nr_class; j++)
3935  {
3936  double sum = 0;
3937  kkint32 si = start[i];
3938  kkint32 sj = start[j];
3939  kkint32 ci = model->nSV[i];
3940  kkint32 cj = model->nSV[j];
3941 
3942  kkint32 k;
3943  double *coef1 = model->sv_coef[j - 1];
3944  double *coef2 = model->sv_coef[i];
3945  for (k = 0; k < ci; k++)
3946  sum += coef1[si + k] * kvalue[si + k];
3947 
3948 
3949  for (k = 0; k < cj; k++)
3950  sum += coef2[sj + k] * kvalue[sj + k];
3951 
3952  sum -= model->rho[p];
3953  dec_values[p] = sum;
3954  p++;
3955  }
3956  }
3957 
3958  delete kvalue; kvalue = NULL;
3959  delete start; start = NULL;
3960  }
3961 } /* svm_predict_values */
FeatureVectorList SV
Definition: svm289_BFS.h:187
__int32 kkint32
Definition: KKBaseTypes.h:88
FeatureNumList selFeatures
Definition: svm289_BFS.h:192
svm_parameter param
Definition: svm289_BFS.h:184
svm_model * SVM289_BFS::svm_train ( const svm_problem prob,
const svm_parameter param,
RunLog log 
)

Definition at line 3385 of file svm289_BFS.cpp.

References SVM289_BFS::decision_function::alpha, SVM289_BFS::svm_parameter::C, EPSILON_SVR, KKMLL::FeatureVectorList::FeatureVectorList(), SVM289_BFS::svm_problem::FileDesc(), info(), SVM289_BFS::svm_problem::l, SVM289_BFS::svm_model::l, SVM289_BFS::svm_model::label, SVM289_BFS::svm_model::nr_class, SVM289_BFS::svm_parameter::nr_weight, SVM289_BFS::svm_model::nSV, NU_SVR, ONE_CLASS, SVM289_BFS::svm_model::probA, SVM289_BFS::svm_parameter::probability, SVM289_BFS::svm_model::probB, SVM289_BFS::svm_model::rho, SVM289_BFS::decision_function::rho, SVM289_BFS::svm_problem::SelFeatures(), SVM289_BFS::svm_model::sv_coef, svm_binary_svc_probability(), svm_group_classes(), SVM289_BFS::svm_model::svm_model(), SVM289_BFS::svm_problem::svm_problem(), svm_svr_probability(), svm_train_one(), SVM289_BFS::svm_parameter::svm_type, SVM289_BFS::svm_parameter::weight, SVM289_BFS::svm_parameter::weight_label, SVM289_BFS::svm_model::weOwnSupportVectors, and SVM289_BFS::svm_problem::y.

Referenced by svm_binary_svc_probability(), and svm_cross_validation().

3389 {
3390  svm_model* model = new svm_model (param, prob.SelFeatures (), prob.FileDesc (), log);
3391 
3392  model->weOwnSupportVectors = false; // XXX
3393 
3394  if ((param.svm_type == SVM_Type::ONE_CLASS) ||
3395  (param.svm_type == SVM_Type::EPSILON_SVR) ||
3396  (param.svm_type == SVM_Type::NU_SVR)
3397  )
3398  {
3399  // regression or one-class-svm
3400  model->nr_class = 2;
3401  model->label = NULL;
3402  model->nSV = NULL;
3403  model->probA = NULL;
3404  model->probB = NULL;
3405  model->sv_coef = new double*[1];
3406 
3407  if (param.probability && (param.svm_type == SVM_Type::EPSILON_SVR || param.svm_type == SVM_Type::NU_SVR))
3408  {
3409  model->probA = new double[1];
3410  model->probA[0] = svm_svr_probability (prob, param, log);
3411  }
3412 
3413  decision_function f = svm_train_one (prob, param, 0, 0, log);
3414  model->rho = new double[1];
3415  model->rho[0] = f.rho;
3416 
3417  kkint32 nSV = 0;
3418  kkint32 i;
3419  for (i = 0; i < prob.l; i++)
3420  {
3421  if (fabs(f.alpha[i]) > 0)
3422  ++nSV;
3423  }
3424 
3425  model->l = nSV;
3426  //model->SV = Malloc(svm_node *,nSV);
3427  // model->SV is now a FeatureVectorList object that was initialized to empty and not owner in the consructor
3428  model->SV.Owner (true);
3429  model->sv_coef[0] = new double[nSV];
3430  kkint32 j = 0;
3431  for (i = 0; i < prob.l; i++)
3432  {
3433  if (fabs (f.alpha[i]) > 0)
3434  {
3435  //model->SV[j] = prob->x[i];
3436  model->SV.PushOnBack (new FeatureVector (prob.x[i]));
3437  model->sv_coef[0][j] = f.alpha[i];
3438  ++j;
3439  }
3440  }
3441 
3442  delete f.alpha; f.alpha = NULL;
3443  }
3444  else
3445  {
3446  // Classification
3447  kkint32 l = prob.l;
3448  kkint32 nr_class;
3449  kkint32 *label = NULL;
3450  kkint32 *start = NULL;
3451  kkint32 *count = NULL;
3452  kkint32 *perm = new kkint32[l];
3453 
3454  // group training data of the same class
3455  svm_group_classes (&prob,
3456  &nr_class,
3457  &label,
3458  &start,
3459  &count,
3460  perm
3461  );
3462 
3463  kkint32 numBinaryCombos = nr_class * (nr_class - 1) / 2;
3464 
3465  //svm_node **x = Malloc(svm_node *,l);
3466  FeatureVectorList x (prob.FileDesc (), false);
3467 
3468  kkint32 i;
3469  for (i = 0; i < l; i++)
3470  {
3471  //x[i] = prob->x[perm[i]];
3472  x.PushOnBack (prob.x.IdxToPtr (perm[i]));
3473  }
3474 
3475  // calculate weighted C
3476  double* weighted_C = new double[nr_class];
3477  for (i = 0; i < nr_class; i++)
3478  weighted_C[i] = param.C;
3479 
3480  for (i = 0; i < param.nr_weight; i++)
3481  {
3482  kkint32 j;
3483  for (j = 0; j < nr_class; j++)
3484  {
3485  if (param.weight_label[i] == label[j])
3486  break;
3487  }
3488 
3489  if (j == nr_class)
3490  fprintf(stderr,"warning: class label %d specified in weight is not found\n", param.weight_label[i]);
3491  else
3492  weighted_C[j] *= param.weight[i];
3493  }
3494 
3495  // train k*(k-1)/2 models
3496 
3497  bool *nonzero = new bool[l];
3498 
3499  for (i = 0; i < l; i++)
3500  nonzero[i] = false;
3501 
3502  decision_function *f = new decision_function[numBinaryCombos];
3503 
3504  double* probA = NULL;
3505  double* probB = NULL;
3506 
3507  if (param.probability)
3508  {
3509  probA = new double[numBinaryCombos];
3510  probB = new double[numBinaryCombos];
3511  }
3512 
3513  kkint32 p = 0;
3514  for (i = 0; i < nr_class; i++)
3515  {
3516  for (kkint32 j = i + 1; j < nr_class; j++)
3517  {
3518  svm_problem sub_prob (prob.SelFeatures (), prob.FileDesc (), log);
3519  kkint32 si = start[i], sj = start[j];
3520  kkint32 ci = count[i], cj = count[j];
3521  sub_prob.l = ci + cj;
3522  //sub_prob.x = Malloc (svm_node *,sub_prob.l);
3523  sub_prob.y = new double[sub_prob.l];
3524  kkint32 k;
3525  for (k = 0; k < ci; k++)
3526  {
3527  //sub_prob.x[k] = x[si+k];
3528  sub_prob.x.PushOnBack (x.IdxToPtr (si + k));
3529  sub_prob.y[k] = +1;
3530  }
3531  for (k = 0; k < cj; k++)
3532  {
3533  //sub_prob.x[ci+k] = x[sj+k];
3534  sub_prob.x.PushOnBack (x.IdxToPtr (sj + k));
3535  sub_prob.y[ci + k] = -1;
3536  }
3537 
3538  if (param.probability)
3539  svm_binary_svc_probability (&sub_prob, &param, weighted_C[i], weighted_C[j], probA[p], probB[p], log);
3540 
3541 
3542  f[p] = svm_train_one (sub_prob, param, weighted_C[i], weighted_C[j], log);
3543 
3544  for (k = 0; k < ci; k++)
3545  {
3546  if (!nonzero[si + k] && fabs(f[p].alpha[k]) > 0)
3547  nonzero[si + k] = true;
3548  }
3549 
3550  for (k = 0; k < cj; k++)
3551  {
3552  if (!nonzero[sj + k] && fabs(f[p].alpha[ci+k]) > 0)
3553  nonzero[sj + k] = true;
3554  }
3555 
3556  //free(sub_prob.x);
3557  delete sub_prob.y;
3558  sub_prob.y = NULL;
3559  ++p;
3560  }
3561  }
3562 
3563 
3564  // At this point all the Binary Classifiers have been built. They are now going
3565  // to be packaged into one not so neat model.
3566 
3567  // build output
3568  model->nr_class = nr_class;
3569 
3570  model->label = new kkint32[nr_class];
3571  for (i = 0; i < nr_class; i++)
3572  model->label[i] = label[i];
3573 
3574  model->rho = new double[numBinaryCombos];
3575  for (i = 0; i < numBinaryCombos; i++)
3576  model->rho[i] = f[i].rho;
3577 
3578  if (param.probability)
3579  {
3580  model->probA = new double[numBinaryCombos];
3581  model->probB = new double[numBinaryCombos];
3582  for (i = 0; i < numBinaryCombos; i++)
3583  {
3584  model->probA[i] = probA[i];
3585  model->probB[i] = probB[i];
3586  }
3587  }
3588  else
3589  {
3590  model->probA = NULL;
3591  model->probB = NULL;
3592  }
3593 
3594  kkint32 total_sv = 0;
3595  kkint32* nz_count = new kkint32[nr_class];
3596 
3597  model->nSV = new kkint32[nr_class];
3598  for (i = 0; i < nr_class; i++)
3599  {
3600  kkint32 nSV = 0;
3601  for (kkint32 j = 0; j < count[i]; j++)
3602  {
3603  if (nonzero[start[i] + j])
3604  {
3605  ++nSV;
3606  ++total_sv;
3607  }
3608  }
3609  model->nSV[i] = nSV;
3610  nz_count[i] = nSV;
3611  }
3612 
3613  info("Total nSV = %d\n",total_sv);
3614 
3615  model->l = total_sv;
3616  //model->SV = Malloc(svm_node *, total_sv);
3617  model->SV.DeleteContents ();
3618  model->SV.Owner (false);
3619  model->weOwnSupportVectors = false;
3620 
3621  p = 0;
3622  for (i = 0; i < l; i++)
3623  {
3624  if (nonzero[i])
3625  {
3626  //model->SV[p++] = x[i];
3627  model->SV.PushOnBack (x.IdxToPtr (i));
3628  p++;
3629  }
3630  }
3631 
3632  kkint32 *nz_start = new kkint32[nr_class];
3633  nz_start[0] = 0;
3634  for (i = 1; i < nr_class; i++)
3635  nz_start[i] = nz_start[i - 1] + nz_count[i - 1];
3636 
3637  model->sv_coef = new double*[nr_class - 1];
3638  for (i = 0; i < nr_class - 1; i++)
3639  model->sv_coef[i] = new double[total_sv];
3640 
3641  p = 0;
3642  for (i = 0; i < nr_class; i++)
3643  {
3644  for (kkint32 j = i + 1; j < nr_class; j++)
3645  {
3646  // classifier (i,j): coefficients with
3647  // i are in sv_coef[j-1][nz_start[i]...],
3648  // j are in sv_coef[i][nz_start[j]...]
3649 
3650  kkint32 si = start[i];
3651  kkint32 sj = start[j];
3652  kkint32 ci = count[i];
3653  kkint32 cj = count[j];
3654 
3655  kkint32 q = nz_start[i];
3656  kkint32 k;
3657 
3658  for (k = 0; k < ci; k++)
3659  {
3660  if (nonzero[si + k])
3661  model->sv_coef[j - 1][q++] = f[p].alpha[k];
3662  }
3663 
3664  q = nz_start[j];
3665  for (k = 0; k < cj; k++)
3666  {
3667  if (nonzero[sj + k])
3668  model->sv_coef[i][q++] = f[p].alpha[ci + k];
3669  }
3670  ++p;
3671  }
3672  }
3673 
3674  delete[] label; label = NULL;
3675  delete[] probA; probA = NULL;
3676  delete[] probB; probB = NULL;
3677  delete[] count; count = NULL;
3678  delete[] perm; perm = NULL;
3679  delete[] start; start = NULL;
3680  //free (x);
3681  delete[] weighted_C; weighted_C = NULL;
3682  delete[] nonzero; nonzero = NULL;
3683  for (i = 0; i < numBinaryCombos; i++)
3684  {
3685  delete f[i].alpha;
3686  f[i].alpha = NULL;
3687  }
3688  delete[] f; f = NULL;
3689  delete[] nz_count; nz_count = NULL;
3690  delete[] nz_start; nz_start = NULL;
3691  }
3692 
3693  return model;
3694 } /* svm_train */
FeatureVectorList SV
Definition: svm289_BFS.h:187
void PushOnBack(FeatureVectorPtr image)
Overloading the PushOnBack function in KKQueue so we can monitor the Version and Sort Order...
__int32 kkint32
Definition: KKBaseTypes.h:88
EntryPtr IdxToPtr(kkuint32 idx) const
Definition: KKQueue.h:732
static void info(const char *fmt,...)
Definition: svm289_BFS.cpp:626
void svm_group_classes(const svm_problem *prob, kkint32 *nr_class_ret, kkint32 **label_ret, kkint32 **start_ret, kkint32 **count_ret, kkint32 *perm)
void DeleteContents()
Definition: KKQueue.h:321
FeatureVectorList x
Definition: svm289_BFS.h:61
bool Owner() const
Definition: KKQueue.h:305
Container class for FeatureVector derived objects.
FileDescPtr FileDesc() const
Definition: svm289_BFS.cpp:172
double svm_svr_probability(const svm_problem &prob, const svm_parameter &param, RunLog &log)
void svm_binary_svc_probability(const svm_problem *prob, const svm_parameter *param, double Cp, double Cn, double &probA, double &probB, RunLog &log)
decision_function svm_train_one(const svm_problem &prob, const svm_parameter &param, double Cp, double Cn, RunLog &_log)
Represents a Feature Vector of a single example, labeled or unlabeled.
Definition: FeatureVector.h:59
const FeatureNumList & SelFeatures() const
Definition: svm289_BFS.h:56
decision_function SVM289_BFS::svm_train_one ( const svm_problem prob,
const svm_parameter param,
double  Cp,
double  Cn,
RunLog _log 
)

Definition at line 2781 of file svm289_BFS.cpp.

References SVM289_BFS::decision_function::alpha, C_SVC, KKB::KKStr::Concat(), EPSILON_SVR, KKB::KKException::KKException(), SVM289_BFS::svm_problem::l, NU_SVC, NU_SVR, ONE_CLASS, SVM289_BFS::Solver::SolutionInfo::rho, SVM289_BFS::decision_function::rho, solve_c_svc(), solve_epsilon_svr(), solve_nu_svc(), solve_nu_svr(), solve_one_class(), SVM289_BFS::svm_parameter::svm_type, and SVM289_BFS::svm_problem::y.

Referenced by svm_train().

2787 {
2788  double* alpha = new double [prob.l];
2790 
2791  switch (param.svm_type)
2792  {
2793  case SVM_Type::C_SVC:
2794  solve_c_svc (&prob, &param, alpha, &si, Cp, Cn, _log);
2795  break;
2796 
2797  case SVM_Type::NU_SVC:
2798  solve_nu_svc (&prob, &param, alpha, &si, _log);
2799  break;
2800 
2801  case SVM_Type::ONE_CLASS:
2802  solve_one_class (&prob, &param, alpha, &si, _log);
2803  break;
2804 
2805  case SVM_Type::EPSILON_SVR:
2806  solve_epsilon_svr (&prob, &param, alpha, &si, _log);
2807  break;
2808 
2809  case SVM_Type::NU_SVR:
2810  solve_nu_svr (&prob, &param, alpha, &si, _log);
2811  break;
2812 
2813  default:
2814  {
2815  KKStr errMsg = "SVM289_BFS::svm_train_one ***ERROR*** Invalid Solver Defined.";
2816  errMsg << " Solver[" << (int)param.svm_type << "]";
2817  _log.Level (-1) << endl << endl << errMsg << endl << endl;
2818  throw KKException (errMsg);
2819  }
2820  }
2821 
2822  //info ("obj = %f, rho = %f\n", si.obj, si.rho);
2823 
2824  // output SVs
2825 
2826  std::vector<kkint32> SVIndex; // Normalize by Margin Width(NMW).
2827 
2828  kkint32 nSV = 0;
2829  kkint32 nBSV = 0;
2830  for (kkint32 i = 0; i < prob.l; i++)
2831  {
2832  if (fabs (alpha[i]) > 0)
2833  {
2834  ++nSV;
2835  SVIndex.push_back (i); // NMW
2836  if (prob.y[i] > 0)
2837  {
2838  if (fabs (alpha[i]) >= si.upper_bound_p)
2839  {
2840  ++nBSV;
2841  // BSVIndex.insert (prob->index[i]); // NMW
2842  }
2843  }
2844  else
2845  {
2846  if (fabs (alpha[i]) >= si.upper_bound_n)
2847  {
2848  ++nBSV;
2849  // BSVIndex.insert (prob->index[i]);
2850  }
2851  }
2852  }
2853  }
2854 
2855 
2856  //**********************************************************************************
2857  // Code to normalize by margin width.
2858 
2859 
2860  double sum=0.0;
2861  std::vector<kkint32>::iterator it,it2;
2862  double kvalue = 0.0;
2863 
2864  for (it = SVIndex.begin(); it < SVIndex.end(); it++)
2865  {
2866  for (it2 = SVIndex.begin(); it2 < SVIndex.end(); it2++)
2867  {
2868  kkint32 k = *it;
2869  kkint32 kk = *it2;
2870 
2871  kvalue = Kernel::k_function (prob.x[k], prob.x[kk], param, prob.SelFeatures ());
2872 
2873  sum += prob.y[k] * prob.y[kk] * alpha[k] * alpha[kk] * kvalue;
2874  }
2875  }
2876 
2877  sum /= SVIndex.size();
2878  sum = sqrt(sum);
2879 
2880  for (it = SVIndex.begin(); it < SVIndex.end(); it++)
2881  alpha[*it] /= sum;
2882 
2883  si.rho /= sum;
2884 
2885  //info ("nSV = %d, nBSV = %d\n", nSV, nBSV);
2886 
2888  f.alpha = alpha;
2889  f.rho = si.rho;
2890  return f;
2891 } /* svm_train_one */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
void solve_epsilon_svr(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, RunLog &_log)
RunLog & Level(kkint32 _level)
Definition: RunLog.cpp:220
FeatureVectorList x
Definition: svm289_BFS.h:61
void solve_one_class(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, RunLog &_log)
static void solve_nu_svr(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, RunLog &_log)
void solve_nu_svc(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, RunLog &_log)
void solve_c_svc(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, double Cp, double Cn, RunLog &_log)
const FeatureNumList & SelFeatures() const
Definition: svm289_BFS.h:56
SVM_Type SVM289_BFS::SVM_Type_FromStr ( KKStr  s)

Definition at line 536 of file svm289_BFS.cpp.

References C_SVC, EPSILON_SVR, KKB::KKStr::EqualIgnoreCase(), NU_SVC, NU_SVR, ONE_CLASS, KKB::KKStr::operator==(), SVM_NULL, and KKB::KKStr::Upper().

Referenced by SVM289_BFS::svm_parameter::ParseTabDelStr(), and SVM289_BFS::svm_parameter::ProcessSvmParameter().

537 {
538  s.Upper ();
539 
540  if ((s.EqualIgnoreCase ("C_SVC")) || (s == "0"))
541  return SVM_Type::C_SVC;
542 
543  if ((s.EqualIgnoreCase ("NU_SVC")) || (s == "1"))
544  return SVM_Type::NU_SVC;
545 
546  if ((s.EqualIgnoreCase ("ONE_CLASS")) || (s == "2"))
547  return SVM_Type::ONE_CLASS;
548 
549  if ((s.EqualIgnoreCase ("EPSILON_SVR")) || (s == "3"))
550  return SVM_Type::EPSILON_SVR;
551 
552  if ((s.EqualIgnoreCase ("NU_SVR")) || (s == "4"))
553  return SVM_Type::NU_SVR;
554 
555  return SVM_Type::SVM_NULL;
556 }
bool EqualIgnoreCase(const KKStr &s2) const
Definition: KKStr.cpp:1250
void Upper()
Converts all characters in string to their Upper case equivalents via &#39;toupper&#39;.
Definition: KKStr.cpp:2461
KKStr SVM289_BFS::SVM_Type_ToStr ( SVM_Type  svmType)

Definition at line 560 of file svm289_BFS.cpp.

References C_SVC, EPSILON_SVR, NU_SVC, NU_SVR, and ONE_CLASS.

Referenced by SVM289_BFS::svm_parameter::ToTabDelStr().

561 {
562  switch (svmType)
563  {
564  case SVM_Type::C_SVC: return "c_svc";
565  case SVM_Type::NU_SVC: return "nu_svc";
566  case SVM_Type::ONE_CLASS: return "one_class";
567  case SVM_Type::EPSILON_SVR: return "epsilon_svr";
568  case SVM_Type::NU_SVR: return "nu_svr";
569  }
570  return "";
571 }
template<class T >
void SVM289_BFS::swap ( T &  x,
T &  y 
)
inline

Definition at line 265 of file svm289_BFS.h.

265 { T t=x; x=y; y=t; }

Variable Documentation

kkint32 SVM289_BFS::libsvm_version
void(* SVM289_BFS::svm_print_string)(const char *) = &print_string_stdout

Definition at line 624 of file svm289_BFS.cpp.