SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
LibSVM.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
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  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
12 #include <shogun/io/SGIO.h>
14 
15 using namespace shogun;
16 
18 : CSVM(), model(NULL), solver_type(LIBSVM_C_SVC)
19 {
20 }
21 
22 CLibSVM::CLibSVM(LIBSVM_SOLVER_TYPE st)
23 : CSVM(), model(NULL), solver_type(st)
24 {
25 }
26 
27 
28 CLibSVM::CLibSVM(float64_t C, CKernel* k, CLabels* lab, LIBSVM_SOLVER_TYPE st)
29 : CSVM(C, k, lab), model(NULL), solver_type(st)
30 {
31  problem = svm_problem();
32 }
33 
35 {
36 }
37 
38 
40 {
41  struct svm_node* x_space;
42 
45 
46  if (data)
47  {
48  if (m_labels->get_num_labels() != data->get_num_vectors())
49  {
50  SG_ERROR("%s::train_machine(): Number of training vectors (%d) does"
51  " not match number of labels (%d)\n", get_name(),
53  }
54  kernel->init(data, data);
55  }
56 
58  SG_INFO("%d trainlabels\n", problem.l)
59 
60  // set linear term
61  if (m_linear_term.vlen>0)
62  {
64  SG_ERROR("Number of training vectors does not match length of linear term\n")
65 
66  // set with linear term from base class
68  }
69  else
70  {
71  // fill with minus ones
72  problem.pv = SG_MALLOC(float64_t, problem.l);
73 
74  for (int i=0; i!=problem.l; i++)
75  problem.pv[i] = -1.0;
76  }
77 
78  problem.y=SG_MALLOC(float64_t, problem.l);
79  problem.x=SG_MALLOC(struct svm_node*, problem.l);
80  problem.C=SG_MALLOC(float64_t, problem.l);
81 
82  x_space=SG_MALLOC(struct svm_node, 2*problem.l);
83 
84  for (int32_t i=0; i<problem.l; i++)
85  {
86  problem.y[i]=((CBinaryLabels*) m_labels)->get_label(i);
87  problem.x[i]=&x_space[2*i];
88  x_space[2*i].index=i;
89  x_space[2*i+1].index=-1;
90  }
91 
92  int32_t weights_label[2]={-1,+1};
93  float64_t weights[2]={1.0,get_C2()/get_C1()};
94 
97 
98  switch (solver_type)
99  {
100  case LIBSVM_C_SVC:
101  param.svm_type=C_SVC;
102  break;
103  case LIBSVM_NU_SVC:
104  param.svm_type=NU_SVC;
105  break;
106  default:
107  SG_ERROR("%s::train_machine(): Unknown solver type!\n", get_name());
108  break;
109  }
110 
111  param.kernel_type = LINEAR;
112  param.degree = 3;
113  param.gamma = 0; // 1/k
114  param.coef0 = 0;
115  param.nu = get_nu();
116  param.kernel=kernel;
117  param.cache_size = kernel->get_cache_size();
118  param.max_train_time = m_max_train_time;
119  param.C = get_C1();
120  param.eps = epsilon;
121  param.p = 0.1;
122  param.shrinking = 1;
123  param.nr_weight = 2;
124  param.weight_label = weights_label;
125  param.weight = weights;
126  param.use_bias = get_bias_enabled();
127 
128  const char* error_msg = svm_check_parameter(&problem, &param);
129 
130  if(error_msg)
131  SG_ERROR("Error: %s\n",error_msg)
132 
133  model = svm_train(&problem, &param);
134 
135  if (model)
136  {
137  ASSERT(model->nr_class==2)
138  ASSERT((model->l==0) || (model->l>0 && model->SV && model->sv_coef && model->sv_coef[0]))
139 
140  int32_t num_sv=model->l;
141 
142  create_new_model(num_sv);
143  CSVM::set_objective(model->objective);
144 
145  float64_t sgn=model->label[0];
146 
147  set_bias(-sgn*model->rho[0]);
148 
149  for (int32_t i=0; i<num_sv; i++)
150  {
151  set_support_vector(i, (model->SV[i])->index);
152  set_alpha(i, sgn*model->sv_coef[0][i]);
153  }
154 
155  SG_FREE(problem.x);
156  SG_FREE(problem.y);
157  SG_FREE(problem.pv);
158  SG_FREE(problem.C);
159 
160 
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:98
#define SG_INFO(...)
Definition: SGIO.h:118
virtual ELabelType get_label_type() const =0
binary labels +1/-1
Definition: LabelTypes.h:18
float64_t get_C2()
Definition: SVM.h:167
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
virtual int32_t get_num_labels() const =0
virtual int32_t get_num_vectors() const =0
float64_t m_max_train_time
Definition: Machine.h:358
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
float64_t get_nu()
Definition: SVM.h:155
float64_t epsilon
Definition: SVM.h:251
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:517
LIBSVM_SOLVER_TYPE solver_type
Definition: LibSVM.h:83
float64_t get_C1()
Definition: SVM.h:161
virtual float64_t * get_linear_term_array()
Definition: SVM.cpp:302
svm_parameter param
Definition: LibSVM.h:78
SGVector< float64_t > m_linear_term
Definition: SVM.h:246
index_t vlen
Definition: SGVector.h:494
struct svm_model * model
Definition: LibSVM.h:80
#define ASSERT(x)
Definition: SGIO.h:201
void set_bias(float64_t bias)
double float64_t
Definition: common.h:50
svm_problem problem
Definition: LibSVM.h:76
bool set_alpha(int32_t idx, float64_t val)
void set_objective(float64_t v)
Definition: SVM.h:209
virtual bool train_machine(CFeatures *data=NULL)
Definition: LibSVM.cpp:39
bool set_support_vector(int32_t idx, int32_t val)
virtual const char * get_name() const
Definition: LibSVM.h:61
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The class Features is the base class of all feature objects.
Definition: Features.h:68
A generic Support Vector Machine Interface.
Definition: SVM.h:49
The Kernel base class.
Definition: Kernel.h:159
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
int32_t get_cache_size()
Definition: Kernel.h:599
virtual ~CLibSVM()
Definition: LibSVM.cpp:34
virtual bool has_features()
Definition: Kernel.h:535
bool create_new_model(int32_t num)

SHOGUN Machine Learning Toolbox - Documentation