KSquare Utilities
SVM289_BFS::Kernel Class Referenceabstract
+ Inheritance diagram for SVM289_BFS::Kernel:

Public Member Functions

 Kernel (const FeatureVectorList &_x, const FeatureNumList &_selFeatures, const svm_parameter &_param, RunLog &_log)
 
virtual ~Kernel ()
 
virtual Qfloatget_Q (kkint32 column, kkint32 len) const =0
 
virtual Qfloatget_QD () const =0
 
virtual void swap_index (kkint32 i, kkint32 j)
 
- Public Member Functions inherited from SVM289_BFS::QMatrix
virtual ~QMatrix ()
 

Static Public Member Functions

static double DotStatic (const FeatureVector &px, const FeatureVector &py, const FeatureNumList &selFeatures)
 
static double k_function (const FeatureVector &x, const FeatureVector &y, const svm_parameter &param, const FeatureNumList &selFeatures)
 Kernel evaluation. More...
 

Protected Attributes

double(Kernel::* kernel_function )(kkint32 i, kkint32 j) const
 

Detailed Description

Definition at line 848 of file svm289_BFS.cpp.

Constructor & Destructor Documentation

SVM289_BFS::Kernel::Kernel ( const FeatureVectorList _x,
const FeatureNumList _selFeatures,
const svm_parameter _param,
RunLog _log 
)

Definition at line 945 of file svm289_BFS.cpp.

References SVM289_BFS::svm_parameter::coef0, SVM289_BFS::svm_parameter::degree, KKMLL::FeatureVectorList::FeatureVectorList(), SVM289_BFS::svm_parameter::gamma, Kernel(), kernel_function, SVM289_BFS::svm_parameter::kernel_type, SVM289_BFS::LINEAR, KKMLL::FeatureNumList::NumSelFeatures(), KKMLL::FeatureNumList::operator[](), SVM289_BFS::POLY, SVM289_BFS::PRECOMPUTED, SVM289_BFS::RBF, and SVM289_BFS::SIGMOID.

Referenced by Kernel().

949  :
950 
951  coef0 (_param.coef0),
952  degree (_param.degree),
953  gamma (_param.gamma),
954  kernel_type (_param.kernel_type),
955  l (_x.QueueSize ()),
956  numSelFeatures (0),
957  preComputed (NULL),
958  selFeatures (NULL),
959  x (NULL)
960 
961 {
962  x = new FeatureVectorList (_x, false);
963 
964  numSelFeatures = _selFeatures.NumSelFeatures ();
965  selFeatures = new kkint32[numSelFeatures];
966  for (kkint32 zed = 0; zed < numSelFeatures; zed++)
967  selFeatures[zed] = _selFeatures[zed];
968 
969  switch (kernel_type)
970  {
971  case LINEAR:
972  kernel_function = &Kernel::kernel_linear;
973  break;
974 
975  case POLY:
976  kernel_function = &Kernel::kernel_poly;
977  break;
978 
979  case RBF:
980  kernel_function = &Kernel::kernel_rbf;
981  break;
982 
983  case SIGMOID:
984  kernel_function = &Kernel::kernel_sigmoid;
985  break;
986 
987  case PRECOMPUTED:
988  {
989  kkint32 z1 = 0;
990  kkint32 z2 = 0;
991  kernel_function = &Kernel::kernel_precomputed;
992  preComputed = new float*[l];
993  for (z1 = 0; z1 < l; z1++)
994  {
995  preComputed[z1] = new float[l];
996  for (z2 = 0; z2 < l; z2++)
997  preComputed[z1][z2] = 0.0f;
998  }
999  }
1000  break;
1001  }
1002 
1003  if (kernel_type == RBF)
1004  {
1005  x_square = new double[l];
1006  for (kkint32 i = 0; i < l; i++)
1007  x_square[i] = dot ((*x)[i], (*x)[i]);
1008  }
1009 
1010  else
1011  {
1012  x_square = 0;
1013  }
1014 }
__int32 kkint32
Definition: KKBaseTypes.h:88
Container class for FeatureVector derived objects.
double(Kernel::* kernel_function)(kkint32 i, kkint32 j) const
Definition: svm289_BFS.cpp:887
kkint32 NumSelFeatures() const
kkint32 QueueSize() const
Definition: KKQueue.h:313
SVM289_BFS::Kernel::~Kernel ( )
virtual

Definition at line 1019 of file svm289_BFS.cpp.

1020 {
1021  delete[] selFeatures; selFeatures = NULL;
1022  delete[] x_square; x_square = NULL;
1023 
1024  if (preComputed)
1025  {
1026  kkint32 n = x->QueueSize ();
1027  kkint32 z1 = 0;
1028  for (z1 = 0; z1 < l; z1++)
1029  delete preComputed[z1];
1030  delete preComputed;
1031  preComputed = NULL;
1032  }
1033 
1034  delete x;
1035  x = NULL;
1036 }
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 QueueSize() const
Definition: KKQueue.h:313

Member Function Documentation

double SVM289_BFS::Kernel::DotStatic ( const FeatureVector px,
const FeatureVector py,
const FeatureNumList selFeatures 
)
static

