KSquare Utilities
SVM289_MFS::Kernel Class Referenceabstract
+ Inheritance diagram for SVM289_MFS::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_MFS::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)
 

Protected Attributes

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

Detailed Description

Definition at line 826 of file svm2.cpp.

Constructor & Destructor Documentation

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

Definition at line 917 of file svm2.cpp.

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

Referenced by Kernel().

921  :
922 
923  coef0 (_param.coef0),
924  degree (_param.degree),
925  gamma (_param.gamma),
926  kernel_type (_param.kernel_type),
927  l (_x.QueueSize ()),
928  numSelFeatures (0),
929  preComputed (NULL),
930  selFeatures (NULL),
931  x (NULL)
932 
933 {
934  x = new FeatureVectorList (_x, false);
935 
936  numSelFeatures = _selFeatures.NumSelFeatures ();
937  selFeatures = new kkint32[numSelFeatures];
938  for (kkint32 zed = 0; zed < numSelFeatures; zed++)
939  selFeatures[zed] = _selFeatures[zed];
940 
941  switch (kernel_type)
942  {
943  case Kernel_Type::LINEAR: kernel_function = &Kernel::kernel_linear; break;
944  case Kernel_Type::POLY: kernel_function = &Kernel::kernel_poly; break;
945  case Kernel_Type::RBF: kernel_function = &Kernel::kernel_rbf; break;
946  case Kernel_Type::SIGMOID: kernel_function = &Kernel::kernel_sigmoid; break;
947 
949  {
950  kkint32 z1 = 0;
951  kkint32 z2 = 0;
952  kernel_function = &Kernel::kernel_precomputed;
953  preComputed = new float*[l];
954  for (z1 = 0; z1 < l; z1++)
955  {
956  preComputed[z1] = new float[l];
957  for (z2 = 0; z2 < l; z2++)
958  preComputed[z1][z2] = 0.0f;
959  }
960  }
961  break;
962  }
963 
964  if (kernel_type == Kernel_Type::RBF)
965  {
966  x_square = new double[l];
967  for (kkint32 i = 0; i < l; i++)
968  x_square[i] = dot ((*x)[i], (*x)[i]);
969  }
970 
971  else
972  {
973  x_square = 0;
974  }
975 }
__int32 kkint32
Definition: KKBaseTypes.h:88
Kernel_Type kernel_type
Definition: svm2.h:132
Container class for FeatureVector derived objects.
double(Kernel::* kernel_function)(kkint32 i, kkint32 j) const
Definition: svm2.cpp:859
kkint32 NumSelFeatures() const
kkint32 QueueSize() const
Definition: KKQueue.h:313
SVM289_MFS::Kernel::~Kernel ( )
virtual

Definition at line 980 of file svm2.cpp.

981 {
982  delete[] selFeatures; selFeatures = NULL;
983  delete[] x_square; x_square = NULL;
984 
985  if (preComputed)
986  {
987  kkint32 n = x->QueueSize ();
988  kkint32 z1 = 0;
989  for (z1 = 0; z1 < l; z1++)
990  delete preComputed[z1];
991  delete preComputed;
992  preComputed = NULL;
993  }
994 
995  delete x;
996  x = NULL;
997 }
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 QueueSize() const
Definition: KKQueue.h:313

Member Function Documentation

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

Definition at line 1024 of file svm2.cpp.

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

Referenced by k_function().

1028 {
1029  kkint32 numFeatures = selFeatures.NumSelFeatures ();
1030 
1031  double sum = 0;
1032  kkint32 fn = 0;
1033  kkint32 idx = 0;
1034 
1035  const float* fvX = px.FeatureData ();
1036  const float* fvY = py.FeatureData ();
1037 
1038  for (idx = 0; idx < numFeatures; idx++)
1039  {
1040  fn = selFeatures[idx];
1041  sum += fvX[fn] * fvY[fn];
1042  }
1043 
1044  return sum;
1045 } /* 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_MFS::Kernel::get_Q ( kkint32  column,
kkint32  len 
) const
pure virtual
virtual Qfloat* SVM289_MFS::Kernel::get_QD ( ) const
pure virtual
double SVM289_MFS::Kernel::k_function ( const FeatureVector x,
const FeatureVector y,
const svm_parameter param,
const FeatureNumList selFeatures 
)
static

Definition at line 1053 of file svm2.cpp.

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

1058 {
1059  switch (param.kernel_type)
1060  {
1061  case Kernel_Type::LINEAR:
1062  return DotStatic(x, y, selFeatures);
1063 
1064  case Kernel_Type::POLY:
1065  return powi (param.gamma * DotStatic (x, y, selFeatures) + param.coef0,param.degree);
1066 
1067  case Kernel_Type::RBF:
1068  {
1069  kkint32 numSelFeatures = selFeatures.NumSelFeatures ();
1070  kkint32 fn = 0;
1071  kkint32 idx = 0;
1072  const float* fvX = x.FeatureData ();
1073  const float* fvY = y.FeatureData ();
1074 
1075  double sum = 0;
1076  for (idx = 0; idx < numSelFeatures; idx++)
1077  {
1078  fn = selFeatures[idx];
1079  double d = fvX[fn] - fvY[fn];
1080  sum += d * d;
1081  }
1082 
1083  return exp (-param.gamma * sum);
1084  }
1085 
1086  case Kernel_Type::SIGMOID:
1087  return tanh(param.gamma * DotStatic (x, y, selFeatures) + param.coef0);
1088 
1089  case Kernel_Type::PRECOMPUTED: //x: test (validation), y: SV
1090  {
1091  cerr << endl
1092  << "SVM289_MFS::Kernel::k_function ***ERROR*** does not support 'PRECOMPUTED'." << endl
1093  << endl;
1094  return 0;
1095  }
1096 
1097  default:
1098  return 0; // Unreachable
1099  }
1100 } /* k_function */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
Kernel_Type kernel_type
Definition: svm2.h:132
double powi(double base, kkint32 times)
Definition: svm2.cpp:94
void FeatureData(kkint32 _featureNum, float _featureValue)
Assign a value to a specific feature number for the feature vector.
static double DotStatic(const FeatureVector &px, const FeatureVector &py, const FeatureNumList &selFeatures)
Definition: svm2.cpp:1024
kkint32 NumSelFeatures() const
virtual void SVM289_MFS::Kernel::swap_index ( kkint32  i,
kkint32  j 
)
inlinevirtual

Implements SVM289_MFS::QMatrix.

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

Definition at line 850 of file svm2.cpp.

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

851  {
852  //swap (x[i], x[j]);
853  x->SwapIndexes (i, j);
854  if (x_square)
855  swap (x_square[i], x_square[j]);
856  }
void swap(T &x, T &y)
Definition: svm2.h:283
void SwapIndexes(size_t idx1, size_t idx2)
Definition: KKQueue.h:769

Member Data Documentation

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

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