LibLinear.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "lib/config.h"
00011
00012 #ifdef HAVE_LAPACK
00013 #include "lib/io.h"
00014 #include "classifier/svm/LibLinear.h"
00015 #include "classifier/svm/SVM_linear.h"
00016 #include "classifier/svm/Tron.h"
00017 #include "features/DotFeatures.h"
00018
00019 using namespace shogun;
00020
00021 CLibLinear::CLibLinear(LIBLINEAR_LOSS l)
00022 : CLinearClassifier()
00023 {
00024 loss=l;
00025 use_bias=false;
00026 C1=1;
00027 C2=1;
00028 }
00029
00030 CLibLinear::CLibLinear(
00031 float64_t C, CDotFeatures* traindat, CLabels* trainlab)
00032 : CLinearClassifier(), C1(C), C2(C), use_bias(true), epsilon(1e-5)
00033 {
00034 set_features(traindat);
00035 set_labels(trainlab);
00036 loss=LR;
00037 }
00038
00039
00040 CLibLinear::~CLibLinear()
00041 {
00042 }
00043
00044 bool CLibLinear::train(CFeatures* data)
00045 {
00046 ASSERT(labels);
00047 if (data)
00048 {
00049 if (!data->has_property(FP_DOT))
00050 SG_ERROR("Specified features are not of type CDotFeatures\n");
00051
00052 set_features((CDotFeatures*) data);
00053 }
00054 ASSERT(features);
00055 ASSERT(labels->is_two_class_labeling());
00056
00057 int32_t num_train_labels=labels->get_num_labels();
00058 int32_t num_feat=features->get_dim_feature_space();
00059 int32_t num_vec=features->get_num_vectors();
00060
00061 ASSERT(num_vec==num_train_labels);
00062 delete[] w;
00063 if (use_bias)
00064 w=new float64_t[num_feat+1];
00065 else
00066 w=new float64_t[num_feat+0];
00067 w_dim=num_feat;
00068
00069 problem prob;
00070 if (use_bias)
00071 {
00072 prob.n=w_dim+1;
00073 memset(w, 0, sizeof(float64_t)*(w_dim+1));
00074 }
00075 else
00076 {
00077 prob.n=w_dim;
00078 memset(w, 0, sizeof(float64_t)*(w_dim+0));
00079 }
00080 prob.l=num_vec;
00081 prob.x=features;
00082 prob.y=new int[prob.l];
00083 prob.use_bias=use_bias;
00084
00085 for (int32_t i=0; i<prob.l; i++)
00086 prob.y[i]=labels->get_int_label(i);
00087
00088 SG_INFO( "%d training points %d dims\n", prob.l, prob.n);
00089
00090 function *fun_obj=NULL;
00091
00092 switch (loss)
00093 {
00094 case LR:
00095 fun_obj=new l2_lr_fun(&prob, get_C1(), get_C2());
00096 break;
00097 case L2:
00098 fun_obj=new l2loss_svm_fun(&prob, get_C1(), get_C2());
00099 break;
00100 default:
00101 SG_ERROR("unknown loss\n");
00102 break;
00103 }
00104
00105 if (fun_obj)
00106 {
00107 CTron tron_obj(fun_obj, epsilon);
00108 tron_obj.tron(w);
00109 float64_t sgn=prob.y[0];
00110
00111 for (int32_t i=0; i<w_dim; i++)
00112 w[i]*=sgn;
00113
00114 if (use_bias)
00115 set_bias(sgn*w[w_dim]);
00116 else
00117 set_bias(0);
00118
00119 delete fun_obj;
00120 }
00121
00122 delete[] prob.y;
00123
00124 return true;
00125 }
00126 #endif //HAVE_LAPACK