KSquare Utilities
SVM233 Namespace Reference

This is version 2.33 of "Chih-Chung Chang" and "Chih-Jen Lin" Support vector Machine; the class "ModelOldSVM" calls ths version. More...

Classes

class  Cache
 
struct  decision_function
 
class  Kernel
 Kernel evaluation. More...
 
class  ONE_CLASS_Q
 
class  Solver
 
class  Solver_NU
 
class  SVC_Q
 
struct  svm_node
 
struct  svm_parameter
 
struct  svm_problem
 
struct  SvmModel233
 
class  SVR_Q
 

Typedefs

typedef signed char schar
 
typedef XmlElementTemplate< SvmModel233XmlElementSvmModel233
 
typedef XmlElementSvmModel233XmlElementSvmModel233Ptr
 

Enumerations

enum  {
  C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR,
  NU_SVR
}
 
enum  { LINEAR, POLY, RBF, SIGMOID }
 

Functions

template<class S , class T >
void clone (T *&dst, S *src, kkint32 n)
 
void info (const char *fmt,...)
 
void info_flush ()
 
template<class T >
max (T x, T y)
 
template<class T >
min (T x, T y)
 
static void solve_c_svc (const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, double Cp, double Cn)
 
static void solve_c_svc (const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, double *C_)
 
