SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
GPBTSVM.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/lib/external/gpdt.h>
13 #include <shogun/lib/external/gpdtsolve.h>
14 #include <shogun/io/SGIO.h>
16 
17 using namespace shogun;
18 
20 : CSVM(), model(NULL)
21 {
22 }
23 
25 : CSVM(C, k, lab), model(NULL)
26 {
27 }
28 
30 {
31  SG_FREE(model);
32 }
33 
35 {
36  float64_t* solution; /* store the solution found */
37  QPproblem prob; /* object containing the solvers */
38 
39  ASSERT(kernel)
42  if (data)
43  {
44  if (m_labels->get_num_labels() != data->get_num_vectors())
45  SG_ERROR("Number of training vectors does not match number of labels\n")
46  kernel->init(data, data);
47  }
48 
49  SGVector<int32_t> lab=((CBinaryLabels*) m_labels)->get_int_labels();
50  prob.KER=new sKernel(kernel, lab.vlen);
51  prob.y=lab.vector;
52  prob.ell=lab.vlen;
53  SG_INFO("%d trainlabels\n", prob.ell)
54 
55  // /*** set options defaults ***/
56  prob.delta = epsilon;
57  prob.maxmw = kernel->get_cache_size();
58  prob.verbosity = 0;
59  prob.preprocess_size = -1;
60  prob.projection_projector = -1;
61  prob.c_const = get_C1();
62  prob.chunk_size = get_qpsize();
63  prob.linadd = get_linadd_enabled();
64 
65  if (prob.chunk_size < 2) prob.chunk_size = 2;
66  if (prob.q <= 0) prob.q = prob.chunk_size / 3;
67  if (prob.q < 2) prob.q = 2;
68  if (prob.q > prob.chunk_size) prob.q = prob.chunk_size;
69  prob.q = prob.q & (~1);
70  if (prob.maxmw < 5)
71  prob.maxmw = 5;
72 
73  /*** set the problem description for final report ***/
74  SG_INFO("\nTRAINING PARAMETERS:\n")
75  SG_INFO("\tNumber of training documents: %d\n", prob.ell)
76  SG_INFO("\tq: %d\n", prob.chunk_size)
77  SG_INFO("\tn: %d\n", prob.q)
78  SG_INFO("\tC: %lf\n", prob.c_const)
79  SG_INFO("\tkernel type: %d\n", prob.ker_type)
80  SG_INFO("\tcache size: %dMb\n", prob.maxmw)
81  SG_INFO("\tStopping tolerance: %lf\n", prob.delta)
82 
83  // /*** compute the number of cache rows up to maxmw Mb. ***/
84  if (prob.preprocess_size == -1)
85  prob.preprocess_size = (int32_t) ( (float64_t)prob.chunk_size * 1.5 );
86 
87  if (prob.projection_projector == -1)
88  {
89  if (prob.chunk_size <= 20) prob.projection_projector = 0;
90  else prob.projection_projector = 1;
91  }
92 
93  /*** compute the problem solution *******************************************/
94  solution = SG_MALLOC(float64_t, prob.ell);
95  prob.gpdtsolve(solution);
96  /****************************************************************************/
97 
98  CSVM::set_objective(prob.objective_value);
99 
100  int32_t num_sv=0;
101  int32_t bsv=0;
102  int32_t i=0;
103  int32_t k=0;
104 
105  for (i = 0; i < prob.ell; i++)
106  {
107  if (solution[i] > prob.DELTAsv)
108  {
109  num_sv++;
110  if (solution[i] > (prob.c_const - prob.DELTAsv)) bsv++;
111  }
112  }
113 
114  create_new_model(num_sv);
115  set_bias(prob.bee);
116 
117  SG_INFO("SV: %d BSV = %d\n", num_sv, bsv)
118 
119  for (i = 0; i < prob.ell; i++)
120  {
121  if (solution[i] > prob.DELTAsv)
122  {
123  set_support_vector(k, i);
124  set_alpha(k++, solution[i]*((CBinaryLabels*) m_labels)->get_label(i));
125  }
126  }
127 
128  delete prob.KER;
129  SG_FREE(solution);
130 
131  return true;
132 }
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
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
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
virtual bool train_machine(CFeatures *data=NULL)
Definition: GPBTSVM.cpp:34
float64_t epsilon
Definition: SVM.h:251
float64_t get_C1()
Definition: SVM.h:161
index_t vlen
Definition: SGVector.h:494
#define ASSERT(x)
Definition: SGIO.h:201
virtual ~CGPBTSVM()
Definition: GPBTSVM.cpp:29
void set_bias(float64_t bias)
double float64_t
Definition: common.h:50
bool set_alpha(int32_t idx, float64_t val)
void set_objective(float64_t v)
Definition: SVM.h:209
struct svm_model * model
Definition: GPBTSVM.h:60
bool set_support_vector(int32_t idx, int32_t val)
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:158
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
int32_t get_cache_size()
Definition: Kernel.h:598
int32_t get_qpsize()
Definition: SVM.h:173
bool create_new_model(int32_t num)

SHOGUN Machine Learning Toolbox - Documentation