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

SHOGUN Machine Learning Toolbox - Documentation