static void solve_epsilon_svr (const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
 
static void solve_nu_svc (const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
 
static void solve_nu_svr (const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
 
static void solve_one_class (const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
 
svm_problemsvm_BuildProbFromTwoClassModel (const SvmModel233 *model, kkint32 excludeSupportVectorIDX)
 
const char * svm_check_parameter (const struct svm_problem *prob, const struct svm_parameter *param)
 
void svm_destroy_model (struct SvmModel233 *model)
 
void svm_get_labels (const struct SvmModel233 *model, kkint32 *label)
 
kkint32 svm_get_nr_class (const struct SvmModel233 *model)
 
void svm_GetSupportVectorStatistics (const struct SvmModel233 *model, kkint32 &numSVs, kkint32 &totalNumSVs)
 Extract Support Vector statistics . More...
 
struct SvmModel233Svm_Load_Model (std::istream &f, RunLog &log)
 
void svm_margin (SvmModel233 *model)
 
double svm_predict (const struct SvmModel233 *model, const struct svm_node *x)
 
double svm_predict (const struct SvmModel233 *model, const svm_node *x, std::vector< double > &dist, std::vector< kkint32 > &winners, kkint32 excludeSupportVectorIDX)
 Predicts a class for the specified training example. More...
 
double svm_predictTwoClasses (const SvmModel233 *model, const svm_node *x, double &dist, kkint32 excludeSupportVectorIDX)
 
void Svm_Save_Model (std::ostream &o, const SvmModel233 *model)
 
struct SvmModel233svm_train (const struct svm_problem *prob, const struct svm_parameter *param)
 
decision_function svm_train_one (const svm_problem *prob, const svm_parameter *param)
 
decision_function svm_train_one (const svm_problem *prob, const svm_parameter *param, double Cp, double Cn, std::set< kkint32 > &BSVIndex)
 
template<class T >
void Swap (T &x, T &y)
 
 XmlFactoryMacro (SvmModel233) typedef float Qfloat
 

Variables

const kkint32 SCALE = 100
 

Detailed Description

This is version 2.33 of "Chih-Chung Chang" and "Chih-Jen Lin" Support vector Machine; the class "ModelOldSVM" calls ths version.

This is a vesion 2.33 of "Chih-Chung Chang" and "Chih-Jen Lin" libSVM (Support Vector Machine); the class "ModelOldSVM" calls ths version.


libSVM is a Support Vector Machine implementation 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 Tong Luo and Kurt Kramer.

Typedef Documentation

typedef signed char SVM233::schar

Definition at line 522 of file svm.cpp.

Definition at line 207 of file svm.h.

Enumeration Type Documentation

anonymous enum
Enumerator
C_SVC 
NU_SVC 
ONE_CLASS 
EPSILON_SVR 
NU_SVR 

Definition at line 62 of file svm.h.

anonymous enum
Enumerator
LINEAR 
POLY 
RBF 
SIGMOID 

Definition at line 63 of file svm.h.

63 { LINEAR, POLY, RBF, SIGMOID }; /* kernel_type */
#define LINEAR
Definition: UsfCasCor.h:221
#define SIGMOID
Definition: UsfCasCor.h:219

Function Documentation

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

Definition at line 690 of file svm.cpp.

References svm_train_one().

691 {
692  dst = new T[n];
693  KKStr::MemCpy((void *)dst, (void *)src, sizeof(T)*n);
694 }
void SVM233::info ( const char *  fmt,
  ... 
)
void SVM233::info_flush ( )

Definition at line 716 of file svm.cpp.

Referenced by SVM233::Solver::Solve().

716 {}
template<class T >
T SVM233::max ( x,
y 
)
inline

Definition at line 680 of file svm.cpp.

References svm_train_one().

680 { return (x>y)?x:y; }
template<class T >
T SVM233::min ( x,
y 
)
inline

Definition at line 676 of file svm.cpp.

References svm_train_one().

676 { return (x<y)?x:y; }
static void SVM233::solve_c_svc ( const svm_problem prob,
const svm_parameter param,
double *  alpha,
Solver::SolutionInfo si,
double  Cp,
double  Cn 
)
static

Definition at line 2811 of file svm.cpp.

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

Referenced by svm_train_one().

2818 {
2819  kkint32 l = prob->l;
2820  double *minus_ones = new double[l];
2821  schar *y = new schar[l];
2822 
2823  kkint32 i;
2824 
2825  for(i=0;i<l;i++)
2826  {
2827  alpha[i] = 0;
2828  minus_ones[i] = -1;
2829  if (prob->y[i] > 0)
2830  y[i] = +1;
2831  else
2832  y[i]=-1;
2833  }
2834 
2835  Solver s;
2836  s.Solve (l,
2837  SVC_Q(*prob,*param,y),
2838  minus_ones,
2839  y,
2840  alpha,
2841  Cp,
2842  Cn,
2843  param->eps,
2844  si,
2845  param->shrinking
2846  );
2847 
2848  double sum_alpha=0;
2849  for(i=0;i<l;i++)
2850  sum_alpha += alpha[i];
2851 
2852  info("nu = %f\n", sum_alpha/(param->C*prob->l));
2853 
2854  for(i=0;i<l;i++)
2855  alpha[i] *= y[i];
2856 
2857  delete[] minus_ones;
2858  delete[] y;
2859 } /* solve_c_svc */
__int32 kkint32
Definition: KKBaseTypes.h:88
void Solve(kkint32 l, const Kernel &Q, const double *b_, const schar *y_, double *alpha_, double Cp, double Cn, double eps, SolutionInfo *si, kkint32 shrinking)
Definition: svm.cpp:2186
kkint32 l
Definition: svm.h:48
kkint32 shrinking
Definition: svm.h:91
double * y
Definition: svm.h:49
signed char schar
Definition: svm.cpp:522
void info(const char *fmt,...)
Definition: svm.cpp:715
static void SVM233::solve_c_svc ( const svm_problem prob,
const svm_parameter param,
double *  alpha,
Solver::SolutionInfo si,
double *  C_ 
)
static

Definition at line 2864 of file svm.cpp.

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

Referenced by svm_train_one().

2870 {
2871  kkint32 l = prob->l;
2872  double *minus_ones = new double[l];
2873  schar *y = new schar[l];
2874 
2875  kkint32 i;
2876 
2877  for (i = 0; i < l; i++)
2878  {
2879  alpha[i] = 0;
2880  minus_ones[i] = -1;
2881  if (prob->y[i] > 0)
2882  y[i] = +1;
2883  else
2884  y[i] = -1;
2885  }
2886 
2887  Solver s;
2888  s.Solve (l,
2889  SVC_Q (*prob, *param, y),
2890  minus_ones,
2891  y,
2892  alpha,
2893  C_,
2894  param->eps,
2895  si,
2896  param->shrinking
2897  );
2898 
2899  double sum_alpha=0;
2900  for (i = 0; i < l; i++)
2901  sum_alpha += alpha[i];
2902 
2903  info("nu = %f\n", sum_alpha/(param->C * prob->l));
2904 
2905  for (i = 0; i < l; i++)
2906  alpha[i] *= y[i];
2907 
2908  delete[] minus_ones;
2909  delete[] y;
2910 } /* solve_c_svc */
__int32 kkint32
Definition: KKBaseTypes.h:88
void Solve(kkint32 l, const Kernel &Q, const double *b_, const schar *y_, double *alpha_, double Cp, double Cn, double eps, SolutionInfo *si, kkint32 shrinking)
Definition: svm.cpp:2186
kkint32 l
Definition: svm.h:48
kkint32 shrinking
Definition: svm.h:91
double * y
Definition: svm.h:49
signed char schar
Definition: svm.cpp:522
void info(const char *fmt,...)
Definition: svm.cpp:715
static void SVM233::solve_epsilon_svr ( const svm_problem prob,
const svm_parameter param,
double *  alpha,
Solver::SolutionInfo si 
)
static

Definition at line 3008 of file svm.cpp.

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

Referenced by svm_train_one().

3013 {
3014  kkint32 l = prob->l;
3015  double *alpha2 = new double[2*l];
3016  double *linear_term = new double[2*l];
3017  schar *y = new schar[2*l];
3018  kkint32 i;
3019 
3020  for(i=0;i<l;i++)
3021  {
3022  alpha2[i] = 0;
3023  linear_term[i] = param->p - prob->y[i];
3024  y[i] = 1;
3025 
3026  alpha2[i+l] = 0;
3027  linear_term[i+l] = param->p + prob->y[i];
3028  y[i+l] = -1;
3029  }
3030 
3031  Solver s;
3032  s.Solve(2 * l,
3033  SVR_Q (*prob,*param),
3034  linear_term,
3035  y,
3036  alpha2,
3037  param->C,
3038  param->C,
3039  param->eps,
3040  si,
3041  param->shrinking
3042  );
3043 
3044  double sum_alpha = 0;
3045  for(i=0;i<l;i++)
3046  {
3047  alpha[i] = alpha2[i] - alpha2[i+l];
3048  sum_alpha += fabs(alpha[i]);
3049  }
3050  info("nu = %f\n",sum_alpha/(param->C*l));
3051 
3052  delete[] alpha2;
3053  delete[] linear_term;
3054  delete[] y;
3055 }
__int32 kkint32
Definition: KKBaseTypes.h:88
void Solve(kkint32 l, const Kernel &Q, const double *b_, const schar *y_, double *alpha_, double Cp, double Cn, double eps, SolutionInfo *si, kkint32 shrinking)
Definition: svm.cpp:2186
kkint32 l
Definition: svm.h:48
kkint32 shrinking
Definition: svm.h:91
double * y
Definition: svm.h:49
signed char schar
Definition: svm.cpp:522
void info(const char *fmt,...)
Definition: svm.cpp:715
static void SVM233::solve_nu_svc ( const svm_problem prob,
const svm_parameter param,
double *  alpha,
Solver::SolutionInfo si 
)
static

Definition at line 2914 of file svm.cpp.

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

Referenced by svm_train_one().

2919 {
2920  kkint32 i;
2921  kkint32 l = prob->l;
2922  double nu = param->nu;
2923 
2924  schar *y = new schar[l];
2925 
2926  for(i=0;i<l;i++)
2927  if(prob->y[i]>0)
2928  y[i] = +1;
2929  else
2930  y[i] = -1;
2931 
2932  double sum_pos = nu*l/2;
2933  double sum_neg = nu*l/2;
2934 
2935  for(i=0;i<l;i++)
2936  if(y[i] == +1)
2937  {
2938  alpha[i] = min(1.0,sum_pos);
2939  sum_pos -= alpha[i];
2940  }
2941  else
2942  {
2943  alpha[i] = min(1.0,sum_neg);
2944  sum_neg -= alpha[i];
2945  }
2946 
2947  double *zeros = new double[l];
2948 
2949  for(i=0;i<l;i++)
2950  zeros[i] = 0;
2951 
2952  Solver_NU s;
2953  s.Solve(l, SVC_Q(*prob,*param,y), zeros, y,
2954  alpha, 1.0, 1.0, param->eps, si, param->shrinking);
2955  double r = si->r;
2956 
2957  info("C = %f\n",1/r);
2958 
2959  for(i=0;i<l;i++)
2960  alpha[i] *= y[i]/r;
2961 
2962  si->rho /= r;
2963  si->obj /= (r*r);
2964  si->upper_bound_p = 1/r;
2965  si->upper_bound_n = 1/r;
2966 
2967  delete[] y;
2968  delete[] zeros;
2969 } /* solve_nu_svc */
#define min(X, Y)
Definition: UsfCasCor.h:244
__int32 kkint32
Definition: KKBaseTypes.h:88
void Solve(kkint32 l, const Kernel &Q, const double *b, const schar *y, double *alpha, double Cp, double Cn, double eps, SolutionInfo *si, kkint32 shrinking)
Definition: svm.cpp:2398
kkint32 l
Definition: svm.h:48
kkint32 shrinking
Definition: svm.h:91
double * y
Definition: svm.h:49
signed char schar
Definition: svm.cpp:522
void info(const char *fmt,...)
Definition: svm.cpp:715
static void SVM233::solve_nu_svr ( const svm_problem prob,
const svm_parameter param,
double *  alpha,
Solver::SolutionInfo si 
)
static

Definition at line 3059 of file svm.cpp.

References SVM233::svm_parameter::C, SVM233::svm_parameter::eps, info(), SVM233::svm_problem::l, SVM233::svm_parameter::nu, SVM233::Solver::SolutionInfo::r, SVM233::svm_parameter::shrinking, SVM233::Solver_NU::Solve(), SVM233::SVR_Q::SVR_Q(), and SVM233::svm_problem::y.

Referenced by svm_train_one().

3064 {
3065  kkint32 l = prob->l;
3066  double C = param->C;
3067  double *alpha2 = new double[2*l];
3068  double *linear_term = new double[2*l];
3069  schar *y = new schar[2*l];
3070  kkint32 i;
3071 
3072  double sum = C * param->nu * l / 2;
3073  for(i=0;i<l;i++)
3074  {
3075  alpha2[i] = alpha2[i+l] = min(sum,C);
3076  sum -= alpha2[i];
3077 
3078  linear_term[i] = - prob->y[i];
3079  y[i] = 1;
3080 
3081  linear_term[i+l] = prob->y[i];
3082  y[i+l] = -1;
3083  }
3084 
3085  Solver_NU s;
3086  s.Solve(2*l, SVR_Q(*prob,*param), linear_term, y,
3087  alpha2, C, C, param->eps, si, param->shrinking);
3088 
3089  info("epsilon = %f\n",-si->r);
3090 
3091  for (i = 0; i < l; i++)
3092  {
3093  alpha[i] = alpha2[i] - alpha2[i + l];
3094  }
3095 
3096  delete[] alpha2;
3097  delete[] linear_term;
3098  delete[] y;
3099 }
#define min(X, Y)
Definition: UsfCasCor.h:244
__int32 kkint32
Definition: KKBaseTypes.h:88
void Solve(kkint32 l, const Kernel &Q, const double *b, const schar *y, double *alpha, double Cp, double Cn, double eps, SolutionInfo *si, kkint32 shrinking)
Definition: svm.cpp:2398
kkint32 l
Definition: svm.h:48
kkint32 shrinking
Definition: svm.h:91
double * y
Definition: svm.h:49
signed char schar
Definition: svm.cpp:522
void info(const char *fmt,...)
Definition: svm.cpp:715
static void SVM233::solve_one_class ( const svm_problem prob,
const svm_parameter param,
double *  alpha,
Solver::SolutionInfo si 
)
static

Definition at line 2973 of file svm.cpp.

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

Referenced by svm_train_one().

2978 {
2979  kkint32 l = prob->l;
2980  double *zeros = new double[l];
2981  schar *ones = new schar[l];
2982  kkint32 i;
2983 
2984  kkint32 n = (kkint32)(param->nu*prob->l); // # of alpha's at upper bound
2985 
2986  for(i=0;i<n;i++)
2987  alpha[i] = 1;
2988  alpha[n] = param->nu * prob->l - n;
2989  for(i=n+1;i<l;i++)
2990  alpha[i] = 0;
2991 
2992  for(i=0;i<l;i++)
2993  {
2994  zeros[i] = 0;
2995  ones[i] = 1;
2996  }
2997 
2998  Solver s;
2999  s.Solve(l, ONE_CLASS_Q(*prob,*param), zeros, ones,
3000  alpha, 1.0, 1.0, param->eps, si, param->shrinking);
3001 
3002  delete[] zeros;
3003  delete[] ones;
3004 } /* solve_one_class */
__int32 kkint32
Definition: KKBaseTypes.h:88
void Solve(kkint32 l, const Kernel &Q, const double *b_, const schar *y_, double *alpha_, double Cp, double Cn, double eps, SolutionInfo *si, kkint32 shrinking)
Definition: svm.cpp:2186
kkint32 l
Definition: svm.h:48
kkint32 shrinking
Definition: svm.h:91
signed char schar
Definition: svm.cpp:522
svm_problem * SVM233::svm_BuildProbFromTwoClassModel ( const SvmModel233 model,
kkint32  excludeSupportVectorIDX 
)
Parameters
excludeSupportVectorIDXSpecify index of a S/V to remove from computation.

Definition at line 4356 of file svm.cpp.

References SVM233::SvmModel233::dim, EPSILON_SVR, SVM233::svm_node::index, SVM233::svm_problem::index, SVM233::svm_problem::l, SVM233::SvmModel233::l, SVM233::SvmModel233::nr_class, SVM233::SvmModel233::nSV, NU_SVR, ONE_CLASS, SVM233::SvmModel233::param, SVM233::SvmModel233::SV, SVM233::svm_problem::svm_problem(), SVM233::svm_parameter::svm_type, SVM233::svm_node::value, SVM233::svm_problem::weOwnContents, SVM233::svm_problem::x, and SVM233::svm_problem::y.

Referenced by KKMLL::SVMModel::FindWorstSupportVectors2().

4359 {
4360  if (model->param.svm_type == ONE_CLASS ||
4361  model->param.svm_type == EPSILON_SVR ||
4362  model->param.svm_type == NU_SVR
4363  )
4364  {
4365  return NULL;
4366  }
4367 
4368  kkint32 nr_class = model->nr_class;
4369  if (nr_class != 2)
4370  {
4371  printf ("\n\n\n svm_predictTwoClasses *** ERROR *** NumOf Classes != 2\n\n");
4372  return NULL;
4373  }
4374 
4375  if (model->dim > 0)
4376  {
4377  cerr << std::endl
4378  << "SVM233::svm_BuildProbFromTwoClassModel ***ERROR*** model->dim[" << model->dim << "] > 0" << std::endl
4379  << std::endl;
4380  return NULL;
4381  }
4382 
4383 
4384  kkint32 l = model->l;
4385  kkint32 numSVsFirstClass = model->nSV[0];
4386 
4387  svm_problem* newProb = new svm_problem ();
4388  newProb->weOwnContents = true;
4389  newProb->l = l;
4390  if ((excludeSupportVectorIDX >= 0) && (excludeSupportVectorIDX < l))
4391  (newProb->l)--;
4392 
4393  newProb->y = new double[newProb->l];
4394  newProb->x = new svm_node*[newProb->l];
4395  newProb->index = new kkint32[newProb->l];
4396 
4397  kkint32 newIDX = 0;
4398  for (kkint32 svIDX = 0; svIDX < l; svIDX++)
4399  {
4400  if (svIDX == excludeSupportVectorIDX)
4401  continue;
4402 
4403  if (svIDX < numSVsFirstClass)
4404  newProb->y[newIDX] = 0;
4405  else
4406  newProb->y[newIDX] = 1;
4407 
4408  newProb->index[newIDX] = newIDX;
4409  newProb->exampleNames.push_back (model->exampleNames[svIDX]);
4410 
4411  {
4412  kkint32 numElements = 0;
4413  svm_node* nextNode = model->SV[svIDX];
4414  while (nextNode->index != -1)
4415  {
4416  numElements++;
4417  nextNode++;
4418  }
4419  numElements++;
4420 
4421  newProb->x[newIDX] = new svm_node[numElements];
4422 
4423  for (kkint32 fnIDX = 0; fnIDX < numElements; fnIDX++)
4424  {
4425  newProb->x[newIDX][fnIDX].index = model->SV[svIDX][fnIDX].index;
4426  newProb->x[newIDX][fnIDX].value = model->SV[svIDX][fnIDX].value;
4427  }
4428  }
4429 
4430  newIDX++;
4431  }
4432 
4433  return newProb;
4434 } /* svm_BuildProbFromTwoClassModel */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
kkint32 dim
Definition: svm.h:161
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 svm_type
Definition: svm.h:81
kkint32 * nSV
Definition: svm.h:148
bool weOwnContents
Definition: svm.h:55
kkint32 l
Definition: svm.h:139
kkint32 l
Definition: svm.h:48
kkint32 nr_class
Definition: svm.h:138
double value
Definition: svm.h:39
kkint16 index
Definition: svm.h:38
struct svm_node ** x
Definition: svm.h:52
double * y
Definition: svm.h:49
svm_parameter param
Definition: svm.h:137
VectorKKStr exampleNames
Definition: svm.h:50
kkint32 * index
Definition: svm.h:51
VectorKKStr exampleNames
Definition: svm.h:140
svm_node ** SV
Definition: svm.h:141
const char* SVM233::svm_check_parameter ( const struct svm_problem prob,
const struct svm_parameter param 
)
void SVM233::svm_destroy_model ( struct SvmModel233 model)

Definition at line 4442 of file svm.cpp.

Referenced by KKMLL::SvmDestroyModel().

4443 {
4444  delete model;
4445  model = NULL;
4446 }
void SVM233::svm_get_labels ( const struct SvmModel233 model,
kkint32 label 
)
kkint32 SVM233::svm_get_nr_class ( const struct SvmModel233 model)
void SVM233::svm_GetSupportVectorStatistics ( const struct SvmModel233 model,
kkint32 numSVs,
kkint32 totalNumSVs 
)

Extract Support Vector statistics .

Parameters
[in]modelTraining Support Vector Machine.
[out]numSVsThe number of training examples selected as Support Vectors.
[out]totalNumSVsTotal number of SVs used by all the binary classifiers.

Definition at line 4776 of file svm.cpp.

References KKB::KKStr::Concat(), EPSILON_SVR, KKB::KKException::KKException(), SVM233::SvmModel233::l, SVM233::SvmModel233::nr_class, SVM233::SvmModel233::nSV, NU_SVR, ONE_CLASS, SVM233::SvmModel233::param, SVM233::SvmModel233::sv_coef, and SVM233::svm_parameter::svm_type.

Referenced by KKMLL::SVMModel::SupportVectorStatistics().

4780 {
4781  numSVs = 0;
4782  totalNumSVs = 0;
4783 
4784  if (model == NULL)
4785  {
4786  KKStr errMsg = "svm_GetupportVectorsStatistics ***ERROR*** (model == NULL). Model was not defined.";
4787  cerr << endl << endl << errMsg << endl << endl;
4788  throw KKException (errMsg);
4789  }
4790 
4791  if (model->param.svm_type == ONE_CLASS ||
4792  model->param.svm_type == EPSILON_SVR ||
4793  model->param.svm_type == NU_SVR
4794  )
4795  {
4796  cerr << endl << endl
4797  << "svm_GetupportVectorsStatistics ***ERROR*** This function does not support for SVM Type[" << model->param.svm_type << "]" << endl
4798  << endl;
4799  return;
4800  }
4801 
4802  numSVs = model->l;
4803 
4804  {
4805  kkint32 i;
4806  kkint32 nr_class = model->nr_class;
4807 
4808  kkint32 l = model->l;
4809 
4810 
4811  // 'start' will be built to point to the beginning of the list of S'V's for each class.
4812  kkint32* start = new kkint32[nr_class];
4813  start[0] = 0;
4814  for (i = 1; i < nr_class; i++)
4815  start[i] = start[i-1] + model->nSV[i-1];
4816 
4817 
4818  for (i = 0; i < nr_class; i++)
4819  {
4820  for (kkint32 j = i + 1; j < nr_class; j++)
4821  {
4822  double sum = 0;
4823  kkint32 si = start[i];
4824  kkint32 sj = start[j];
4825  kkint32 ci = model->nSV[i];
4826  kkint32 cj = model->nSV[j];
4827 
4828  kkint32 k;
4829  double *coef1 = model->sv_coef[j-1];
4830  double *coef2 = model->sv_coef[i];
4831 
4832  for (k = 0; k < ci; k++)
4833  if (coef1[si + k] != 0.0)
4834  totalNumSVs++;
4835 
4836  for (k = 0; k < cj; k++)
4837  if (coef2[sj + k] != 0.0)
4838  totalNumSVs++;
4839  }
4840  }
4841 
4842  delete[] start;
4843  start = NULL;
4844  }
4845 
4846  return;
4847 } /* svm_GetupportVectorsStatistic */
double ** sv_coef
Definition: svm.h:142
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 svm_type
Definition: svm.h:81
kkint32 * nSV
Definition: svm.h:148
kkint32 l
Definition: svm.h:139
kkint32 nr_class
Definition: svm.h:138
svm_parameter param
Definition: svm.h:137
struct SvmModel233* SVM233::Svm_Load_Model ( std::istream &  f,
RunLog log 
)

Referenced by KKMLL::SvmLoadModel().

void SVM233::svm_margin ( SvmModel233 model)

Definition at line 4650 of file svm.cpp.

References EPSILON_SVR, SVM233::Kernel::k_function(), SVM233::SvmModel233::margin, SVM233::SvmModel233::nr_class, SVM233::SvmModel233::nSV, NU_SVR, ONE_CLASS, SVM233::SvmModel233::param, SVM233::SvmModel233::SV, SVM233::SvmModel233::sv_coef, and SVM233::svm_parameter::svm_type.

4654 {
4655  //kkint32 numClass=model->param.numClass;
4656 
4657  assert (model);
4658 
4659  if(model->param.svm_type == ONE_CLASS ||
4660  model->param.svm_type == EPSILON_SVR ||
4661  model->param.svm_type == NU_SVR)
4662  {
4663  /*
4664  double *sv_coef = model->sv_coef[0];
4665  double sum = 0;
4666  for(kkint32 i=0;i<model->l;i++)
4667  sum += sv_coef[i] * Kernel::k_function(x,model->SV[i],model->param);
4668  sum -= model->rho[0];
4669  if(model->param.svm_type == ONE_CLASS)
4670  return (sum>0)?1:-1;
4671  else
4672  return sum;
4673  */
4674  }
4675  else
4676  {
4677  kkint32 i;
4678  kkint32 nr_class = model->nr_class;
4679 
4680  model->margin = new double[nr_class * (nr_class - 1) / 2];
4681 
4682  std::vector<kkint32> start(nr_class,0);
4683  start[0] = 0;
4684  for(i=1;i<nr_class;i++)
4685  start[i] = start[i-1]+model->nSV[i-1];
4686 
4687  kkint32 p=0;
4688  for (i = 0; i < nr_class; i++)
4689  for (kkint32 j = i + 1; j < nr_class; j++)
4690  {
4691  double sum = 0; // computer the L of the K since it is symetric
4692  double trace=0; // compute the trace
4693 
4694  kkint32 si = start[i];
4695  kkint32 sj = start[j];
4696  kkint32 ci = model->nSV[i];
4697  kkint32 cj = model->nSV[j];
4698 
4699  kkint32 k;
4700  double *coef1 = model->sv_coef[j-1];
4701  double *coef2 = model->sv_coef[i];
4702 
4703  for (k = 0; k < ci; k++)
4704  //y[k]=+1;
4705  {
4706  double kvalue = Kernel::k_function(model->SV[si+k],model->SV[si+k],model->param);
4707  trace += coef1[si+k]*coef1[si+k]*kvalue;
4708 
4709  for(kkint32 kk=k+1; kk<ci; kk++)
4710  //y[kk]=+1 and y[k]*y[kk]=+1;
4711  {
4712  kvalue = Kernel::k_function(model->SV[si+k],model->SV[si+kk],model->param);
4713  sum+= coef1[si+k]*coef1[si+kk]*kvalue;
4714  }
4715  //sum += coef1[si+k] * kvalue[si+k];
4716 
4717  { // kk: jun-06-2003
4718  for(kkint32 kk=0; kk<cj; kk++)
4719  //y[kk]=-1 and y[k]*y[kk]=-1
4720  {
4721  kvalue = Kernel::k_function(model->SV[si+k],model->SV[sj+kk],model->param);
4722 
4723  // kk 2010-06-13 I believe that since the coef already reflects the label(sign) of the training
4724  // example that the following line should be adding not subtracting.
4725  // sum -= coef1[si+k]*coef2[sj+kk]*kvalue;
4726  sum += coef1[si+k]*coef2[sj+kk]*kvalue;
4727  }
4728  } // kk: jun-06-2003
4729 
4730  }
4731 
4732  for (k=0; k<cj-1; k++)
4733  //y[k]=-1
4734  {
4735  double kvalue = Kernel::k_function(model->SV[sj+k],model->SV[sj+k],model->param);
4736  trace += coef2[sj + k] * coef2[sj + k] * kvalue;
4737 
4738  for(kkint32 kk=k+1; kk<cj; kk++)
4739  //y[kk]=-1 and y[k]*y[kk]=+1
4740  {
4741  kvalue = Kernel::k_function(model->SV[sj+k],model->SV[sj+kk],model->param);
4742 
4743  // kk 2010-06-13
4744  // I suspect this next line should be subtracting but I am not doing anything
4745  // about it at this point until I understand.
4746  sum += coef2[sj + k] * coef2[sj + kk] * kvalue;
4747  }
4748  }
4749 
4750  double marginSqr = 2 * sum + trace;
4751  if (marginSqr <= 0.0)
4752  {
4753  cerr << endl << endl
4754  << "SVM233::svm_margin ***ERROR*** the computed margin <= 0.0. This is very bad." << endl
4755  << endl;
4756  marginSqr = 0.1;
4757  }
4758 
4759 
4760 
4761  model->margin[p] = sqrt(marginSqr);
4762  p++;
4763  }
4764  }
4765 }
double ** sv_coef
Definition: svm.h:142
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 svm_type
Definition: svm.h:81
double * margin
Definition: svm.h:158
kkint32 * nSV
Definition: svm.h:148
kkint32 nr_class
Definition: svm.h:138
svm_parameter param
Definition: svm.h:137
svm_node ** SV
Definition: svm.h:141
double SVM233::svm_predict ( const struct SvmModel233 model,
const struct svm_node x 
)
double SVM233::svm_predict ( const struct SvmModel233 model,
const svm_node x,
std::vector< double > &  dist,
std::vector< kkint32 > &  winners,
kkint32  excludeSupportVectorIDX 
)

Predicts a class for the specified training example.

Parameters
[in]modelA previously trained model.
[in]xExample that we want to make prediction on.
[out]distEntry for each class-pair indicating the distance from the decision boundary.
[out]winnersA list of one or more classes that won the highest number of votes; that is for each pair of classes there is a vote and it is possible for there t be a tie for winner.
[in]excludeSupportVectorIDXIndex of training example that should be excluded from computation; if less than zero will be ignored; this would be the same index specified when training the model to ignore.
Returns
The predicted class; the won that won the most amount of votes; if there is a tie the 1st one will be returned.
double SVM233::svm_predictTwoClasses ( const SvmModel233 model,
const svm_node x,
double &  dist,
kkint32  excludeSupportVectorIDX 
)
Parameters
[in]modelA previously trained model.
[in]xExample that we want to make prediction on.
[out]distDistance from decision boundary.
[in]excludeSupportVectorIDXIndex of support vector that should be excluded form computation; if less than zero will be ignored.
Returns
The predicted class; Zero(0) or One(1); if (dist less-or-equal 0) the class Zero otherwise class One.

Definition at line 4218 of file svm.cpp.

References SVM233::svm_parameter::dimSelect, EPSILON_SVR, SVM233::SvmModel233::featureWeight, SVM233::Kernel::k_function(), SVM233::Kernel::k_function_subspace(), SVM233::SvmModel233::kValueTable, SVM233::SvmModel233::l, SVM233::SvmModel233::label, SVM233::SvmModel233::nr_class, SVM233::SvmModel233::nSV, NU_SVR, ONE_CLASS, SVM233::SvmModel233::param, SVM233::SvmModel233::rho, SVM233::SvmModel233::SV, SVM233::SvmModel233::sv_coef, and SVM233::svm_parameter::svm_type.

Referenced by KKMLL::SVMModel::DistanceFromDecisionBoundary(), KKMLL::SVMModel::FindWorstSupportVectors(), KKMLL::SVMModel::FindWorstSupportVectors2(), and KKMLL::SvmPredictRaw().

4223 {
4224  kkint32 winner = 0;
4225 
4226  kkint32 dimSelect = model->param.dimSelect;
4227 
4228  if (model->param.svm_type == ONE_CLASS ||
4229  model->param.svm_type == EPSILON_SVR ||
4230  model->param.svm_type == NU_SVR
4231  )
4232  {
4233  double* sv_coef = model->sv_coef[0];
4234  double sum = 0;
4235 
4236  for (kkint32 i=0; i < model->l; i++)
4237  {
4238  if (i == excludeSupportVectorIDX)
4239  continue;
4240 
4241  if (dimSelect > 0)
4242  sum += sv_coef[i] * Kernel::k_function_subspace (x, model->SV[i],model->param, model->featureWeight);
4243  else
4244  sum += sv_coef[i] * Kernel::k_function (x, model->SV[i], model->param);
4245  }
4246 
4247  sum -= model->rho[0];
4248 
4249  double returnVal = 0.0;
4250 
4251  if (model->param.svm_type == ONE_CLASS)
4252  returnVal = (sum > 0) ? 1:-1;
4253  else
4254  returnVal = sum;
4255 
4256  return returnVal;
4257  }
4258  else
4259  {
4260  kkint32 i;
4261 
4262  kkint32 nr_class = model->nr_class;
4263  if (nr_class != 2)
4264  {
4265  cerr << std::endl
4266  << "svm_predictTwoClasses ***ERROR*** nr_class[" << nr_class << "] != 2" << std::endl
4267  << std::endl;
4268  exit (-1);
4269  }
4270 
4271  kkint32 l = model->l;
4272  //double *kvalue = Malloc (double, l);
4273 
4274  double* kvalue = model->kValueTable;
4275 
4276  if (dimSelect > 0)
4277  {
4278  for (i = 0; i < l; i++)
4279  {
4280  if (i == excludeSupportVectorIDX)
4281  kvalue[i] = 0.0;
4282  else
4283  kvalue[i] = Kernel::k_function_subspace (x, model->SV[i], model->param, model->featureWeight);
4284  }
4285  }
4286  else
4287  {
4288  for (i = 0; i < l; i++)
4289  {
4290  if (i == excludeSupportVectorIDX)
4291  kvalue[i] = 0.0;
4292  else
4293  kvalue[i] = Kernel::k_function (x, model->SV[i], model->param);
4294  }
4295  }
4296 
4297  kkint32 start[2];
4298  start[0] = 0;
4299  for (i = 1; i < nr_class; i++)
4300  start[i] = start[i-1] + model->nSV[i-1];
4301 
4302  kkint32 p = 0;
4303 
4304  i = 0;
4305  kkint32 j = 1;
4306  {
4307  double sum = 0;
4308  kkint32 si = start[i];
4309  kkint32 sj = start[j];
4310  kkint32 ci = model->nSV[i];
4311  kkint32 cj = model->nSV[j];
4312 
4313  kkint32 k;
4314  double *coef1 = model->sv_coef[j-1];
4315  double *coef2 = model->sv_coef[i];
4316 
4317  for (k = 0; k < ci; k++)
4318  sum += coef1[si + k] * kvalue[si + k];
4319 
4320  for (k = 0; k < cj; k++)
4321  sum += coef2[sj + k] * kvalue[sj + k];
4322 
4323  sum -= model->rho[p];
4324 
4325  if (sum > 0)
4326  winner = 0;
4327  else
4328  winner = 1;
4329 
4330  dist = sum;
4331 
4332  //if (model->margin)
4333  //{
4334  // if (model->margin[0] != 0.0)
4335  // dist = dist / model->margin[0];
4336  //}
4337  }
4338 
4339  p=0;
4340 
4341  double winningLabel = (double)model->label[winner];
4342 
4343  return winningLabel;
4344  }
4345 } /* svm_predictTwoClasses */
double ** sv_coef
Definition: svm.h:142
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
kkint32 dimSelect
Definition: svm.h:102
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 svm_type
Definition: svm.h:81
kkint32 * nSV
Definition: svm.h:148
double * featureWeight
Definition: svm.h:162
kkint32 l
Definition: svm.h:139
kkint32 nr_class
Definition: svm.h:138
kkint32 * label
Definition: svm.h:147
svm_parameter param
Definition: svm.h:137
svm_node ** SV
Definition: svm.h:141
double * kValueTable
Definition: svm.h:165
double * rho
Definition: svm.h:143
void SVM233::Svm_Save_Model ( std::ostream &  o,
const SvmModel233 model 
)

Referenced by KKMLL::SvmSaveModel().

struct SvmModel233* SVM233::svm_train ( const struct svm_problem prob,
const struct svm_parameter param 
)
decision_function SVM233::svm_train_one ( const svm_problem prob,
const svm_parameter param 
)

Definition at line 3107 of file svm.cpp.

References SVM233::decision_function::alpha, C_SVC, info(), SVM233::svm_problem::l, SVM233::Solver::SolutionInfo::obj, SVM233::Solver::SolutionInfo::rho, SVM233::decision_function::rho, solve_c_svc(), SVM233::svm_parameter::svm_type, and SVM233::svm_problem::W.

3110 {
3111  double *alpha = Malloc(double,prob->l);
3113  switch(param->svm_type)
3114  {
3115  case C_SVC:
3116  solve_c_svc (prob, param, alpha,&si, prob->W);
3117  break;
3118  }
3119 
3120  info("obj = %f, rho = %f\n",si.obj,si.rho);
3121 
3122  // output SVs
3123 
3124  kkint32 nSV = 0;
3125  kkint32 nBSV = 0;
3126  for (kkint32 i = 0; i < prob->l; i++)
3127  {
3128  if (fabs (alpha[i]) > 0)
3129  {
3130  ++nSV;
3131  if (prob->y[i] > 0)
3132  {
3133  if (fabs (alpha[i]) >= prob->W[i])
3134  ++nBSV;
3135  }
3136  else
3137  {
3138  if (fabs(alpha[i]) >= prob->W[i])
3139  ++nBSV;
3140  }
3141  }
3142  }
3143 
3144  info("nSV = %d, nBSV = %d\n",nSV,nBSV);
3145 
3147  f.alpha = alpha;
3148  f.rho = si.rho;
3149  return f;
3150 } /* svm_train_one */
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 svm_type
Definition: svm.h:81
static void solve_c_svc(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, double *C_)
Definition: svm.cpp:2864
kkint32 l
Definition: svm.h:48
double * W
Definition: svm.h:53
double * y
Definition: svm.h:49
#define Malloc(type, n)
Definition: svm.cpp:699
void info(const char *fmt,...)
Definition: svm.cpp:715
decision_function SVM233::svm_train_one ( const svm_problem prob,
const svm_parameter param,
double  Cp,
double  Cn,
std::set< kkint32 > &  BSVIndex 
)

Definition at line 3154 of file svm.cpp.

References SVM233::decision_function::alpha, C_SVC, SVM233::svm_parameter::dimSelect, EPSILON_SVR, SVM233::svm_parameter::featureWeight, info(), SVM233::Kernel::k_function(), SVM233::Kernel::k_function_subspace(), SVM233::svm_problem::l, NU_SVC, NU_SVR, SVM233::Solver::SolutionInfo::obj, ONE_CLASS, SVM233::Solver::SolutionInfo::rho, SVM233::decision_function::rho, solve_c_svc(), solve_epsilon_svr(), solve_nu_svc(), solve_nu_svr(), solve_one_class(), SVM233::svm_parameter::svm_type, SVM233::svm_problem::x, and SVM233::svm_problem::y.

Referenced by clone(), max(), min(), and Swap().

3160 {
3161  double *alpha = Malloc (double, prob->l);
3163  switch(param->svm_type)
3164  {
3165  case C_SVC:
3166  solve_c_svc (prob, param, alpha, &si, Cp, Cn);
3167  break;
3168 
3169  case NU_SVC:
3170  solve_nu_svc(prob,param,alpha,&si);
3171  break;
3172 
3173  case ONE_CLASS:
3174  solve_one_class(prob,param,alpha,&si);
3175  break;
3176 
3177  case EPSILON_SVR:
3178  solve_epsilon_svr(prob,param,alpha,&si);
3179  break;
3180 
3181  case NU_SVR:
3182  solve_nu_svr(prob,param,alpha,&si);
3183  break;
3184  }
3185 
3186  info("obj = %f, rho = %f\n",si.obj,si.rho);
3187 
3188  // output SVs
3189 
3190  std::vector<kkint32> SVIndex;
3191 
3192  kkint32 nSV = 0;
3193  kkint32 nBSV = 0;
3194 
3195  for (kkint32 i = 0; i < prob->l; i++)
3196  {
3197  if (fabs(alpha[i]) > 0)
3198  {
3199  ++nSV;
3200  SVIndex.push_back (i);
3201  if (prob->y[i] > 0)
3202  {
3203  if (fabs (alpha[i]) >= si.upper_bound_p)
3204  {
3205  ++nBSV;
3206  BSVIndex.insert (prob->index[i]);
3207  }
3208  }
3209  else
3210  {
3211  if (fabs(alpha[i]) >= si.upper_bound_n)
3212  {
3213  ++nBSV;
3214  BSVIndex.insert (prob->index[i]);
3215  }
3216  }
3217  }
3218  }
3219 
3220  info("nSV = %d, nBSV = %d\n",nSV,nBSV);
3221 
3222  double sum=0.0;
3223  std::vector<kkint32>::iterator it,it2;
3224 
3225  for (it = SVIndex.begin(); it < SVIndex.end(); it++)
3226  {
3227  for (it2 = SVIndex.begin(); it2 < SVIndex.end(); it2++)
3228  {
3229  kkint32 k = *it;
3230  kkint32 kk = *it2;
3231 
3232  double kvalue;
3233 
3234  if (param->dimSelect > 0)
3235  {
3236  kvalue = Kernel::k_function_subspace (prob->x[k],
3237  prob->x[kk],
3238  *param,
3239  param->featureWeight
3240  );
3241  }
3242  else
3243  {
3244  kvalue = Kernel::k_function(prob->x[k], prob->x[kk], *param);
3245  }
3246 
3247  sum+= prob->y[k]*prob->y[kk]*alpha[k]*alpha[kk]*kvalue;
3248  }
3249  }
3250 
3251  sum /= SVIndex.size();
3252  sum = sqrt(sum);
3253 
3254  for (it = SVIndex.begin(); it < SVIndex.end(); it++)
3255  {
3256  alpha[*it] /= sum;
3257  }
3258 
3259  si.rho /= sum;
3260 
3262 
3263  f.alpha = alpha;
3264  f.rho = si.rho;
3265  return f;
3266 } /* svm_train_one */
kkint32 dimSelect
Definition: svm.h:102
__int32 kkint32
Definition: KKBaseTypes.h:88
static void solve_one_class(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
Definition: svm.cpp:2973
kkint32 svm_type
Definition: svm.h:81
double * featureWeight
Definition: svm.h:103
static void solve_c_svc(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, double *C_)
Definition: svm.cpp:2864
static void solve_nu_svr(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
Definition: svm.cpp:3059
kkint32 l
Definition: svm.h:48
struct svm_node ** x
Definition: svm.h:52
double * y
Definition: svm.h:49
static void solve_nu_svc(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
Definition: svm.cpp:2914
#define Malloc(type, n)
Definition: svm.cpp:699
kkint32 * index
Definition: svm.h:51
void info(const char *fmt,...)
Definition: svm.cpp:715
static void solve_epsilon_svr(const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si)
Definition: svm.cpp:3008
template<class T >
void SVM233::Swap ( T &  x,
T &  y 
)
inline

Definition at line 687 of file svm.cpp.

References svm_train_one().

687 { T t=x; x=y; y=t; }
SVM233::XmlFactoryMacro ( SvmModel233  )

Variable Documentation

const kkint32 SVM233::SCALE = 100

Definition at line 27 of file svm.h.