SHOGUN
v1.1.0
|
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/regression/svr/LibSVR.h> 00012 #include <shogun/io/SGIO.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 SG_FREE(model); 00036 } 00037 00038 EClassifierType CLibSVR::get_classifier_type() 00039 { 00040 return CT_LIBSVR; 00041 } 00042 00043 bool CLibSVR::train_machine(CFeatures* data) 00044 { 00045 ASSERT(kernel); 00046 ASSERT(labels && labels->get_num_labels()); 00047 00048 if (data) 00049 { 00050 if (labels->get_num_labels() != data->get_num_vectors()) 00051 SG_ERROR("Number of training vectors does not match number of labels\n"); 00052 kernel->init(data, data); 00053 } 00054 00055 SG_FREE(model); 00056 00057 struct svm_node* x_space; 00058 00059 problem.l=labels->get_num_labels(); 00060 SG_INFO( "%d trainlabels\n", problem.l); 00061 00062 problem.y=SG_MALLOC(float64_t, problem.l); 00063 problem.x=SG_MALLOC(struct svm_node*, problem.l); 00064 x_space=SG_MALLOC(struct svm_node, 2*problem.l); 00065 00066 for (int32_t i=0; i<problem.l; i++) 00067 { 00068 problem.y[i]=labels->get_label(i); 00069 problem.x[i]=&x_space[2*i]; 00070 x_space[2*i].index=i; 00071 x_space[2*i+1].index=-1; 00072 } 00073 00074 int32_t weights_label[2]={-1,+1}; 00075 float64_t weights[2]={1.0,get_C2()/get_C1()}; 00076 00077 param.svm_type=EPSILON_SVR; // epsilon SVR 00078 param.kernel_type = LINEAR; 00079 param.degree = 3; 00080 param.gamma = 0; // 1/k 00081 param.coef0 = 0; 00082 param.nu = 0.5; 00083 param.kernel=kernel; 00084 param.cache_size = kernel->get_cache_size(); 00085 param.max_train_time = max_train_time; 00086 param.C = get_C1(); 00087 param.eps = epsilon; 00088 param.p = tube_epsilon; 00089 param.shrinking = 1; 00090 param.nr_weight = 2; 00091 param.weight_label = weights_label; 00092 param.weight = weights; 00093 param.use_bias = get_bias_enabled(); 00094 00095 const char* error_msg = svm_check_parameter(&problem,¶m); 00096 00097 if(error_msg) 00098 SG_ERROR("Error: %s\n",error_msg); 00099 00100 model = svm_train(&problem, ¶m); 00101 00102 if (model) 00103 { 00104 ASSERT(model->nr_class==2); 00105 ASSERT((model->l==0) || (model->l>0 && model->SV && model->sv_coef && model->sv_coef[0])); 00106 00107 int32_t num_sv=model->l; 00108 00109 create_new_model(num_sv); 00110 00111 CSVM::set_objective(model->objective); 00112 00113 set_bias(-model->rho[0]); 00114 00115 for (int32_t i=0; i<num_sv; i++) 00116 { 00117 set_support_vector(i, (model->SV[i])->index); 00118 set_alpha(i, model->sv_coef[0][i]); 00119 } 00120 00121 SG_FREE(problem.x); 00122 SG_FREE(problem.y); 00123 SG_FREE(x_space); 00124 00125 svm_destroy_model(model); 00126 model=NULL; 00127 return true; 00128 } 00129 else 00130 return false; 00131 }