GPBTSVM.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-2009 Soeren Sonnenburg
00008  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include <shogun/classifier/svm/GPBTSVM.h>
00012 #include <shogun/classifier/svm/gpdt.h>
00013 #include <shogun/classifier/svm/gpdtsolve.h>
00014 #include <shogun/io/SGIO.h>
00015 
00016 using namespace shogun;
00017 
00018 CGPBTSVM::CGPBTSVM()
00019 : CSVM(), model(NULL)
00020 {
00021 }
00022 
00023 CGPBTSVM::CGPBTSVM(float64_t C, CKernel* k, CLabels* lab)
00024 : CSVM(C, k, lab), model(NULL)
00025 {
00026 }
00027 
00028 CGPBTSVM::~CGPBTSVM()
00029 {
00030     SG_FREE(model);
00031 }
00032 
00033 bool CGPBTSVM::train_machine(CFeatures* data)
00034 {
00035     float64_t* solution;                     /* store the solution found       */
00036     QPproblem prob;                          /* object containing the solvers  */
00037 
00038     ASSERT(kernel);
00039     ASSERT(labels && labels->get_num_labels());
00040     ASSERT(labels->is_two_class_labeling());
00041     if (data)
00042     {
00043         if (labels->get_num_labels() != data->get_num_vectors())
00044             SG_ERROR("Number of training vectors does not match number of labels\n");
00045         kernel->init(data, data);
00046     }
00047 
00048     SGVector<int32_t> lab=labels->get_int_labels();
00049     prob.KER=new sKernel(kernel, lab.vlen);
00050     prob.y=lab.vector;
00051     prob.ell=lab.vlen;
00052     SG_INFO( "%d trainlabels\n", prob.ell);
00053 
00054     //  /*** set options defaults ***/
00055     prob.delta = epsilon;
00056     prob.maxmw = kernel->get_cache_size();
00057     prob.verbosity       = 0;
00058     prob.preprocess_size = -1;
00059     prob.projection_projector = -1;
00060     prob.c_const = get_C1();
00061     prob.chunk_size = get_qpsize();
00062     prob.linadd = get_linadd_enabled();
00063 
00064     if (prob.chunk_size < 2)      prob.chunk_size = 2;
00065     if (prob.q <= 0)              prob.q = prob.chunk_size / 3;
00066     if (prob.q < 2)               prob.q = 2;
00067     if (prob.q > prob.chunk_size) prob.q = prob.chunk_size;
00068     prob.q = prob.q & (~1);
00069     if (prob.maxmw < 5)
00070         prob.maxmw = 5;
00071 
00072     /*** set the problem description for final report ***/
00073     SG_INFO( "\nTRAINING PARAMETERS:\n");
00074     SG_INFO( "\tNumber of training documents: %d\n", prob.ell);
00075     SG_INFO( "\tq: %d\n", prob.chunk_size);
00076     SG_INFO( "\tn: %d\n", prob.q);
00077     SG_INFO( "\tC: %lf\n", prob.c_const);
00078     SG_INFO( "\tkernel type: %d\n", prob.ker_type);
00079     SG_INFO( "\tcache size: %dMb\n", prob.maxmw);
00080     SG_INFO( "\tStopping tolerance: %lf\n", prob.delta);
00081 
00082     //  /*** compute the number of cache rows up to maxmw Mb. ***/
00083     if (prob.preprocess_size == -1)
00084         prob.preprocess_size = (int32_t) ( (float64_t)prob.chunk_size * 1.5 );
00085 
00086     if (prob.projection_projector == -1)
00087     {
00088         if (prob.chunk_size <= 20) prob.projection_projector = 0;
00089         else prob.projection_projector = 1;
00090     }
00091 
00092     /*** compute the problem solution *******************************************/
00093     solution = SG_MALLOC(float64_t, prob.ell);
00094     prob.gpdtsolve(solution);
00095     /****************************************************************************/
00096 
00097     CSVM::set_objective(prob.objective_value);
00098 
00099     int32_t num_sv=0;
00100     int32_t bsv=0;
00101     int32_t i=0;
00102     int32_t k=0;
00103 
00104     for (i = 0; i < prob.ell; i++)
00105     {
00106         if (solution[i] > prob.DELTAsv)
00107         {
00108             num_sv++;
00109             if (solution[i] > (prob.c_const - prob.DELTAsv)) bsv++;
00110         }
00111     }
00112 
00113     create_new_model(num_sv);
00114     set_bias(prob.bee);
00115 
00116     SG_INFO("SV: %d BSV = %d\n", num_sv, bsv);
00117 
00118     for (i = 0; i < prob.ell; i++)
00119     {
00120         if (solution[i] > prob.DELTAsv)
00121         {
00122             set_support_vector(k, i);
00123             set_alpha(k++, solution[i]*labels->get_label(i));
00124         }
00125     }
00126 
00127     delete prob.KER;
00128     lab.free_vector();
00129     SG_FREE(solution);
00130 
00131     return true;
00132 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation