⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ginisvm.h

📁 也是一个不错的SVM实现算法。经常有人要用的
💻 H
字号:
#ifndef _GINI_SVM_BLOCK_#define _GINI_SVM_BLOCK_/*****************************************************************************/// NAME :  Gini Support Vector Machine interface// // DESCRIPTION :  Interface file to GiniSVM class.//// USAGE ://// KNOWN BUGS ://// BUG REPORT: shantanu@jhu.edu///*****************************************************************************/#include<ginidefs.h>#include<ginikernel.h>#include<stdio.h>#include<sys/time.h>// The SVM machine includes four modes , in which the state could be// Evaluation mode where the machine only makes produces decision based // on the input vector// GiniSVMBLKTRN mode is the normal mode of operation where the // machine does a batch training over given input data points// GiniSVMHUGTRN mode is used when the training data size is very large// and hence an single optimization mode is followed.// GiniSVMSEQTRN mode is the sequential training mode, where in optimization// is based on the previous support vectors and the current input.// GiniSVMTRANS mode is the transductive mode of the SVM wherein the// the machine learns from unlabeled samples based on the the previous// training examples.// GiniSVMCOMADAPT is the SVM learning mode where the machine adapts the // complexity trade off term C to achieve better generalization metric// implying the number of support vectors.a// GiniSVMKERADAPT is the SVM learning mode where the machine tries to // adapt the kernel parameters to achieve better generalization.// GiniSVMRAW is the start mode for the machine wherein the machine has not// been trained as yet. //// This machine tries to optimize memory by simulating virtual memory// wherein all the kernel values are computed before hand and they// stored in memory which is partially mapped to hard-disk. Only the// cache correspoding to the support vectors are stored and the rest// are kept on the disk. enum GiniSVMTrainingMode{   GINISVM_EVAL,   GINISVM_BLKTRN,   GINISVM_HUGTRN,   GINISVM_SEQTRN,   GINISVM_TRANS,   GINISVM_COMADAPT,   GINISVM_KERADAPT,   GINISVM_RAW};enum GiniSVMErrno{   GINISVM_NOERROR,   GINISVM_OUTOFMEM,   GINISVM_BADFORMAT};enum GINI_status{   GINI_UP_DOWN,   GINI_DOWN,   GINI_UP};struct GINI_Set{   GINI_u32 dataind;   GINI_double alpha;   GINI_double E;   GINI_u32 shrinklevel;   GINI_u32 cachehits;   GINI_bool cacheupdate;   GINI_Set  *next;   GINI_Set  *prev;};class GINI_SVMBlock{   // Holds the lagrangian values   // For a multi-class scenario this is a    // multi-dimensional array.   GINI_double** lagrange;   // Support Vectors   GINI_double** supportVectors;   // Bias which is a vector in a multi-class   // case.   GINI_double *bias;   // Number of SVs   GINI_u32 numberofSVs;   // Feature vector dimension   GINI_u32 dimension;   // Number of classes   GINI_u32 classes;   // Rate Distortion Factor   GINI_double rdist;   // Type of kernel   GINI_SVMKernel *kernel;   // Training Mode   GiniSVMTrainingMode mode;   //-----------------------------   // Training parameters   //-----------------------------   // Training Labels which are prior   // probabilities indicating our initial   // confidence values.   GINI_double **Y;   // Margin Vector Set   GINI_Set **svset;   // Suitable set candidate for computing   // the second heuristic in SMO.   GINI_Set **maxE;   GINI_Set **minE;   // Cache for storing decision functions.   GINI_Set ***svmap;   // Training data   GINI_double **traindata;   // Memory to store the set sizes for all the   // classes.   GINI_u32 *setsize;   // List iterator used by examineExample   GINI_Set *globalptr;   // Total number of data points   GINI_u32 totaldata;   // Training size for the support vector   // machine training   GINI_u32 maxtraindata;   // Complexity trade off parameter   GINI_double *C;   // Tolerance value for the values of lagrangians   GINI_double alphaeps;   // Tolerance for kkt condition   GINI_double kkteps;   // Search window size when random data points   // are selected.   GINI_u32 srchwindow;   // Percent decrease in the cost function   GINI_double costeps;   // Costfunction window   // Number of times the cost function is computed.   GINI_u32 costwindow;   // Total number of cache hits   GINI_u32 numofhits;   // Total number of cache hits   GINI_u32 maxsvsrch;   // Total number of cache hits   GINI_u32 kktiter;   // Total number of first level iterations   GINI_u32 fpass;   // Seed for using the random number generator   GINI_u32 seed;   // Floor count gives the number of times   // truncation occurs.   GINI_u32 floorcount;   // Truncation for alphas at the end of training.   // for approximate solutions.   GINI_double threshold;   // Number of training iterations   GINI_u32 iterations;   // Boolean indicator to indicate if there   // is space remaining in the cache.   GINI_bool cachefull;   // Errno for error tracking   GINI_ERROR_VAL ginierr;   // Statistics variables   // Number of times in the loop svs were deleted   // and added.   GINI_u32 numofdel;   GINI_u32 numofadd;   GINI_u32 phase1;   GINI_u32 phase2;   GINI_u32 phase3;   GINI_u32 phase4;   GINI_u32 startupsize;   GINI_double timer1;   GINI_double timer2;   GINI_double timer3;   GINI_double timer4;   GINI_double timer5;   GINI_double timer6;   GINI_double timer7;   GINI_double timer8;   //-----------------------------   // private functions   //-----------------------------   GINI_double currtimeval( struct timeval *cmp );   void printtimers();   void _purgesvlist();   // Main 4x4 optimization routine   // that uses four coefficients to   // optimize at the same time.   GINI_u32 _takestep (  GINI_u32 i1,                        GINI_u32 c1,                        GINI_u32 i2,                        GINI_u32 c2,			GINI_double *E11,			GINI_double *E12,			GINI_double *E21,			GINI_double *E22                     );   // Inner Loop that looks for the second example   // to optimize   GINI_u32 _examineExample( GINI_u32 i1 );   GINI_u32 _examinesvExample( GINI_u32 i1 );   // Get an estimate of the bias   void _biasestimate();   // Get an estimate of KKT condition   // for each data point   GINI_bool _kktcondition( GINI_u32 dataind, 		            GINI_u32 *decision,			    GINI_double *E11,			    GINI_status *dir                          );   GINI_bool _kktsvcondition( GINI_u32 dataind, 		            GINI_u32 *decision,			    GINI_double *E11,			    GINI_status *dir                          );   // Removes an element from the link list.   void _removeelement(GINI_u32 classid, GINI_Set *ptr);   // Adds an element to the link list.   GINI_Set* _addelement(  GINI_u32 dataid, 		      GINI_u32 classid,		      GINI_double ecache,		      GINI_double alpha                   );   // Evaluates the threshold function based on   // reverse water-filling procedure and the unormalized   // array of class evaluation functions.   GINI_double _evaluateThreshold( GINI_double *currfn);   GINI_status _getdirection( GINI_u32 dataid, GINI_u32 classid);   GINI_u32 _minmaxopt();   //-----------------------------   // public functions   //-----------------------------   public:   // Constructor   GINI_SVMBlock( GINI_SVMKernel *initkernel );   // Destructor   virtual ~GINI_SVMBlock();   // Initialization of SVM machine using an input   // configuration file   GINI_bool Initialize( FILE *input );   // Sequentially add training data to the    // support vector machine.   GINI_bool InsertTrainingData( GINI_double *label, GINI_double* data);   // Sequentially add training data to the    // support vector machine with a prior indicating the weightage   // of the data point.   GINI_bool InsertTrainingData( GINI_double *label,                                  GINI_double* data,                                 GINI_double prior		               );   // Saves the GiniSVM configuration in a file   GINI_bool Write( FILE *output);   // Reads in the GiniSVM configuration from a file   GINI_bool Read( FILE *output);   // Initializes training mode for the SVM machine.   GINI_bool InitTraining(                            // Number of data points                           GINI_u32 number,                                     // Number of input dimension                              GINI_u32 dimension,                           // Number of Classes                              GINI_u32 inpclass,                           // Starting complexity tradeoff term                           GINI_double *C,			   // Rate distortion factor			   GINI_double rdist,                           // Tolerance value for the lagrangians                           GINI_double inpeps,			   // Tolerance value for kkt			   GINI_double inpkkteps,			   // Search window parameter			   GINI_u32 srchwindow,			   // Cost function tolerance			   GINI_double inpcosteps,			   // Length of cost window			   GINI_u32   inpcostwindow,			   // Cache-hit threshold			   GINI_u32  hitthreshold,			   // Maximum sv search window			   GINI_u32  maxsvsrch,			   // KKT tolerance increase window			   GINI_u32  liter,			   // Number of first passes over full			   // optimization			   GINI_u32 inpfpass   );   // Start Training the machine   // precomp is a flag saying if the kernel values   // are to be pre-computed or not. Verbose flag toggles   // between printing detail optimization information.   GINI_bool StartTraining( GINI_bool precomp, 		            GINI_u32 iter,		            GINI_bool verbose		          );   // Stop training the SVM machine   GINI_u32 StopTraining();   //-----------------------------------   // Post Training evaluation Functions   //-----------------------------------     // SVM decision function based on the  input vector    virtual void Value ( GINI_double* input, GINI_double* output );   // GiniSVM decision function based on the  input vector    virtual void GiniProb ( GINI_double* input, GINI_double* output );   // Total amount of memory required to store this machine   GINI_u32 GetSize() { return numberofSVs; }   // Gets the dimension of the support vectors for this    // machine.    GINI_u32 GetDimension() { return dimension; }   // Returns threshold value for this SVM   GINI_double GetThreshold( GINI_u32 classid ) { return bias[classid]; }   // Access to the value of weights parameters   GINI_double GetAlpha( GINI_u32 index , GINI_u32 classid ) { return lagrange[index][classid]; }   // Returns the value of a support vector element    GINI_double GetSVElement( GINI_u32 i, GINI_u32 j) { return supportVectors[i][j]; }   // Returns the number of classes.   GINI_u32 GetClasses() { return classes; }   //-----------------------------------   // Performance functions   //-----------------------------------   // Total number of points this machine has been trained on   GINI_u32 GetNumberofTrainingPoints() { return totaldata;  }   //-----------------------------------   // Debug functions   //-----------------------------------   // Computes the cost function to check if    // the value actually decreases or not.   GINI_double CostFunction();   GINI_double evaluateCache( GINI_u32 dind, GINI_u32 cind );};#endif   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -