// file: $isip/class/pr/SupportVectorMachine/SupportVectorMachine.h // // make sure definitions are only made once // #ifndef ISIP_SUPPORT_VECTOR_MACHINE #define ISIP_SUPPORT_VECTOR_MACHINE // isip include files // #ifndef ISIP_SDB #include #endif #ifndef ISIP_KERNEL #include #endif #ifndef ISIP_STRING #include #endif #ifndef ISIP_VECTOR #include #endif #ifndef ISIP_VECTOR_FLOAT #include #endif #ifndef ISIP_AUDIO_DATABASE #include #endif #ifndef ISIP_FEATURE_FILE #include #endif #ifndef ISIP_FILENAME #include #endif #ifndef ISIP_FLOAT #include #endif #ifndef ISIP_STATISTICAL_MODEL #include #endif #ifndef ISIP_SUPPORT_VECTOR_MODEL #include #endif #ifndef ISIP_KERNEL #include #endif #ifndef ISIP_MEMORY_MANAGER #include #endif // SupportVectorMachine: a class to compute the support vectors in the // data set using the Sequential Minimization Optimization (SMO) algorithm. // // References: // // [1] V. N. Vapnik, "The Nature of Statistical Learning Theory", // Springer-Verlag, New York, 1995. // // [2] J. C. Platt, "Fast Training of Support Vector Machines using // Sequential Minimal Optimization". In B. Scholkopf, C. J. C. // Burges and A. J. Smola, editors, "Advances in Kernel Methods // - Support Vector Learning", pp 185-208, MIT Press, 1998. // // [3] N. Cristianini, J. Shawe-Taylor, "An Introduction to Support Vector // Machines", Cambridge Press, 2000. // // [4] C. J. C. Burges, "A Tutorial on Support Vector Machines for // Pattern Recognition". Available on-line at (Microsoft Research): // http://www.research.microsoft.com/ // class SupportVectorMachine { //--------------------------------------------------------------------------- // // public constants // //--------------------------------------------------------------------------- public: // define the class name // static const String CLASS_NAME; //--------------------------------------------------------------------------- // // other important constants // //--------------------------------------------------------------------------- // define the algorithm choices // enum ALGORITHM { SEQUENTIAL_MINIMAL_OPTIMIZATION = 0, DEF_ALGORITHM = SEQUENTIAL_MINIMAL_OPTIMIZATION }; // define the implementation choices // enum IMPLEMENTATION { LINEAR = 0, POLYNOMIAL, RBF, SIGMOID, DEF_IMPLEMENTATION = LINEAR }; // define output type choices // enum TYPE { TEXT = 0, BINARY, DEF_TYPE = BINARY }; // define the static NameMap objects // static const NameMap IMPL_MAP; static const NameMap ALGO_MAP; static const NameMap TYPE_MAP; //---------------------------------------- // // i/o related constants // //---------------------------------------- static const String DEF_PARAM; static const String DEF_COMMENT_TAG; static const String PARAM_OUTPUT_TYPE; static const String PARAM_OUTPUT_FILE; static const String PARAM_AUDIO_DB; static const String PARAM_PENALTY; static const String PARAM_EPSILON; static const String PARAM_TOLERANCE; static const String PARAM_POLYNOMIAL_DEGREE; static const String PARAM_RBF_GAMMA; static const String PARAM_SIGMOID_KAPPA; static const String PARAM_SIGMOID_DELTA; static const String PARAM_ALGORITHM; static const String PARAM_IMPLEMENTATION; //---------------------------------------- // // default values and arguments // //---------------------------------------- static const int32 DEF_MAX_POINTS = 0; static const int32 DEF_CAPACITY = 1000; static const float32 DEF_BIAS = 0.0; static const float32 DEF_PENALTY = 0.05; static const float32 DEF_EPSILON = 0.001; static const float32 DEF_TOLERANCE = 0.001; static const float32 DEF_POLYNOMIAL_DEGREE = 3.0; static const float32 DEF_RBF_GAMMA = 0.5; static const float32 DEF_SIGMOID_KAPPA = 1.0; static const float32 DEF_SIGMOID_DELTA = 1.0; //--------------------------------------------------------------------------- // // protected data // //--------------------------------------------------------------------------- protected: // algorithm name // ALGORITHM algorithm_d; // implementation name // IMPLEMENTATION implementation_d; // output type // TYPE output_type_d; // output file // Filename output_file_d; // audio database file // Filename audio_db_file_d; // audio database // AudioDatabase audio_db_d; // kernel function: we assume that the same kernel is used for // all training points. this only makes sense though since anything else // would mean different training points operating in different spaces. // Kernel kernel_d; // default polynomial degree // Float polynomial_degree_d; // default rbf gamma // Float rbf_gamma_d; // default sigmoid kappa // Float sigmoid_kappa_d; // default sigmoid delta // Float sigmoid_delta_d; // lagrange multipliers // VectorFloat alpha_d; // maximum penalty for an error // Float penalty_d; // default tolearnce parameter // Float tol_d; // default epsilon parameter // Float eps_d; // maximum number of points // Long max_points_d; // default bias parameter // Float bias_d; // error cache parameter // VectorFloat error_cache_d; // input observations and their labels // Vector points_d; VectorFloat target_d; // verbosity // static Integral::DEBUG verbosity_d; // a static debug level // static Integral::DEBUG debug_level_d; // a static memory manager // static MemoryManager mgr_d; //--------------------------------------------------------------------------- // // required public methods // //--------------------------------------------------------------------------- public: // method: name // static const String& name() { return CLASS_NAME; } // other static methods // static bool8 diagnose(Integral::DEBUG debug_level); // method: setDebug // static bool8 setDebug(Integral::DEBUG level) { debug_level_d = level; return true; } // method: debug // bool8 debug(const unichar* msg) const { return true; } // method: destructor // ~SupportVectorMachine() {} // method: default constructor // SupportVectorMachine(); // method: copy constructor // SupportVectorMachine(const SupportVectorMachine& arg) { assign(arg); } // method: assign // bool8 assign(const SupportVectorMachine& copy_node) { return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__); } // method: operator= // SupportVectorMachine& operator= (const SupportVectorMachine& arg) { assign(arg); return *this; } // i/o methods // int32 sofSize() const; bool8 read(Sof& sof, int32 tag, const String& name = CLASS_NAME); bool8 write(Sof& sof, int32 tag, const String& name = CLASS_NAME) const; bool8 readData(Sof& sof, const String& pname = DEF_PARAM, int32 size = SofParser::FULL_OBJECT, bool8 param = true, bool8 nested = false); bool8 writeData(Sof& sof, const String& pname = DEF_PARAM) const; // method: eq // bool8 eq(const SupportVectorMachine& arg) const { return Error::handle(name(), L"eq", Error::ARG, __FILE__, __LINE__); } // method: new // static void* operator new(size_t size) { return mgr_d.get(); } // method: new[] // static void* operator new[](size_t size) { return mgr_d.getBlock(size); } // method: delete // static void operator delete(void* ptr) { mgr_d.release(ptr); } // method: delete[] // static void operator delete[](void* ptr) { mgr_d.releaseBlock(ptr); } // method: setGrowSize // static bool8 setGrowSize(int32 grow_size) { return mgr_d.setGrow(grow_size); } // method: clear // bool8 clear(Integral::CMODE cmode = Integral::DEF_CMODE) { return true; } //--------------------------------------------------------------------------- // // class-specific public methods // //--------------------------------------------------------------------------- // method: getAlgorithm // ALGORITHM getAlgorithm() { return algorithm_d; } // method: setAlgorithm // bool8 setAlgorithm(ALGORITHM arg) { return (algorithm_d = arg); } // method: getImplementation // IMPLEMENTATION getImplementation() { return implementation_d; } // method: setImplementation // bool8 setImplementation(IMPLEMENTATION arg) { return (implementation_d = arg); } // method: setPenalty // bool8 setPenalty(float32 arg) { if (arg != DEF_PENALTY) { penalty_d = arg; } return true; } // method: setEpsilon // bool8 setEpsilon(float32 arg) { if (arg != DEF_EPSILON) { eps_d = arg; } return true; } // method: setTolerance // bool8 setTolerance (float32 arg) { if (arg != DEF_TOLERANCE) { tol_d = arg; } return true; } // method: setDegree // bool8 setDegree(float32 arg) { if (arg != DEF_POLYNOMIAL_DEGREE) { polynomial_degree_d = arg; } return true; } // method: setKappa // bool8 setKappa(float32 arg) { if (arg != DEF_SIGMOID_KAPPA) { sigmoid_kappa_d = arg; } return true; } // method: setDelta // bool8 setDelta(float32 arg) { if (arg != DEF_SIGMOID_DELTA) { sigmoid_delta_d = arg; } return true; } // method: setGamma // bool8 setGamma(float32 arg) { if (arg != DEF_RBF_GAMMA) { rbf_gamma_d = arg; } return true; } // method: setVerbosity // bool8 setVerbosity(Integral::DEBUG verbosity) { verbosity_d = verbosity; return true; } // method: setOutputFile // bool8 setOutputFile(Filename& arg) { return output_file_d.assign(arg); } // method: setOutputType // bool8 setOutputType(TYPE arg) { output_type_d = arg; return true; } // this method initializes the kernel parameters // bool8 init(); // this method load the feature vectors in RAW format // bool8 loadRawFeatures(Filename& arg); // this method load the feature vectors used in the optimization process // bool8 loadFeatures(Sdb& in_sdb, Sdb& out_sdb); // this method loads the training data and determines the support vectors // currently using the Sequential Minimal Optimization algorithm // bool8 train(); // this method writes the support vector model to file // bool8 writeModel(); //--------------------------------------------------------------------------- // // private methods // //--------------------------------------------------------------------------- private: // this is the main routine of the Sequential Minimal Optimization algorithm // bool8 sequentialMinimalOptimization(); // method determines the SVM output for the training example // float32 evaluateOutput(VectorFloat& point); // method that jointly optimizes two Lagrange multipliers and // maintains the feasibility of the dual QP // int takeStep(int i1, int i2); // method to examine a specific Lagrange multiplier and check if // it violates the KKT condition // int examineExample(int i2); }; // end of include file // #endif