SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MulticlassLibLinear.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) 2012 Sergey Lisitsyn
8  * Copyright (C) 2012 Sergey Lisitsyn
9  */
10 
11 #include <shogun/lib/config.h>
12 #ifdef HAVE_LAPACK
16 #include <shogun/lib/v_array.h>
17 #include <shogun/lib/Signal.h>
19 
20 using namespace shogun;
21 
24 {
25  init_defaults();
26 }
27 
30 {
31  init_defaults();
32  set_C(C);
33 }
34 
35 void CMulticlassLibLinear::init_defaults()
36 {
37  set_C(1.0);
38  set_epsilon(1e-2);
39  set_max_iter(10000);
40  set_use_bias(false);
41  set_save_train_state(false);
42  m_train_state = NULL;
43 }
44 
45 void CMulticlassLibLinear::register_parameters()
46 {
47  SG_ADD(&m_C, "m_C", "regularization constant",MS_AVAILABLE);
48  SG_ADD(&m_epsilon, "m_epsilon", "tolerance epsilon",MS_NOT_AVAILABLE);
49  SG_ADD(&m_max_iter, "m_max_iter", "max number of iterations",MS_NOT_AVAILABLE);
50  SG_ADD(&m_use_bias, "m_use_bias", "indicates whether bias should be used",MS_NOT_AVAILABLE);
51  SG_ADD(&m_save_train_state, "m_save_train_state", "indicates whether bias should be used",MS_NOT_AVAILABLE);
52 }
53 
55 {
57 }
58 
60 {
61  if (!m_train_state)
62  SG_ERROR("Please enable save_train_state option and train machine.\n");
63 
65 
66  int32_t num_vectors = m_features->get_num_vectors();
67  int32_t num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes();
68 
69  v_array<int32_t> nz_idxs;
70  nz_idxs.reserve(num_vectors);
71 
72  for (int32_t i=0; i<num_vectors; i++)
73  {
74  for (int32_t y=0; y<num_classes; y++)
75  {
76  if (CMath::abs(m_train_state->alpha[i*num_classes+y])>1e-6)
77  {
78  nz_idxs.push(i);
79  break;
80  }
81  }
82  }
83  int32_t num_nz = nz_idxs.index();
84  nz_idxs.reserve(num_nz);
85  return SGVector<int32_t>(nz_idxs.begin,num_nz);
86 }
87 
89 {
90  return SGMatrix<float64_t>();
91 }
92 
94 {
95  if (data)
96  set_features((CDotFeatures*)data);
97 
101 
102  int32_t num_vectors = m_features->get_num_vectors();
103  int32_t num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes();
104  int32_t bias_n = m_use_bias ? 1 : 0;
105 
106  problem mc_problem;
107  mc_problem.l = num_vectors;
108  mc_problem.n = m_features->get_dim_feature_space() + bias_n;
109  mc_problem.y = SG_MALLOC(float64_t, mc_problem.l);
110  for (int32_t i=0; i<num_vectors; i++)
111  mc_problem.y[i] = ((CMulticlassLabels*) m_labels)->get_int_label(i);
112 
113  mc_problem.x = m_features;
114  mc_problem.use_bias = m_use_bias;
115 
117 
118  if (!m_train_state)
119  m_train_state = new mcsvm_state();
120 
121  float64_t* C = SG_MALLOC(float64_t, num_vectors);
122  for (int32_t i=0; i<num_vectors; i++)
123  C[i] = m_C;
124 
126 
127  Solver_MCSVM_CS solver(&mc_problem,num_classes,C,w0.matrix,m_epsilon,
129  solver.solve();
130 
132  for (int32_t i=0; i<num_classes; i++)
133  {
134  CLinearMachine* machine = new CLinearMachine();
135  SGVector<float64_t> cw(mc_problem.n-bias_n);
136 
137  for (int32_t j=0; j<mc_problem.n-bias_n; j++)
138  cw[j] = m_train_state->w[j*num_classes+i];
139 
140  machine->set_w(cw);
141 
142  if (m_use_bias)
143  machine->set_bias(m_train_state->w[(mc_problem.n-bias_n)*num_classes+i]);
144 
145  m_machines->push_back(machine);
146  }
147 
148  if (!m_save_train_state)
150 
151  SG_FREE(C);
152  SG_FREE(mc_problem.y);
153 
154  return true;
155 }
156 #endif /* HAVE_LAPACK */

SHOGUN Machine Learning Toolbox - Documentation