GNPPSVM.cpp

Go to the documentation of this file.
00001 /* 
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 1999-2008 Vojtech Franc, xfrancv@cmp.felk.cvut.cz
00008  * Copyright (C) 1999-2008 Center for Machine Perception, CTU FEL Prague 
00009  */
00010 
00011 #include "lib/io.h"
00012 #include "classifier/svm/GNPPSVM.h"
00013 #include "classifier/svm/gnpplib.h"
00014 
00015 #define INDEX(ROW,COL,DIM) (((COL)*(DIM))+(ROW)) 
00016 
00017 CGNPPSVM::CGNPPSVM()
00018 : CSVM()
00019 {
00020 }
00021 
00022 CGNPPSVM::CGNPPSVM(float64_t C, CKernel* k, CLabels* lab)
00023 : CSVM(C, k, lab)
00024 {
00025 }
00026 
00027 CGNPPSVM::~CGNPPSVM()
00028 {
00029 }
00030 
00031 bool CGNPPSVM::train()
00032 {
00033     ASSERT(kernel);
00034     ASSERT(labels && labels->get_num_labels());
00035 
00036     int32_t num_data=labels->get_num_labels();
00037     SG_INFO("%d trainlabels\n", num_data);
00038 
00039     float64_t* vector_y = new float64_t[num_data];
00040     for (int32_t i=0; i<num_data; i++)
00041     {
00042         if (get_labels()->get_label(i)==+1)
00043             vector_y[i]=1;
00044         else if (get_labels()->get_label(i)==-1)
00045             vector_y[i]=2;
00046         else
00047             SG_ERROR("label unknown (%f)\n", get_labels()->get_label(i));
00048     }
00049 
00050     float64_t C=get_C1();
00051     int32_t tmax=1000000000;
00052     float64_t tolabs=0;
00053     float64_t tolrel=epsilon;
00054 
00055     float64_t reg_const=0;
00056     if (C!=0)
00057         reg_const=1/C;
00058 
00059     float64_t* diagK=new float64_t[num_data];
00060     for(int32_t i=0; i<num_data; i++) {
00061         diagK[i]=2*kernel->kernel(i,i)+reg_const;
00062     }
00063 
00064     float64_t* alpha=new float64_t[num_data];
00065     float64_t* vector_c=new float64_t[num_data];
00066     memset(vector_c, 0, num_data*sizeof(float64_t));
00067 
00068     float64_t thlb=10000000000.0;
00069     int32_t t=0;
00070     float64_t* History=NULL;
00071     int32_t verb=0;
00072     float64_t aHa11, aHa22;
00073 
00074     CGNPPLib npp(vector_y,kernel,num_data, reg_const);
00075 
00076     npp.gnpp_imdm(diagK, vector_c, vector_y, num_data, 
00077             tmax, tolabs, tolrel, thlb, alpha, &t, &aHa11, &aHa22, 
00078             &History, verb ); 
00079 
00080     int32_t num_sv = 0;
00081     float64_t nconst = History[INDEX(1,t,2)];
00082     float64_t trnerr = 0; /* counter of training error */
00083 
00084     for(int32_t i = 0; i < num_data; i++ )
00085     {
00086         if( alpha[i] != 0 ) num_sv++;
00087         if(vector_y[i] == 1) 
00088         {
00089             alpha[i] = alpha[i]*2/nconst;
00090             if( alpha[i]/(2*C) >= 1 ) trnerr++;
00091         }
00092         else
00093         {
00094             alpha[i] = -alpha[i]*2/nconst;
00095             if( alpha[i]/(2*C) <= -1 ) trnerr++;
00096         }
00097     }
00098 
00099     float64_t b = 0.5*(aHa22 - aHa11)/nconst;;
00100 
00101     create_new_model(num_sv);
00102     CSVM::set_objective(nconst);
00103 
00104     set_bias(b);
00105     int32_t j = 0;
00106     for (int32_t i=0; i<num_data; i++)
00107     {
00108         if( alpha[i] !=0)
00109         {
00110             set_support_vector(j, i);
00111             set_alpha(j, alpha[i]);
00112             j++;
00113         }
00114     }
00115 
00116     delete[] vector_c;
00117     delete[] alpha;
00118     delete[] diagK;
00119     delete[] vector_y;
00120     delete[] History;
00121 
00122     return true;
00123 }

SHOGUN Machine Learning Toolbox - Documentation