SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LibLinearMTL.h
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) 2011-2012 Christian Widmer
8  * Written (W) 2007-2010 Soeren Sonnenburg
9  * Copyright (c) 2007-2009 The LIBLINEAR Project.
10  * Copyright (C) 2007-2012 Fraunhofer Institute FIRST and Max-Planck-Society
11  */
12 
13 #ifndef _LIBLINEARMTL_H___
14 #define _LIBLINEARMTL_H___
15 
16 #include <shogun/lib/config.h>
17 
18 #include <shogun/lib/common.h>
19 #include <shogun/base/Parameter.h>
23 
24 #include <map>
25 
26 namespace shogun
27 {
28 
29 #ifdef HAVE_LAPACK
30 
31 
36 {
37 
38  public:
39 
44  inline const float64_t operator()(index_t i_row, index_t i_col) const
45  {
46 
47  // lookup complexity is O(log n)
48  std::map<index_t, float64_t>::const_iterator it = data[i_row].find(i_col);
49 
50  if (it != data[i_row].end())
51  {
52  // use mapping for lookup
53  return it->second;
54  } else {
55  return 0.0;
56  }
57  }
58 
63  {
64  data.clear();
65 
66  // deep copy sparse matrix
67  for (int32_t i=0; i!=sgm.num_vectors; i++)
68  {
69 
71  data.push_back(std::map<index_t, float64_t>());
72 
73  for (int32_t k=0; k!=ts_row.num_feat_entries; k++)
74  {
75  // get data from sparse matrix
77  data[i][e.feat_index] = e.entry;
78  }
79 
80  }
81  }
82 
84  std::vector< std::map<index_t, float64_t> > data;
85 
86 };
87 
88 
91 {
92  public:
94  CLibLinearMTL();
95 
96 
104  float64_t C, CDotFeatures* traindat,
105  CLabels* trainlab);
106 
108  virtual ~CLibLinearMTL();
109 
110 
116 
122  inline void set_C(float64_t c_neg, float64_t c_pos) { C1=c_neg; C2=c_pos; }
123 
128  inline float64_t get_C1() { return C1; }
129 
134  inline float64_t get_C2() { return C2; }
135 
140  inline void set_epsilon(float64_t eps) { epsilon=eps; }
141 
146  inline float64_t get_epsilon() { return epsilon; }
147 
152  inline void set_bias_enabled(bool enable_bias) { use_bias=enable_bias; }
153 
158  inline bool get_bias_enabled() { return use_bias; }
159 
161  virtual const char* get_name() const { return "LibLinearMTL"; }
162 
164  inline int32_t get_max_iterations()
165  {
166  return max_iterations;
167  }
168 
170  inline void set_max_iterations(int32_t max_iter=1000)
171  {
172  max_iterations=max_iter;
173  }
174 
176  inline void set_num_tasks(int32_t nt)
177  {
178  num_tasks = nt;
179  }
180 
182  inline void set_linear_term(SGVector<float64_t> linear_term)
183  {
184  if (!m_labels)
185  SG_ERROR("Please assign labels first!\n")
186 
187  int32_t num_labels=m_labels->get_num_labels();
188 
189  if (num_labels!=linear_term.vlen)
190  {
191  SG_ERROR("Number of labels (%d) does not match number"
192  " of entries (%d) in linear term \n", num_labels,
193  linear_term.vlen);
194  }
195 
196  m_linear_term = linear_term;
197  }
198 
201  {
202  task_indicator_lhs = ti;
203  }
204 
207  {
208  task_indicator_rhs = ti;
209  }
210 
213  {
215  }
216 
219  {
220  graph_laplacian = lap;
221  }
222 
228  {
229  return V;
230  }
231 
237  {
238 
239  int32_t w_size = V.num_rows;
240 
242  for(int32_t k=0; k<w_size*num_tasks; k++)
243  {
244  W.matrix[k] = 0;
245  }
246 
247  for (int32_t s=0; s<num_tasks; s++)
248  {
249  float64_t* v_s = V.get_column_vector(s);
250  for (int32_t t=0; t<num_tasks; t++)
251  {
252  float64_t sim_ts = task_similarity_matrix(s,t);
253  for(int32_t i=0; i<w_size; i++)
254  {
255  W.matrix[t*w_size + i] += sim_ts * v_s[i];
256  }
257  }
258  }
259 
260  return W;
261  }
262 
268  {
269  return alphas;
270  }
271 
276  virtual float64_t compute_primal_obj();
277 
282  virtual float64_t compute_dual_obj();
283 
288  virtual float64_t compute_duality_gap();
289 
290 
291  protected:
300  virtual bool train_machine(CFeatures* data=NULL);
301 
302  private:
304  void init();
305 
306  void solve_l2r_l1l2_svc(
307  const liblinear_problem *prob, double eps, double Cp, double Cn);
308 
309 
310  protected:
316  bool use_bias;
320  int32_t max_iterations;
321 
324 
327 
329  int32_t num_tasks;
330 
333 
336 
338  //SGMatrix<float64_t> task_similarity_matrix;
339  //SGSparseMatrix<float64_t> task_similarity_matrix;
341 
344 
347 
350 
351 };
352 
353 #endif //HAVE_LAPACK
354 
355 } /* namespace shogun */
356 
357 #endif //_LIBLINEARMTL_H___

SHOGUN Machine Learning Toolbox - Documentation