Shark Machine Learning Library
  • About Shark
  • Sourceforge
    • Project Summary
    • Downloads
    • Subversion Repository
  • Getting Started
  • Tutorials
  • FAQ
  • Main Modules
    • ReClaM
    • EALib
    • MOO-EALib
    • Fuzzy
  • Tools
    • Mixture
    • Array
    • Rng
    • LinAlg
    • FileUtil
  • Main Page
  • Related Pages
  • Classes

Svm.h

Go to the documentation of this file.
00001 //===========================================================================
00036 //===========================================================================
00037 
00038 
00039 #ifndef _Svm_H_
00040 #define _Svm_H_
00041 
00042 
00043 #include <SharkDefs.h>
00044 #include <ReClaM/Model.h>
00045 #include <ReClaM/Optimizer.h>
00046 #include <ReClaM/KernelFunction.h>
00047 #include <ReClaM/QuadraticProgram.h>
00048 #include <Rng/GlobalRng.h>
00049 
00050 class SvmApproximation;
00051 
00052 
00088 class SVM : public Model
00089 {
00090 public:
00095     SVM(KernelFunction* pKernel, bool bSignOutput = false);
00096 
00102     SVM(KernelFunction* pKernel, const Array<double>& input, bool bSignOutput = false);
00103 
00105     ~SVM();
00106 
00127     void SetTrainingData(const Array<double>& input, bool copy = false);
00128 
00133     void model(const Array<double>& input, Array<double>& output);
00134 
00139     double model(const Array<double>& input);
00140 
00155     void modelDerivative(const Array<double>& input, Array<double>& derivative);
00156 
00171     void modelDerivative(const Array<double>& input, Array<double>& output, Array<double>& derivative);
00172 
00180     inline double getAlpha(int index)
00181     {
00182         return parameter(index);
00183     }
00184 
00192     inline double getOffset()
00193     {
00194         return parameter(examples);
00195     }
00196 
00198     inline KernelFunction* getKernel()
00199     {
00200         return kernel;
00201     }
00202 
00204     inline const Array<double>& getPoints()
00205     {
00206         return *x;
00207     }
00208 
00210     inline unsigned int getExamples()
00211     {
00212         return examples;
00213     }
00214 
00216     inline unsigned int getDimension()
00217     {
00218         return inputDimension;
00219     }
00220 
00228     void MakeSparse();
00229 
00238     bool LoadSVMModel(std::istream& is);
00239 
00248     bool SaveSVMModel(std::ostream& os);
00249 
00259     static SVM* ImportLibsvmModel(std::istream& is);
00260 
00270     static SVM* ImportSvmlightModel(std::istream& is);
00271 
00272     friend class SvmApproximation;
00273 
00274 protected:
00295     static int ReadToken(std::istream& is, char* buffer, int maxlength, const char* separators);
00296 
00312     static int DiscardUntil(std::istream& is, const char* separators);
00313 
00315     KernelFunction* kernel;
00316 
00318     bool bOwnMemory;
00319 
00321     const Array<double>* x;
00322 
00325     bool signOutput;
00326 
00328     unsigned int examples;
00329 };
00330 
00331 
00374 class MultiClassSVM : public Model
00375 {
00376 public:
00382     MultiClassSVM(KernelFunction* pKernel, unsigned int numberOfClasses, bool bOrthogonalVectors, bool bNumberOutput = true);
00383 
00388     MultiClassSVM(KernelFunction* pKernel, Array<double> prototypes, bool bNumberOutput = true);
00389 
00391     ~MultiClassSVM();
00392 
00393 
00403     void SetTrainingData(const Array<double>& input, bool copy = false);
00404 
00406     void model(const Array<double>& input, Array<double>& output);
00407 
00409     unsigned int model(const Array<double>& input);
00410 
00412     void Normalize();
00413 
00415     inline KernelFunction* getKernel()
00416     {
00417         return kernel;
00418     }
00419 
00421     inline const Array<double>& getPoints()
00422     {
00423         return *x;
00424     }
00425 
00429     inline double getAlpha(unsigned int index, unsigned int c) const
00430     {
00431         return parameter(classes * index + c);
00432     }
00433 
00436     inline double getOffset(unsigned int c) const
00437     {
00438         return parameter(classes * examples + c);
00439     }
00440 
00442     inline unsigned int getClasses() const
00443     {
00444         return classes;
00445     }
00446 
00449     inline const ArrayReference<double> getClassPrototype(unsigned int c) const
00450     {
00451         return prototypes[c];
00452     }
00453 
00455     unsigned int VectorToClass(const Array<double>& v);
00456 
00457 protected:
00458     void Predict(const Array<double>& input, Array<double>& output);
00459     void Predict(const Array<double>& input, ArrayReference<double> output);
00460 
00462     KernelFunction* kernel;
00463 
00465     bool bOwnMemory;
00466 
00468     const Array<double>* x;
00469 
00472     bool numberOutput;
00473 
00475     unsigned int examples;
00476 
00478     unsigned int classes;
00479 
00481     Array<double> prototypes;
00482 };
00483 
00484 
00497 class MetaSVM : public Model
00498 {
00499 public:
00504     MetaSVM(SVM* pSVM, unsigned int numberOfHyperParameters);
00505 
00510     MetaSVM(MultiClassSVM* pSVM, unsigned int numberOfHyperParameters);
00511 
00513     ~MetaSVM();
00514 
00515 
00517     inline SVM* getSVM()
00518     {
00519         return dynamic_cast<SVM*>(svm);
00520     }
00521 
00523     inline MultiClassSVM* getMultiClassSVM()
00524     {
00525         return dynamic_cast<MultiClassSVM*>(svm);
00526     }
00527 
00529     void model(const Array<double>& input, Array<double>& output);
00530 
00532     void setParameter(unsigned int index, double value);
00533 
00535     bool isFeasible();
00536 
00537 protected:
00539     Model* svm;
00540 
00542     KernelFunction* kernel;
00543 
00545     unsigned int hyperparameters;
00546 };
00547 
00548 
00600 class C_SVM : public MetaSVM
00601 {
00602 public:
00610     C_SVM(SVM* pSVM, double Cplus, double Cminus, bool norm2 = false, bool unconst = false);
00611 
00613     ~C_SVM();
00614 
00615 
00621     void PrepareDerivative();
00622 
00627     void modelDerivative(const Array<double>& input, Array<double>& derivative);
00628 
00630     void setParameter(unsigned int index, double value);
00631 
00633     inline double get_Cplus()
00634     {
00635         return C_plus;
00636     }
00637 
00639     inline double get_Cminus()
00640     {
00641         return C_minus;
00642     }
00643 
00645     inline bool is2norm()
00646     {
00647         return norm2penalty;
00648     }
00649 
00651     inline bool isUnconstrained()
00652     {
00653         return exponential;
00654     }
00655 
00657     inline double getCRatio()
00658     {
00659         return C_ratio;
00660     }
00661 
00663     bool isFeasible();
00664 
00665 protected:
00667     bool norm2penalty;
00668 
00670     double C_plus;
00671 
00673     double C_minus;
00674 
00676     double C_ratio;
00677 
00679     bool exponential;
00680 
00681     Array<double> alpha_b_Derivative;
00682 };
00683 
00684 
00713 class Epsilon_SVM : public MetaSVM
00714 {
00715 public:
00723     Epsilon_SVM(SVM* pSVM, double C, double epsilon, bool unconst = false);
00724 
00726     ~Epsilon_SVM();
00727 
00728 
00730     void setParameter(unsigned int index, double value);
00731 
00733     inline double get_C()
00734     {
00735         return C;
00736     }
00737 
00739     inline double get_epsilon()
00740     {
00741         return epsilon;
00742     }
00743 
00745     bool isFeasible();
00746 
00747 protected:
00749     double C;
00750 
00752     double epsilon;
00753 
00755     bool exponential;
00756 };
00757 
00758 
00759 
00771 class OneClassSVM : public MetaSVM
00772 {
00773 public:
00774 
00777     OneClassSVM(SVM* pSVM, double fractionNu);
00778 
00780     ~OneClassSVM();
00781 
00783     void setParameter(unsigned int index, double value);
00784 
00786     inline double getNu()
00787     {
00788         return nu;
00789     }
00790 
00792     bool isFeasible();
00793 
00794 protected:
00796     double nu;
00797 };
00798 
00799 
00812 class RegularizationNetwork : public MetaSVM
00813 {
00814 public:
00816     RegularizationNetwork(SVM* pSVM, double gamma);
00817 
00819     ~RegularizationNetwork();
00820 
00821 
00822     inline double get_gamma() { return parameter(0); }
00823     inline void set_gamma(double gamma) { setParameter(0, gamma); }
00824 
00826     bool isFeasible();
00827 };
00828 
00829 
00840 class AllInOneMcSVM : public MetaSVM
00841 {
00842 public:
00844     AllInOneMcSVM(MultiClassSVM* pSVM, double C);
00845 
00847     ~AllInOneMcSVM();
00848 
00849 
00850     inline double get_C() { return parameter(0); }
00851     inline void set_C(double C) { setParameter(0, C); }
00852 
00854     bool isFeasible();
00855 };
00856 
00857 
00868 class CrammerSingerMcSVM : public MetaSVM
00869 {
00870 public:
00876     CrammerSingerMcSVM(MultiClassSVM* pSVM, double beta);
00877 
00879     ~CrammerSingerMcSVM();
00880 
00881 
00882     inline double get_beta() { return parameter(0); }
00883     inline void set_beta(double beta) { setParameter(0, beta); }
00884 
00886     bool isFeasible();
00887 };
00888 
00889 
00900 class OVAMcSVM : public MetaSVM
00901 {
00902 public:
00904     OVAMcSVM(MultiClassSVM* pSVM, double C);
00905 
00907     ~OVAMcSVM();
00908 
00909 
00910     inline double get_C() { return parameter(0); }
00911     inline void set_C(double C) { setParameter(0, C); }
00912 
00914     bool isFeasible();
00915 };
00916 
00917 
00932 class OCCMcSVM : public MetaSVM
00933 {
00934 public:
00936     OCCMcSVM(MultiClassSVM* pSVM, double C);
00937 
00939     ~OCCMcSVM();
00940 
00941 
00942     inline double get_C() { return parameter(0); }
00943     inline void set_C(double C) { setParameter(0, C); }
00944 
00946     bool isFeasible();
00947 };
00948 
00949 
00973 class SVM_Optimizer : public Optimizer
00974 {
00975 public:
00977     SVM_Optimizer();
00978 
00980     ~SVM_Optimizer();
00981 
00982 
00991     void init(Model& model);
00992 
01013     double optimize(Model& model, ErrorFunction& error, const Array<double>& input, const Array<double>& target);
01014 
01029     double optimize(SVM& model, const Array<double>& input, const Array<double>& target, bool copy = false);
01030 
01045     void optimize(MultiClassSVM& model, const Array<double>& input, const Array<double>& target, bool copy = false);
01046 
01052     static ErrorFunction& dummyError;
01053 
01059     inline QPSolver* get_Solver()
01060     {
01061         return solver;
01062     }
01063 
01065     inline void setAccuracy(double accuracy = 0.001)
01066     {
01067         this->accuracy = accuracy;
01068     }
01069 
01072     inline void setMaxIterations(SharkInt64 maxiter = -1)
01073     {
01074         this->maxIter = maxiter;
01075     }
01076 
01079     inline void setMaxSeconds(int seconds = -1)
01080     {
01081         maxSeconds = seconds;
01082     }
01083 
01085     inline void setVerbose(bool verbose = true)
01086     {
01087         printInfo = verbose;
01088     }
01089 
01091     inline void setCacheSize(unsigned int cacheSize)
01092     {
01093         cacheMB = cacheSize;
01094     }
01095 
01096     inline bool isOptimal()
01097     {
01098         return optimal;
01099     }
01100 
01101 protected:
01102     enum eMode
01103     {
01104         eC1,                // C-SVM with 1-norm penalty for binary classification
01105         eC2,                // C-SVM with 2-norm penalty for binary classification
01106         eEpsilon,           // \varepsilon-SVM for regression
01107         eNu,                // \nu-SVM, not implemented yet
01108         eRegularizationNetwork,
01109         eGaussianProcess,   // Gaussian Process
01110         e1Class,            // One-Class SVM for density estimation XXX
01111         eAllInOne,          // standard multi class SVM
01112         eCrammerSinger,     // MC-SVM by Crammer and Singer
01113         eOVA,               // one-versus-all multi class SVM
01114         eOCC,       // binary-SVM-cost multi-class SVM
01115     };
01116 
01118     eMode mode;
01119 
01121     QPMatrix* matrix;
01122 
01124     CachedMatrix* cache;
01125 
01127     QPSolver* solver;
01128 
01130     double Cplus;
01131 
01133     double Cminus;
01134 
01136     double C;
01137 
01139     double epsilon;
01140 
01142     double gamma;
01143 
01145     double beta;
01146 
01148     double fractionOfOutliers;
01149 
01151     double OneClassBoxUpper;
01152 
01154     bool printInfo;
01155 
01157     unsigned int cacheMB;
01158 
01160     double accuracy;
01161 
01163     SharkInt64 maxIter;
01164 
01166     int maxSeconds;
01167 
01169     bool optimal;
01170 };
01171 
01172 
01173 typedef SVM KernelExpansion;
01174 typedef SVM LinearKernelModel;
01175 
01176 
01177 #endif
01178