SHOGUN  v2.0.0
 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 
17 CLibSVM::CLibSVM(LIBSVM_SOLVER_TYPE st)
18 : CSVM(), model(NULL), solver_type(st)
19 {
20 }
21 
23 : CSVM(C, k, lab), model(NULL), solver_type(LIBSVM_C_SVC)
24 {
25  problem = svm_problem();
26 }
27 
29 {
30 }
31 
32 
34 {
35  struct svm_node* x_space;
36 
39 
40  if (data)
41  {
42  if (m_labels->get_num_labels() != data->get_num_vectors())
43  {
44  SG_ERROR("%s::train_machine(): Number of training vectors (%d) does"
45  " not match number of labels (%d)\n", get_name(),
47  }
48  kernel->init(data, data);
49  }
50 
52  SG_INFO( "%d trainlabels\n", problem.l);
53 
54  // set linear term
55  if (m_linear_term.vlen>0)
56  {
58  SG_ERROR("Number of training vectors does not match length of linear term\n");
59 
60  // set with linear term from base class
62  }
63  else
64  {
65  // fill with minus ones
67 
68  for (int i=0; i!=problem.l; i++)
69  problem.pv[i] = -1.0;
70  }
71 
73  problem.x=SG_MALLOC(struct svm_node*, problem.l);
75 
76  x_space=SG_MALLOC(struct svm_node, 2*problem.l);
77 
78  for (int32_t i=0; i<problem.l; i++)
79  {
80  problem.y[i]=((CBinaryLabels*) m_labels)->get_label(i);
81  problem.x[i]=&x_space[2*i];
82  x_space[2*i].index=i;
83  x_space[2*i+1].index=-1;
84  }
85 
86  int32_t weights_label[2]={-1,+1};
87  float64_t weights[2]={1.0,get_C2()/get_C1()};
88 
91 
92  param.svm_type=solver_type; // C SVM or NU_SVM
93  param.kernel_type = LINEAR;
94  param.degree = 3;
95  param.gamma = 0; // 1/k
96  param.coef0 = 0;
97  param.nu = get_nu();
98  param.kernel=kernel;
99  param.cache_size = kernel->get_cache_size();
100  param.max_train_time = m_max_train_time;
101  param.C = get_C1();
102  param.eps = epsilon;
103  param.p = 0.1;
104  param.shrinking = 1;
105  param.nr_weight = 2;
106  param.weight_label = weights_label;
107  param.weight = weights;
108  param.use_bias = get_bias_enabled();
109 
110  const char* error_msg = svm_check_parameter(&problem, &param);
111 
112  if(error_msg)
113  SG_ERROR("Error: %s\n",error_msg);
114 
115  model = svm_train(&problem, &param);
116 
117  if (model)
118  {
119  ASSERT(model->nr_class==2);
120  ASSERT((model->l==0) || (model->l>0 && model->SV && model->sv_coef && model->sv_coef[0]));
121 
122  int32_t num_sv=model->l;
123 
124  create_new_model(num_sv);
125  CSVM::set_objective(model->objective);
126 
127  float64_t sgn=model->label[0];
128 
129  set_bias(-sgn*model->rho[0]);
130 
131  for (int32_t i=0; i<num_sv; i++)
132  {
133  set_support_vector(i, (model->SV[i])->index);
134  set_alpha(i, sgn*model->sv_coef[0][i]);
135  }
136 
137  SG_FREE(problem.x);
138  SG_FREE(problem.y);
139  SG_FREE(problem.pv);
140  SG_FREE(problem.C);
141 
142 
143  SG_FREE(x_space);
144 
145  svm_destroy_model(model);
146  model=NULL;
147  return true;
148  }
149  else
150  return false;
151 }

SHOGUN Machine Learning Toolbox - Documentation