Definition at line 1063 of file svm289_BFS.cpp.

References KKMLL::FeatureVector::FeatureData(), KKMLL::FeatureNumList::NumSelFeatures(), and KKMLL::FeatureNumList::operator[]().

Referenced by k_function().

1067 {
1068  kkint32 numFeatures = selFeatures.NumSelFeatures ();
1069 
1070  double sum = 0;
1071  kkint32 fn = 0;
1072  kkint32 idx = 0;
1073 
1074  const float* fvX = px.FeatureData ();
1075  const float* fvY = py.FeatureData ();
1076 
1077  for (idx = 0; idx < numFeatures; idx++)
1078  {
1079  fn = selFeatures[idx];
1080  sum += fvX[fn] * fvY[fn];
1081  }
1082 
1083  return sum;
1084 } /* dot */
__int32 kkint32
Definition: KKBaseTypes.h:88
void FeatureData(kkint32 _featureNum, float _featureValue)
Assign a value to a specific feature number for the feature vector.
kkint32 NumSelFeatures() const
virtual Qfloat* SVM289_BFS::Kernel::get_Q ( kkint32  column,
kkint32  len 
) const
pure virtual
virtual Qfloat* SVM289_BFS::Kernel::get_QD ( ) const
pure virtual
double SVM289_BFS::Kernel::k_function ( const FeatureVector x,
const FeatureVector y,
const svm_parameter param,
const FeatureNumList selFeatures 
)
static

Kernel evaluation.

The static method k_function is for doing single kernel evaluation the constructor of Kernel prepares to calculate the l*l kernel matrix the member function get_Q is for getting one column from the Q Matrix

Definition at line 1092 of file svm289_BFS.cpp.

References SVM289_BFS::svm_parameter::coef0, SVM289_BFS::svm_parameter::degree, DotStatic(), KKMLL::FeatureVector::FeatureData(), SVM289_BFS::svm_parameter::gamma, SVM289_BFS::svm_parameter::kernel_type, SVM289_BFS::LINEAR, KKMLL::FeatureNumList::NumSelFeatures(), KKMLL::FeatureNumList::operator[](), SVM289_BFS::POLY, SVM289_BFS::powi(), SVM289_BFS::PRECOMPUTED, SVM289_BFS::RBF, and SVM289_BFS::SIGMOID.

1097 {
1098  switch (param.kernel_type)
1099  {
1100  case LINEAR:
1101  return DotStatic(x, y, selFeatures);
1102 
1103  case POLY:
1104  return powi (param.gamma * DotStatic (x, y, selFeatures) + param.coef0,param.degree);
1105 
1106  case RBF:
1107  {
1108  kkint32 numSelFeatures = selFeatures.NumSelFeatures ();
1109  kkint32 fn = 0;
1110  kkint32 idx = 0;
1111  const float* fvX = x.FeatureData ();
1112  const float* fvY = y.FeatureData ();
1113 
1114  double sum = 0;
1115  for (idx = 0; idx < numSelFeatures; idx++)
1116  {
1117  fn = selFeatures[idx];
1118  double d = fvX[fn] - fvY[fn];
1119  sum += d * d;
1120  }
1121 
1122  return exp (-param.gamma * sum);
1123  }
1124 
1125  case SIGMOID:
1126  return tanh(param.gamma * DotStatic (x, y, selFeatures) + param.coef0);
1127 
1128  case PRECOMPUTED: //x: test (validation), y: SV
1129  {
1130  cerr << endl
1131  << "SVM289_BFS::Kernel::k_function ***ERROR*** does not support 'PRECOMPUTED'." << endl
1132  << endl;
1133  return 0;
1134  }
1135 
1136  default:
1137  return 0; // Unreachable
1138  }
1139 } /* k_function */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
static double DotStatic(const FeatureVector &px, const FeatureVector &py, const FeatureNumList &selFeatures)
double powi(double base, kkint32 times)
Definition: svm289_BFS.cpp:93
void FeatureData(kkint32 _featureNum, float _featureValue)
Assign a value to a specific feature number for the feature vector.
kkint32 NumSelFeatures() const
virtual void SVM289_BFS::Kernel::swap_index ( kkint32  i,
kkint32  j 
)
inlinevirtual

Implements SVM289_BFS::QMatrix.

Reimplemented in SVM289_BFS::SVR_Q, SVM289_BFS::ONE_CLASS_Q, and SVM289_BFS::SVC_Q.

Definition at line 878 of file svm289_BFS.cpp.

Referenced by SVM289_BFS::SVC_Q::swap_index(), and SVM289_BFS::ONE_CLASS_Q::swap_index().

879  {
880  //swap (x[i], x[j]);
881  x->SwapIndexes (i, j);
882  if (x_square)
883  swap (x_square[i], x_square[j]);
884  }
void swap(T &x, T &y)
Definition: svm289_BFS.h:265
void SwapIndexes(size_t idx1, size_t idx2)
Definition: KKQueue.h:769

Member Data Documentation

double(Kernel::* SVM289_BFS::Kernel::kernel_function) (kkint32 i, kkint32 j) const
protected

The documentation for this class was generated from the following file: