SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
GNPPSVM.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-2008 Vojtech Franc, xfrancv@cmp.felk.cvut.cz
8  * Copyright (C) 1999-2008 Center for Machine Perception, CTU FEL Prague
9  */
10 
11 #include <shogun/io/SGIO.h>
15 
16 using namespace shogun;
17 #define INDEX(ROW,COL,DIM) (((COL)*(DIM))+(ROW))
18 
20 : CSVM()
21 {
22 }
23 
25 : CSVM(C, k, lab)
26 {
27 }
28 
30 {
31 }
32 
34 {
35  ASSERT(kernel)
38 
39  if (data)
40  {
41  if (m_labels->get_num_labels() != data->get_num_vectors())
42  SG_ERROR("Number of training vectors does not match number of labels\n")
43  kernel->init(data, data);
44  }
45 
46  int32_t num_data=m_labels->get_num_labels();
47  SG_INFO("%d trainlabels\n", num_data)
48 
49  float64_t* vector_y = SG_MALLOC(float64_t, num_data);
50  for (int32_t i=0; i<num_data; i++)
51  {
52  float64_t lab=((CBinaryLabels*) m_labels)->get_label(i);
53  if (lab==+1)
54  vector_y[i]=1;
55  else if (lab==-1)
56  vector_y[i]=2;
57  else
58  SG_ERROR("label unknown (%f)\n", lab)
59  }
60 
61  float64_t C=get_C1();
62  int32_t tmax=1000000000;
63  float64_t tolabs=0;
64  float64_t tolrel=epsilon;
65 
66  float64_t reg_const=0;
67  if (C!=0)
68  reg_const=1/C;
69 
70  float64_t* diagK=SG_MALLOC(float64_t, num_data);
71  for(int32_t i=0; i<num_data; i++) {
72  diagK[i]=2*kernel->kernel(i,i)+reg_const;
73  }
74 
75  float64_t* alpha=SG_MALLOC(float64_t, num_data);
76  float64_t* vector_c=SG_MALLOC(float64_t, num_data);
77  memset(vector_c, 0, num_data*sizeof(float64_t));
78 
79  float64_t thlb=10000000000.0;
80  int32_t t=0;
81  float64_t* History=NULL;
82  int32_t verb=0;
83  float64_t aHa11, aHa22;
84 
85  CGNPPLib npp(vector_y,kernel,num_data, reg_const);
86 
87  npp.gnpp_imdm(diagK, vector_c, vector_y, num_data,
88  tmax, tolabs, tolrel, thlb, alpha, &t, &aHa11, &aHa22,
89  &History, verb );
90 
91  int32_t num_sv = 0;
92  float64_t nconst = History[INDEX(1,t,2)];
93  float64_t trnerr = 0; /* counter of training error */
94 
95  for(int32_t i = 0; i < num_data; i++ )
96  {
97  if( alpha[i] != 0 ) num_sv++;
98  if(vector_y[i] == 1)
99  {
100  alpha[i] = alpha[i]*2/nconst;
101  if( alpha[i]/(2*C) >= 1 ) trnerr++;
102  }
103  else
104  {
105  alpha[i] = -alpha[i]*2/nconst;
106  if( alpha[i]/(2*C) <= -1 ) trnerr++;
107  }
108  }
109 
110  float64_t b = 0.5*(aHa22 - aHa11)/nconst;;
111 
112  create_new_model(num_sv);
113  CSVM::set_objective(nconst);
114 
115  set_bias(b);
116  int32_t j = 0;
117  for (int32_t i=0; i<num_data; i++)
118  {
119  if( alpha[i] !=0)
120  {
121  set_support_vector(j, i);
122  set_alpha(j, alpha[i]);
123  j++;
124  }
125  }
126 
127  SG_FREE(vector_c);
128  SG_FREE(alpha);
129  SG_FREE(diagK);
130  SG_FREE(vector_y);
131  SG_FREE(History);
132 
133  return true;
134 }
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:98
virtual ~CGNPPSVM()
Definition: GNPPSVM.cpp:29
#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
class GNPPLib, a Library of solvers for Generalized Nearest Point Problem (GNPP). ...
Definition: GNPPLib.h:30
virtual int32_t get_num_vectors() const =0
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
float64_t kernel(int32_t idx_a, int32_t idx_b)
Definition: Kernel.h:206
#define INDEX(ROW, COL, DIM)
Definition: GNPPSVM.cpp:17
float64_t epsilon
Definition: SVM.h:251
float64_t get_C1()
Definition: SVM.h:161
#define ASSERT(x)
Definition: SGIO.h:201
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
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
int8_t gnpp_imdm(float64_t *diag_H, float64_t *vector_c, float64_t *vector_y, int32_t dim, int32_t tmax, float64_t tolabs, float64_t tolrel, float64_t th, float64_t *alpha, int32_t *ptr_t, float64_t *ptr_aHa11, float64_t *ptr_aHa22, float64_t **ptr_History, int32_t verb)
Definition: GNPPLib.cpp:354
The Kernel base class.
Definition: Kernel.h:158
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
virtual bool train_machine(CFeatures *data=NULL)
Definition: GNPPSVM.cpp:33
bool create_new_model(int32_t num)

SHOGUN Machine Learning Toolbox - Documentation