Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <shogun/io/SGIO.h>
00012 #include <shogun/classifier/svm/GNPPSVM.h>
00013 #include <shogun/classifier/svm/GNPPLib.h>
00014
00015 using namespace shogun;
00016 #define INDEX(ROW,COL,DIM) (((COL)*(DIM))+(ROW))
00017
00018 CGNPPSVM::CGNPPSVM()
00019 : CSVM()
00020 {
00021 }
00022
00023 CGNPPSVM::CGNPPSVM(float64_t C, CKernel* k, CLabels* lab)
00024 : CSVM(C, k, lab)
00025 {
00026 }
00027
00028 CGNPPSVM::~CGNPPSVM()
00029 {
00030 }
00031
00032 bool CGNPPSVM::train_machine(CFeatures* data)
00033 {
00034 ASSERT(kernel);
00035 ASSERT(labels && labels->get_num_labels());
00036
00037 if (data)
00038 {
00039 if (labels->get_num_labels() != data->get_num_vectors())
00040 SG_ERROR("Number of training vectors does not match number of labels\n");
00041 kernel->init(data, data);
00042 }
00043
00044 int32_t num_data=labels->get_num_labels();
00045 SG_INFO("%d trainlabels\n", num_data);
00046
00047 float64_t* vector_y = SG_MALLOC(float64_t, num_data);
00048 for (int32_t i=0; i<num_data; i++)
00049 {
00050 if (get_labels()->get_label(i)==+1)
00051 vector_y[i]=1;
00052 else if (get_labels()->get_label(i)==-1)
00053 vector_y[i]=2;
00054 else
00055 SG_ERROR("label unknown (%f)\n", get_labels()->get_label(i));
00056 }
00057
00058 float64_t C=get_C1();
00059 int32_t tmax=1000000000;
00060 float64_t tolabs=0;
00061 float64_t tolrel=epsilon;
00062
00063 float64_t reg_const=0;
00064 if (C!=0)
00065 reg_const=1/C;
00066
00067 float64_t* diagK=SG_MALLOC(float64_t, num_data);
00068 for(int32_t i=0; i<num_data; i++) {
00069 diagK[i]=2*kernel->kernel(i,i)+reg_const;
00070 }
00071
00072 float64_t* alpha=SG_MALLOC(float64_t, num_data);
00073 float64_t* vector_c=SG_MALLOC(float64_t, num_data);
00074 memset(vector_c, 0, num_data*sizeof(float64_t));
00075
00076 float64_t thlb=10000000000.0;
00077 int32_t t=0;
00078 float64_t* History=NULL;
00079 int32_t verb=0;
00080 float64_t aHa11, aHa22;
00081
00082 CGNPPLib npp(vector_y,kernel,num_data, reg_const);
00083
00084 npp.gnpp_imdm(diagK, vector_c, vector_y, num_data,
00085 tmax, tolabs, tolrel, thlb, alpha, &t, &aHa11, &aHa22,
00086 &History, verb );
00087
00088 int32_t num_sv = 0;
00089 float64_t nconst = History[INDEX(1,t,2)];
00090 float64_t trnerr = 0;
00091
00092 for(int32_t i = 0; i < num_data; i++ )
00093 {
00094 if( alpha[i] != 0 ) num_sv++;
00095 if(vector_y[i] == 1)
00096 {
00097 alpha[i] = alpha[i]*2/nconst;
00098 if( alpha[i]/(2*C) >= 1 ) trnerr++;
00099 }
00100 else
00101 {
00102 alpha[i] = -alpha[i]*2/nconst;
00103 if( alpha[i]/(2*C) <= -1 ) trnerr++;
00104 }
00105 }
00106
00107 float64_t b = 0.5*(aHa22 - aHa11)/nconst;;
00108
00109 create_new_model(num_sv);
00110 CSVM::set_objective(nconst);
00111
00112 set_bias(b);
00113 int32_t j = 0;
00114 for (int32_t i=0; i<num_data; i++)
00115 {
00116 if( alpha[i] !=0)
00117 {
00118 set_support_vector(j, i);
00119 set_alpha(j, alpha[i]);
00120 j++;
00121 }
00122 }
00123
00124 SG_FREE(vector_c);
00125 SG_FREE(alpha);
00126 SG_FREE(diagK);
00127 SG_FREE(vector_y);
00128 SG_FREE(History);
00129
00130 return true;
00131 }