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 }

SHOGUN Machine Learning Toolbox - Documentation