SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 }

SHOGUN Machine Learning Toolbox - Documentation