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

SHOGUN Machine Learning Toolbox - Documentation