SHOGUN  3.2.1
 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>
24 
25 #include <map>
26 
27 namespace shogun
28 {
29 
30 #ifdef HAVE_LAPACK
31 
32 
37 {
38 
39  public:
40 
45  inline const float64_t operator()(index_t i_row, index_t i_col) const
46  {
47 
48  // lookup complexity is O(log n)
49  std::map<index_t, float64_t>::const_iterator it = data[i_row].find(i_col);
50 
51  if (it != data[i_row].end())
52  {
53  // use mapping for lookup
54  return it->second;
55  } else {
56  return 0.0;
57  }
58  }
59 
64  {
65  data.clear();
66 
67  // deep copy sparse matrix
68  for (int32_t i=0; i!=sgm.num_vectors; i++)
69  {
70 
72  data.push_back(std::map<index_t, float64_t>());
73 
74  for (int32_t k=0; k!=ts_row.num_feat_entries; k++)
75  {
76  // get data from sparse matrix
78  data[i][e.feat_index] = e.entry;
79  }
80 
81  }
82  }
83 
85  std::vector< std::map<index_t, float64_t> > data;
86 
87 };
88 
89 
92 {
93  public:
95  CLibLinearMTL();
96 
97 
105  float64_t C, CDotFeatures* traindat,
106  CLabels* trainlab);
107 
109  virtual ~CLibLinearMTL();
110 
111 
117 
123  inline void set_C(float64_t c_neg, float64_t c_pos) { C1=c_neg; C2=c_pos; }
124 
129  inline float64_t get_C1() { return C1; }
130 
135  inline float64_t get_C2() { return C2; }
136 
141  inline void set_epsilon(float64_t eps) { epsilon=eps; }
142 
147  inline float64_t get_epsilon() { return epsilon; }
148 
153  inline void set_bias_enabled(bool enable_bias) { use_bias=enable_bias; }
154 
159  inline bool get_bias_enabled() { return use_bias; }
160 
162  virtual const char* get_name() const { return "LibLinearMTL"; }
163 
165  inline int32_t get_max_iterations()
166  {
167  return max_iterations;
168  }
169 
171  inline void set_max_iterations(int32_t max_iter=1000)
172  {
173  max_iterations=max_iter;
174  }
175 
177  inline void set_num_tasks(int32_t nt)
178  {
179  num_tasks = nt;
180  }
181 
183  inline void set_linear_term(SGVector<float64_t> linear_term)
184  {
185  if (!m_labels)
186  SG_ERROR("Please assign labels first!\n")
187 
188  int32_t num_labels=m_labels->get_num_labels();
189 
190  if (num_labels!=linear_term.vlen)
191  {
192  SG_ERROR("Number of labels (%d) does not match number"
193  " of entries (%d) in linear term \n", num_labels,
194  linear_term.vlen);
195  }
196 
197  m_linear_term = linear_term;
198  }
199 
202  {
203  task_indicator_lhs = ti;
204  }
205 
208  {
209  task_indicator_rhs = ti;
210  }
211 
214  {
216  }
217 
220  {
221  graph_laplacian = lap;
222  }
223 
229  {
230  return V;
231  }
232 
238  {
239 
240  int32_t w_size = V.num_rows;
241 
243  for(int32_t k=0; k<w_size*num_tasks; k++)
244  {
245  W.matrix[k] = 0;
246  }
247 
248  for (int32_t s=0; s<num_tasks; s++)
249  {
250  float64_t* v_s = V.get_column_vector(s);
251  for (int32_t t=0; t<num_tasks; t++)
252  {
253  float64_t sim_ts = task_similarity_matrix(s,t);
254  for(int32_t i=0; i<w_size; i++)
255  {
256  W.matrix[t*w_size + i] += sim_ts * v_s[i];
257  }
258  }
259  }
260 
261  return W;
262  }
263 
269  {
270  return alphas;
271  }
272 
277  virtual float64_t compute_primal_obj();
278 
283  virtual float64_t compute_dual_obj();
284 
289  virtual float64_t compute_duality_gap();
290 
291 
292  protected:
301  virtual bool train_machine(CFeatures* data=NULL);
302 
303  private:
305  void init();
306 
307  void solve_l2r_l1l2_svc(
308  const liblinear_problem *prob, double eps, double Cp, double Cn);
309 
310 
311  protected:
317  bool use_bias;
321  int32_t max_iterations;
322 
325 
328 
330  int32_t num_tasks;
331 
334 
337 
339  //SGMatrix<float64_t> task_similarity_matrix;
340  //SGSparseMatrix<float64_t> task_similarity_matrix;
342 
345 
348 
351 
352 };
353 
354 #endif //HAVE_LAPACK
355 
356 } /* namespace shogun */
357 
358 #endif //_LIBLINEARMTL_H___

SHOGUN Machine Learning Toolbox - Documentation