SHOGUN  3.2.1
 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>
15 #include <shogun/lib/v_array.h>
16 #include <shogun/lib/Signal.h>
18 
19 using namespace shogun;
20 
23 {
24  init_defaults();
25 }
26 
29 {
30  init_defaults();
31  set_C(C);
32 }
33 
34 void CMulticlassLibLinear::init_defaults()
35 {
36  set_C(1.0);
37  set_epsilon(1e-2);
38  set_max_iter(10000);
39  set_use_bias(false);
40  set_save_train_state(false);
41  m_train_state = NULL;
42 }
43 
44 void CMulticlassLibLinear::register_parameters()
45 {
46  SG_ADD(&m_C, "m_C", "regularization constant",MS_AVAILABLE);
47  SG_ADD(&m_epsilon, "m_epsilon", "tolerance epsilon",MS_NOT_AVAILABLE);
48  SG_ADD(&m_max_iter, "m_max_iter", "max number of iterations",MS_NOT_AVAILABLE);
49  SG_ADD(&m_use_bias, "m_use_bias", "indicates whether bias should be used",MS_NOT_AVAILABLE);
50  SG_ADD(&m_save_train_state, "m_save_train_state", "indicates whether bias should be used",MS_NOT_AVAILABLE);
51 }
52 
54 {
56 }
57 
59 {
60  if (!m_train_state)
61  SG_ERROR("Please enable save_train_state option and train machine.\n")
62 
64 
65  int32_t num_vectors = m_features->get_num_vectors();
66  int32_t num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes();
67 
68  v_array<int32_t> nz_idxs;
69  nz_idxs.reserve(num_vectors);
70 
71  for (int32_t i=0; i<num_vectors; i++)
72  {
73  for (int32_t y=0; y<num_classes; y++)
74  {
75  if (CMath::abs(m_train_state->alpha[i*num_classes+y])>1e-6)
76  {
77  nz_idxs.push(i);
78  break;
79  }
80  }
81  }
82  int32_t num_nz = nz_idxs.index();
83  nz_idxs.reserve(num_nz);
84  return SGVector<int32_t>(nz_idxs.begin,num_nz);
85 }
86 
88 {
89  return SGMatrix<float64_t>();
90 }
91 
93 {
94  if (data)
95  set_features((CDotFeatures*)data);
96 
100 
101  int32_t num_vectors = m_features->get_num_vectors();
102  int32_t num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes();
103  int32_t bias_n = m_use_bias ? 1 : 0;
104 
105  liblinear_problem mc_problem;
106  mc_problem.l = num_vectors;
107  mc_problem.n = m_features->get_dim_feature_space() + bias_n;
108  mc_problem.y = SG_MALLOC(float64_t, mc_problem.l);
109  for (int32_t i=0; i<num_vectors; i++)
110  mc_problem.y[i] = ((CMulticlassLabels*) m_labels)->get_int_label(i);
111 
112  mc_problem.x = m_features;
113  mc_problem.use_bias = m_use_bias;
114 
116 
117  if (!m_train_state)
118  m_train_state = new mcsvm_state();
119 
120  float64_t* C = SG_MALLOC(float64_t, num_vectors);
121  for (int32_t i=0; i<num_vectors; i++)
122  C[i] = m_C;
123 
125 
126  Solver_MCSVM_CS solver(&mc_problem,num_classes,C,w0.matrix,m_epsilon,
128  solver.solve();
129 
131  for (int32_t i=0; i<num_classes; i++)
132  {
133  CLinearMachine* machine = new CLinearMachine();
134  SGVector<float64_t> cw(mc_problem.n-bias_n);
135 
136  for (int32_t j=0; j<mc_problem.n-bias_n; j++)
137  cw[j] = m_train_state->w[j*num_classes+i];
138 
139  machine->set_w(cw);
140 
141  if (m_use_bias)
142  machine->set_bias(m_train_state->w[(mc_problem.n-bias_n)*num_classes+i]);
143 
144  m_machines->push_back(machine);
145  }
146 
147  if (!m_save_train_state)
149 
150  SG_FREE(C);
151  SG_FREE(mc_problem.y);
152 
153  return true;
154 }

SHOGUN Machine Learning Toolbox - Documentation