SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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>
15 
16 using namespace shogun;
17 
19 : CSVM(), model(NULL)
20 {
21 }
22 
24 : CSVM(C, k, lab), model(NULL)
25 {
26 }
27 
29 {
30  SG_FREE(model);
31 }
32 
34 {
35  float64_t* solution; /* store the solution found */
36  QPproblem prob; /* object containing the solvers */
37 
38  ASSERT(kernel);
41  if (data)
42  {
43  if (labels->get_num_labels() != data->get_num_vectors())
44  SG_ERROR("Number of training vectors does not match number of labels\n");
45  kernel->init(data, data);
46  }
47 
49  prob.KER=new sKernel(kernel, lab.vlen);
50  prob.y=lab.vector;
51  prob.ell=lab.vlen;
52  SG_INFO( "%d trainlabels\n", prob.ell);
53 
54  // /*** set options defaults ***/
55  prob.delta = epsilon;
56  prob.maxmw = kernel->get_cache_size();
57  prob.verbosity = 0;
58  prob.preprocess_size = -1;
59  prob.projection_projector = -1;
60  prob.c_const = get_C1();
61  prob.chunk_size = get_qpsize();
62  prob.linadd = get_linadd_enabled();
63 
64  if (prob.chunk_size < 2) prob.chunk_size = 2;
65  if (prob.q <= 0) prob.q = prob.chunk_size / 3;
66  if (prob.q < 2) prob.q = 2;
67  if (prob.q > prob.chunk_size) prob.q = prob.chunk_size;
68  prob.q = prob.q & (~1);
69  if (prob.maxmw < 5)
70  prob.maxmw = 5;
71 
72  /*** set the problem description for final report ***/
73  SG_INFO( "\nTRAINING PARAMETERS:\n");
74  SG_INFO( "\tNumber of training documents: %d\n", prob.ell);
75  SG_INFO( "\tq: %d\n", prob.chunk_size);
76  SG_INFO( "\tn: %d\n", prob.q);
77  SG_INFO( "\tC: %lf\n", prob.c_const);
78  SG_INFO( "\tkernel type: %d\n", prob.ker_type);
79  SG_INFO( "\tcache size: %dMb\n", prob.maxmw);
80  SG_INFO( "\tStopping tolerance: %lf\n", prob.delta);
81 
82  // /*** compute the number of cache rows up to maxmw Mb. ***/
83  if (prob.preprocess_size == -1)
84  prob.preprocess_size = (int32_t) ( (float64_t)prob.chunk_size * 1.5 );
85 
86  if (prob.projection_projector == -1)
87  {
88  if (prob.chunk_size <= 20) prob.projection_projector = 0;
89  else prob.projection_projector = 1;
90  }
91 
92  /*** compute the problem solution *******************************************/
93  solution = SG_MALLOC(float64_t, prob.ell);
94  prob.gpdtsolve(solution);
95  /****************************************************************************/
96 
97  CSVM::set_objective(prob.objective_value);
98 
99  int32_t num_sv=0;
100  int32_t bsv=0;
101  int32_t i=0;
102  int32_t k=0;
103 
104  for (i = 0; i < prob.ell; i++)
105  {
106  if (solution[i] > prob.DELTAsv)
107  {
108  num_sv++;
109  if (solution[i] > (prob.c_const - prob.DELTAsv)) bsv++;
110  }
111  }
112 
113  create_new_model(num_sv);
114  set_bias(prob.bee);
115 
116  SG_INFO("SV: %d BSV = %d\n", num_sv, bsv);
117 
118  for (i = 0; i < prob.ell; i++)
119  {
120  if (solution[i] > prob.DELTAsv)
121  {
122  set_support_vector(k, i);
123  set_alpha(k++, solution[i]*labels->get_label(i));
124  }
125  }
126 
127  delete prob.KER;
128  lab.free_vector();
129  SG_FREE(solution);
130 
131  return true;
132 }

SHOGUN Machine Learning Toolbox - Documentation