LibSVR.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 "regression/svr/LibSVR.h"
00012 #include "lib/io.h"
00013 
00014 using namespace shogun;
00015 
00016 CLibSVR::CLibSVR()
00017 : CSVM()
00018 {
00019     model=NULL;
00020 }
00021 
00022 CLibSVR::CLibSVR(float64_t C, float64_t eps, CKernel* k, CLabels* lab)
00023 : CSVM()
00024 {
00025     model=NULL;
00026 
00027     set_C(C,C);
00028     set_tube_epsilon(eps);
00029     set_labels(lab);
00030     set_kernel(k);
00031 }
00032 
00033 CLibSVR::~CLibSVR()
00034 {
00035     free(model);
00036 }
00037 
00038 bool CLibSVR::train(CFeatures* data)
00039 {
00040     ASSERT(kernel);
00041     ASSERT(labels && labels->get_num_labels());
00042 
00043     if (data)
00044     {
00045         if (labels->get_num_labels() != data->get_num_vectors())
00046             SG_ERROR("Number of training vectors does not match number of labels\n");
00047         kernel->init(data, data);
00048     }
00049 
00050     free(model);
00051 
00052     struct svm_node* x_space;
00053 
00054     problem.l=labels->get_num_labels();
00055     SG_INFO( "%d trainlabels\n", problem.l);
00056 
00057     problem.y=new float64_t[problem.l];
00058     problem.x=new struct svm_node*[problem.l];
00059     x_space=new struct svm_node[2*problem.l];
00060 
00061     for (int32_t i=0; i<problem.l; i++)
00062     {
00063         problem.y[i]=labels->get_label(i);
00064         problem.x[i]=&x_space[2*i];
00065         x_space[2*i].index=i;
00066         x_space[2*i+1].index=-1;
00067     }
00068 
00069     int32_t weights_label[2]={-1,+1};
00070     float64_t weights[2]={1.0,get_C2()/get_C1()};
00071 
00072     param.svm_type=EPSILON_SVR; // epsilon SVR
00073     param.kernel_type = LINEAR;
00074     param.degree = 3;
00075     param.gamma = 0;    // 1/k
00076     param.coef0 = 0;
00077     param.nu = 0.5;
00078     param.kernel=kernel;
00079     param.cache_size = kernel->get_cache_size();
00080     param.max_train_time = max_train_time;
00081     param.C = get_C1();
00082     param.eps = epsilon;
00083     param.p = tube_epsilon;
00084     param.shrinking = 1;
00085     param.nr_weight = 2;
00086     param.weight_label = weights_label;
00087     param.weight = weights;
00088     param.use_bias = get_bias_enabled();
00089 
00090     const char* error_msg = svm_check_parameter(&problem,&param);
00091 
00092     if(error_msg)
00093         SG_ERROR("Error: %s\n",error_msg);
00094 
00095     model = svm_train(&problem, &param);
00096 
00097     if (model)
00098     {
00099         ASSERT(model->nr_class==2);
00100         ASSERT((model->l==0) || (model->l>0 && model->SV && model->sv_coef && model->sv_coef[0]));
00101 
00102         int32_t num_sv=model->l;
00103 
00104         create_new_model(num_sv);
00105 
00106         CSVM::set_objective(model->objective);
00107 
00108         set_bias(-model->rho[0]);
00109 
00110         for (int32_t i=0; i<num_sv; i++)
00111         {
00112             set_support_vector(i, (model->SV[i])->index);
00113             set_alpha(i, model->sv_coef[0][i]);
00114         }
00115 
00116         delete[] problem.x;
00117         delete[] problem.y;
00118         delete[] x_space;
00119 
00120         svm_destroy_model(model);
00121         model=NULL;
00122         return true;
00123     }
00124     else
00125         return false;
00126 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation