Feat C++ API
A feature engineering automation tool
MyMulticlassLibLinear.cc
Go to the documentation of this file.
1 /*
2  * This software is distributed under BSD 3-clause license (see LICENSE file).
3  *
4  * Authors: Sergey Lisitsyn, Fernando Iglesias, Yuyu Zhang, Evan Shelhamer,
5  * Bjoern Esser, Soeren Sonnenburg
6  */
7 
9 
10 namespace shogun
11 {
12 
16  CLinearMulticlassMachine()
17  {
18  init_defaults();
19  }
20 
26  CMyMulticlassLibLinear::CMyMulticlassLibLinear(float64_t C, CDotFeatures* features, CLabels* labs) :
27  CLinearMulticlassMachine(new CMulticlassOneVsRestStrategy(),features,NULL,labs)
28  {
29  init_defaults();
30  set_C(C);
31  }
32 
33 
34 
37  {
39  }
40 
41 
42 
45  {
46  return "MulticlassLibLinear";
47  }
48 
52  inline void CMyMulticlassLibLinear::set_C(float64_t C)
53  {
54  ASSERT(C>0)
55  m_C = C;
56  }
57 
61  inline float64_t CMyMulticlassLibLinear::get_C() const { return m_C; }
62 
66  inline void CMyMulticlassLibLinear::set_epsilon(float64_t epsilon)
67  {
68  ASSERT(epsilon>0)
69  m_epsilon = epsilon;
70  }
71 
75  inline float64_t CMyMulticlassLibLinear::get_epsilon() const { return m_epsilon; }
76 
80  inline void CMyMulticlassLibLinear::set_use_bias(bool use_bias)
81  {
82  m_use_bias = use_bias;
83  }
84 
89  {
90  return m_use_bias;
91  }
92 
96  inline void CMyMulticlassLibLinear::set_save_train_state(bool save_train_state)
97  {
98  m_save_train_state = save_train_state;
99  }
100 
105  {
106  return m_save_train_state;
107  }
108 
112  inline void CMyMulticlassLibLinear::set_max_iter(int32_t max_iter)
113  {
114  ASSERT(max_iter>0)
115  m_max_iter = max_iter;
116  }
117 
121  inline int32_t CMyMulticlassLibLinear::get_max_iter() const { return m_max_iter; }
122 
125  {
126  if (m_train_state)
127  {
128  delete m_train_state;
129  m_train_state = NULL;
130  }
131  }
132 
137  {
138  if (!m_train_state)
139  SG_ERROR("Please enable save_train_state option and train machine.\n")
140 
141  ASSERT(m_labels && m_labels->get_label_type() == LT_MULTICLASS)
142 
143  int32_t num_vectors = m_features->get_num_vectors();
144  int32_t num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes();
145 
146  v_array<int32_t> nz_idxs;
147  nz_idxs.reserve(num_vectors);
148 
149  for (int32_t i=0; i<num_vectors; i++)
150  {
151  for (int32_t y=0; y<num_classes; y++)
152  {
153  if (CMath::abs(m_train_state->alpha[i*num_classes+y])>1e-6)
154  {
155  nz_idxs.push(i);
156  break;
157  }
158  }
159  }
160  int32_t num_nz = nz_idxs.index();
161  nz_idxs.reserve(num_nz);
162  return SGVector<int32_t>(nz_idxs.begin,num_nz);
163  }
164 
165  /* get the weights for each SVM Subclass
166  * @return the vector of weights for each subclass
167  */
168  vector<SGVector<float64_t>> CMyMulticlassLibLinear::get_w() const
169  {
170  vector<SGVector<float64_t>> weights_vector;
171  int32_t num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes();
172 
173  for (int32_t i=0; i<num_classes; i++)
174  {
175  CLinearMachine* machine = (CLinearMachine*)m_machines->get_element(i);
176  weights_vector.push_back(machine->get_w().clone());
177  }
178 
179  return weights_vector;
180 
181  }
182 
183  void CMyMulticlassLibLinear::set_w(vector<Eigen::VectorXd> wnew)
184  {
185 
186  int32_t num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes();
187 
188  for (int32_t i=0; i<num_classes; i++)
189  {
190  CLinearMachine* machine = (CLinearMachine*)m_machines->get_element(i);
191  machine->set_w(SGVector<float64_t>(wnew.at(i)));
192  }
193 
194  }
195 
197  {
198  if (data)
199  set_features((CDotFeatures*)data);
200 
201  ASSERT(m_features)
202  ASSERT(m_labels && m_labels->get_label_type()==LT_MULTICLASS)
203  ASSERT(m_multiclass_strategy)
204  init_strategy();
205 
206  int32_t num_vectors = m_features->get_num_vectors();
207  int32_t num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes();
208  int32_t bias_n = m_use_bias ? 1 : 0;
209 
210  liblinear_problem mc_problem;
211  mc_problem.l = num_vectors;
212  mc_problem.n = m_features->get_dim_feature_space() + bias_n;
213  mc_problem.y = SG_MALLOC(float64_t, mc_problem.l);
214  for (int32_t i=0; i<num_vectors; i++)
215  mc_problem.y[i] = ((CMulticlassLabels*) m_labels)->get_int_label(i);
216 
217  mc_problem.x = m_features;
218  mc_problem.use_bias = m_use_bias;
219 
220  SGMatrix<float64_t> w0 = obtain_regularizer_matrix();
221 
222  if (!m_train_state)
223  m_train_state = new mcsvm_state();
224 
225  float64_t* C = SG_MALLOC(float64_t, num_vectors);
226  for (int32_t i=0; i<num_vectors; i++)
227  C[i] = m_C;
228 
229  Solver_MCSVM_CS solver(&mc_problem,num_classes,C,w0.matrix,m_epsilon,
230  m_max_iter,m_max_train_time,m_train_state);
231  solver.solve();
232 
233  m_machines->reset_array();
234  for (int32_t i=0; i<num_classes; i++)
235  {
236  CLinearMachine* machine = new CLinearMachine();
237  SGVector<float64_t> cw(mc_problem.n-bias_n);
238 
239  for (int32_t j=0; j<mc_problem.n-bias_n; j++)
240  cw[j] = m_train_state->w[j*num_classes+i];
241 
242  machine->set_w(cw);
243 
244  if (m_use_bias)
245  machine->set_bias(m_train_state->w[(mc_problem.n-bias_n)*num_classes+i]);
246 
247  m_machines->push_back(machine);
248  }
249 
250  if (!m_save_train_state)
252 
253  SG_FREE(C);
254  SG_FREE(mc_problem.y);
255 
256  return true;
257  }
258 
259 
262  {
263  return SGMatrix<float64_t>();
264  }
265 
267  {
268  set_C(1.0);
269  set_epsilon(1e-2);
270  set_max_iter(10000);
271  set_use_bias(false);
272  set_save_train_state(false);
273  m_train_state = NULL;
274  }
275 
277  {
278  SG_ADD(&m_C, "m_C", "regularization constant",MS_AVAILABLE);
279  SG_ADD(&m_epsilon, "m_epsilon", "tolerance epsilon",MS_NOT_AVAILABLE);
280  SG_ADD(&m_max_iter, "m_max_iter", "max number of iterations",MS_NOT_AVAILABLE);
281  SG_ADD(&m_use_bias, "m_use_bias", "indicates whether bias should be used",MS_NOT_AVAILABLE);
282  SG_ADD(&m_save_train_state, "m_save_train_state", "indicates whether bias should be used",MS_NOT_AVAILABLE);
283  }
284 }
285 
vector< SGVector< float64_t > > get_w() const
void set_epsilon(float64_t epsilon)
virtual const char * get_name() const
void set_w(vector< Eigen::VectorXd > wnew)
SGVector< int32_t > get_support_vectors() const
SGMatrix< float64_t > obtain_regularizer_matrix() const
void set_save_train_state(bool save_train_state)
int i
Definition: params.cc:552