SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
LibSVR.cpp
Go to the documentation of this file.
1 /*
2 
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Written (W) 2013 Heiko Strathmann
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
14 #include <shogun/io/SGIO.h>
15 
16 using namespace shogun;
17 
19 : CSVM()
20 {
21  register_params();
22  solver_type=LIBSVR_EPSILON_SVR;
23 }
24 
26  LIBSVR_SOLVER_TYPE st)
27 : CSVM()
28 {
29  register_params();
30  set_C(C,C);
31 
32  switch (st)
33  {
34  case LIBSVR_EPSILON_SVR:
35  set_tube_epsilon(svr_param);
36  break;
37  case LIBSVR_NU_SVR:
38  set_nu(svr_param);
39  break;
40  default:
41  SG_ERROR("CLibSVR::CLibSVR(): Unknown solver type!\n");
42  break;
43  }
44 
45  set_labels(lab);
46  set_kernel(k);
47  solver_type=st;
48 }
49 
51 {
52 }
53 
54 void CLibSVR::register_params()
55 {
56  SG_ADD((machine_int_t*) &solver_type, "libsvr_solver_type", "LibSVR Solver type", MS_NOT_AVAILABLE);
57 }
58 
60 {
61  return CT_LIBSVR;
62 }
63 
65 {
66  svm_problem problem;
67  svm_parameter param;
68  struct svm_model* model = nullptr;
69 
70  ASSERT(kernel)
73 
74  if (data)
75  {
76  if (m_labels->get_num_labels() != data->get_num_vectors())
77  SG_ERROR("Number of training vectors does not match number of labels\n")
78  kernel->init(data, data);
79  }
80 
81  SG_FREE(model);
82 
83  struct svm_node* x_space;
84 
85  problem.l=m_labels->get_num_labels();
86  SG_INFO("%d trainlabels\n", problem.l)
87 
88  problem.y=SG_MALLOC(float64_t, problem.l);
89  problem.x=SG_MALLOC(struct svm_node*, problem.l);
90  x_space=SG_MALLOC(struct svm_node, 2*problem.l);
91 
92  for (int32_t i=0; i<problem.l; i++)
93  {
94  problem.y[i]=((CRegressionLabels*) m_labels)->get_label(i);
95  problem.x[i]=&x_space[2*i];
96  x_space[2*i].index=i;
97  x_space[2*i+1].index=-1;
98  }
99 
100  int32_t weights_label[2]={-1,+1};
101  float64_t weights[2]={1.0,get_C2()/get_C1()};
102 
103  switch (solver_type)
104  {
105  case LIBSVR_EPSILON_SVR:
106  param.svm_type=EPSILON_SVR;
107  break;
108  case LIBSVR_NU_SVR:
109  param.svm_type=NU_SVR;
110  break;
111  default:
112  SG_ERROR("%s::train_machine(): Unknown solver type!\n", get_name());
113  break;
114  }
115 
116  param.kernel_type = LINEAR;
117  param.degree = 3;
118  param.gamma = 0; // 1/k
119  param.coef0 = 0;
120  param.nu = nu;
121  param.kernel=kernel;
122  param.cache_size = kernel->get_cache_size();
123  param.max_train_time = m_max_train_time;
124  param.C = get_C1();
125  param.eps = epsilon;
126  param.p = tube_epsilon;
127  param.shrinking = 1;
128  param.nr_weight = 2;
129  param.weight_label = weights_label;
130  param.weight = weights;
131  param.use_bias = get_bias_enabled();
132 
133  const char* error_msg = svm_check_parameter(&problem,&param);
134 
135  if(error_msg)
136  SG_ERROR("Error: %s\n",error_msg)
137 
138  model = svm_train(&problem, &param);
139 
140  if (model)
141  {
142  ASSERT(model->nr_class==2)
143  ASSERT((model->l==0) || (model->l>0 && model->SV && model->sv_coef && model->sv_coef[0]))
144 
145  int32_t num_sv=model->l;
146 
147  create_new_model(num_sv);
148 
149  CSVM::set_objective(model->objective);
150 
151  set_bias(-model->rho[0]);
152 
153  for (int32_t i=0; i<num_sv; i++)
154  {
155  set_support_vector(i, (model->SV[i])->index);
156  set_alpha(i, model->sv_coef[0][i]);
157  }
158 
159  SG_FREE(problem.x);
160  SG_FREE(problem.y);
161  SG_FREE(x_space);
162 
163  svm_destroy_model(model);
164  model=NULL;
165  return true;
166  }
167  else
168  return false;
169 }
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:96
EMachineType
Definition: Machine.h:33
virtual bool train_machine(CFeatures *data=NULL)
Definition: LibSVR.cpp:64
#define SG_INFO(...)
Definition: SGIO.h:117
virtual ELabelType get_label_type() const =0
float64_t get_C2()
Definition: SVM.h:167
Real Labels are real-valued labels.
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
virtual int32_t get_num_labels() const =0
real valued labels (e.g. for regression, classifier outputs)
Definition: LabelTypes.h:22
virtual ~CLibSVR()
Definition: LibSVR.cpp:50
virtual int32_t get_num_vectors() const =0
float64_t m_max_train_time
Definition: Machine.h:362
CLabels * m_labels
Definition: Machine.h:365
#define SG_ERROR(...)
Definition: SGIO.h:128
float64_t epsilon
Definition: SVM.h:266
int32_t cache_size
cache_size in MB
void set_nu(float64_t nue)
Definition: SVM.h:107
virtual EMachineType get_classifier_type()
Definition: LibSVR.cpp:59
float64_t get_C1()
Definition: SVM.h:161
#define ASSERT(x)
Definition: SGIO.h:200
void set_bias(float64_t bias)
double float64_t
Definition: common.h:60
bool set_alpha(int32_t idx, float64_t val)
void set_objective(float64_t v)
Definition: SVM.h:209
bool set_support_vector(int32_t idx, int32_t val)
float64_t tube_epsilon
Definition: SVM.h:268
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
int machine_int_t
Definition: common.h:69
The class Features is the base class of all feature objects.
Definition: Features.h:68
LIBSVR_SOLVER_TYPE solver_type
Definition: LibSVR.h:116
virtual const char * get_name() const
Definition: LibSVR.h:99
A generic Support Vector Machine Interface.
Definition: SVM.h:49
The Kernel base class.
int32_t get_cache_size()
void set_kernel(CKernel *k)
#define SG_ADD(...)
Definition: SGObject.h:94
float64_t nu
Definition: SVM.h:270
virtual void set_labels(CLabels *lab)
Definition: Machine.cpp:65
void set_C(float64_t c_neg, float64_t c_pos)
Definition: SVM.h:118
bool create_new_model(int32_t num)
void set_tube_epsilon(float64_t eps)
Definition: SVM.h:131

SHOGUN Machine Learning Toolbox - Documentation