SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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>
14 
15 using namespace shogun;
16 #define INDEX(ROW,COL,DIM) (((COL)*(DIM))+(ROW))
17 
19 : CSVM()
20 {
21 }
22 
24 : CSVM(C, k, lab)
25 {
26 }
27 
29 {
30 }
31 
33 {
34  ASSERT(kernel);
36 
37  if (data)
38  {
39  if (labels->get_num_labels() != data->get_num_vectors())
40  SG_ERROR("Number of training vectors does not match number of labels\n");
41  kernel->init(data, data);
42  }
43 
44  int32_t num_data=labels->get_num_labels();
45  SG_INFO("%d trainlabels\n", num_data);
46 
47  float64_t* vector_y = SG_MALLOC(float64_t, num_data);
48  for (int32_t i=0; i<num_data; i++)
49  {
50  if (get_labels()->get_label(i)==+1)
51  vector_y[i]=1;
52  else if (get_labels()->get_label(i)==-1)
53  vector_y[i]=2;
54  else
55  SG_ERROR("label unknown (%f)\n", get_labels()->get_label(i));
56  }
57 
58  float64_t C=get_C1();
59  int32_t tmax=1000000000;
60  float64_t tolabs=0;
61  float64_t tolrel=epsilon;
62 
63  float64_t reg_const=0;
64  if (C!=0)
65  reg_const=1/C;
66 
67  float64_t* diagK=SG_MALLOC(float64_t, num_data);
68  for(int32_t i=0; i<num_data; i++) {
69  diagK[i]=2*kernel->kernel(i,i)+reg_const;
70  }
71 
72  float64_t* alpha=SG_MALLOC(float64_t, num_data);
73  float64_t* vector_c=SG_MALLOC(float64_t, num_data);
74  memset(vector_c, 0, num_data*sizeof(float64_t));
75 
76  float64_t thlb=10000000000.0;
77  int32_t t=0;
78  float64_t* History=NULL;
79  int32_t verb=0;
80  float64_t aHa11, aHa22;
81 
82  CGNPPLib npp(vector_y,kernel,num_data, reg_const);
83 
84  npp.gnpp_imdm(diagK, vector_c, vector_y, num_data,
85  tmax, tolabs, tolrel, thlb, alpha, &t, &aHa11, &aHa22,
86  &History, verb );
87 
88  int32_t num_sv = 0;
89  float64_t nconst = History[INDEX(1,t,2)];
90  float64_t trnerr = 0; /* counter of training error */
91 
92  for(int32_t i = 0; i < num_data; i++ )
93  {
94  if( alpha[i] != 0 ) num_sv++;
95  if(vector_y[i] == 1)
96  {
97  alpha[i] = alpha[i]*2/nconst;
98  if( alpha[i]/(2*C) >= 1 ) trnerr++;
99  }
100  else
101  {
102  alpha[i] = -alpha[i]*2/nconst;
103  if( alpha[i]/(2*C) <= -1 ) trnerr++;
104  }
105  }
106 
107  float64_t b = 0.5*(aHa22 - aHa11)/nconst;;
108 
109  create_new_model(num_sv);
110  CSVM::set_objective(nconst);
111 
112  set_bias(b);
113  int32_t j = 0;
114  for (int32_t i=0; i<num_data; i++)
115  {
116  if( alpha[i] !=0)
117  {
118  set_support_vector(j, i);
119  set_alpha(j, alpha[i]);
120  j++;
121  }
122  }
123 
124  SG_FREE(vector_c);
125  SG_FREE(alpha);
126  SG_FREE(diagK);
127  SG_FREE(vector_y);
128  SG_FREE(History);
129 
130  return true;
131 }
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:83
virtual ~CGNPPSVM()
Definition: GNPPSVM.cpp:28
#define SG_INFO(...)
Definition: SGIO.h:73
virtual float64_t get_label(int32_t i)
Definition: Machine.cpp:76
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:35
class GNPPLib, a Library of solvers for Generalized Nearest Point Problem (GNPP). ...
Definition: GNPPLib.h:31
virtual int32_t get_num_vectors() const =0
int32_t get_num_labels()
Definition: Labels.cpp:240
#define SG_ERROR(...)
Definition: SGIO.h:75
float64_t kernel(int32_t idx_a, int32_t idx_b)
Definition: Kernel.h:218
#define INDEX(ROW, COL, DIM)
Definition: GNPPSVM.cpp:16
float64_t epsilon
Definition: SVM.h:243
float64_t get_C1()
Definition: SVM.h:153
CLabels * labels
Definition: Machine.h:251
#define ASSERT(x)
Definition: SGIO.h:102
void set_bias(float64_t bias)
double float64_t
Definition: common.h:56
bool set_alpha(int32_t idx, float64_t val)
void set_objective(float64_t v)
Definition: SVM.h:201
virtual CLabels * get_labels()
Definition: Machine.cpp:70
bool set_support_vector(int32_t idx, int32_t val)
#define SG_FREE(ptr)
Definition: memory.h:39
The class Features is the base class of all feature objects.
Definition: Features.h:56
A generic Support Vector Machine Interface.
Definition: SVM.h:46
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:355
The Kernel base class.
Definition: Kernel.h:172
virtual bool train_machine(CFeatures *data=NULL)
Definition: GNPPSVM.cpp:32
#define SG_MALLOC(type, len)
Definition: memory.h:36
bool create_new_model(int32_t num)

SHOGUN Machine Learning Toolbox - Documentation