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

SHOGUN Machine Learning Toolbox - Documentation