Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <shogun/classifier/svm/SVMLin.h>
00012 #include <shogun/features/Labels.h>
00013 #include <shogun/mathematics/Math.h>
00014 #include <shogun/classifier/svm/ssl.h>
00015 #include <shogun/machine/LinearMachine.h>
00016 #include <shogun/features/DotFeatures.h>
00017 #include <shogun/features/Labels.h>
00018
00019 using namespace shogun;
00020
00021 CSVMLin::CSVMLin()
00022 : CLinearMachine(), C1(1), C2(1), epsilon(1e-5), use_bias(true)
00023 {
00024 }
00025
00026 CSVMLin::CSVMLin(
00027 float64_t C, CDotFeatures* traindat, CLabels* trainlab)
00028 : CLinearMachine(), C1(C), C2(C), epsilon(1e-5), use_bias(true)
00029 {
00030 set_features(traindat);
00031 set_labels(trainlab);
00032 }
00033
00034
00035 CSVMLin::~CSVMLin()
00036 {
00037 }
00038
00039 bool CSVMLin::train_machine(CFeatures* data)
00040 {
00041 ASSERT(labels);
00042
00043 if (data)
00044 {
00045 if (!data->has_property(FP_DOT))
00046 SG_ERROR("Specified features are not of type CDotFeatures\n");
00047 set_features((CDotFeatures*) data);
00048 }
00049
00050 ASSERT(features);
00051
00052 SGVector<float64_t> train_labels=labels->get_labels();
00053 int32_t num_feat=features->get_dim_feature_space();
00054 int32_t num_vec=features->get_num_vectors();
00055
00056 ASSERT(num_vec==train_labels.vlen);
00057 SG_FREE(w);
00058
00059 struct options Options;
00060 struct data Data;
00061 struct vector_double Weights;
00062 struct vector_double Outputs;
00063
00064 Data.l=num_vec;
00065 Data.m=num_vec;
00066 Data.u=0;
00067 Data.n=num_feat+1;
00068 Data.nz=num_feat+1;
00069 Data.Y=train_labels.vector;
00070 Data.features=features;
00071 Data.C = SG_MALLOC(float64_t, Data.l);
00072
00073 Options.algo = SVM;
00074 Options.lambda=1/(2*get_C1());
00075 Options.lambda_u=1/(2*get_C1());
00076 Options.S=10000;
00077 Options.R=0.5;
00078 Options.epsilon = get_epsilon();
00079 Options.cgitermax=10000;
00080 Options.mfnitermax=50;
00081 Options.Cp = get_C2()/get_C1();
00082 Options.Cn = 1;
00083
00084 if (use_bias)
00085 Options.bias=1.0;
00086 else
00087 Options.bias=0.0;
00088
00089 for (int32_t i=0;i<num_vec;i++)
00090 {
00091 if(train_labels.vector[i]>0)
00092 Data.C[i]=Options.Cp;
00093 else
00094 Data.C[i]=Options.Cn;
00095 }
00096 ssl_train(&Data, &Options, &Weights, &Outputs);
00097 ASSERT(Weights.vec && Weights.d==num_feat+1);
00098
00099 float64_t sgn=train_labels.vector[0];
00100 for (int32_t i=0; i<num_feat+1; i++)
00101 Weights.vec[i]*=sgn;
00102
00103 set_w(Weights.vec, num_feat);
00104 set_bias(Weights.vec[num_feat]);
00105
00106 SG_FREE(Weights.vec);
00107 SG_FREE(Data.C);
00108 SG_FREE(Outputs.vec);
00109 train_labels.free_vector();
00110 return true;
00111 }