SHOGUN  v3.0.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>
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___
void set_bias_enabled(bool enable_bias)
Definition: LibLinearMTL.h:152
const float64_t operator()(index_t i_row, index_t i_col) const
Definition: LibLinearMTL.h:44
void set_task_indicator_lhs(SGVector< int32_t > ti)
Definition: LibLinearMTL.h:200
EMachineType
Definition: Machine.h:33
template class SGSparseMatrix
void set_C(float64_t c_neg, float64_t c_pos)
Definition: LibLinearMTL.h:122
class to implement LibLinear
Definition: LibLinearMTL.h:90
void set_task_similarity_matrix(SGSparseMatrix< float64_t > tsm)
Definition: LibLinearMTL.h:212
SGMatrix< float64_t > get_V()
Definition: LibLinearMTL.h:227
int32_t index_t
Definition: common.h:60
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:35
virtual int32_t get_num_labels() const =0
virtual float64_t compute_dual_obj()
virtual float64_t compute_primal_obj()
virtual EMachineType get_classifier_type()
Definition: LibLinearMTL.h:115
CLabels * m_labels
Definition: Machine.h:356
SGSparseVector< T > * sparse_matrix
array of sparse vectors of size num_vectors
#define SG_ERROR(...)
Definition: SGIO.h:131
float64_t get_epsilon()
Definition: LibLinearMTL.h:146
SGMatrix< float64_t > V
Definition: LibLinearMTL.h:346
index_t num_vectors
total number of vectors
SGVector< float64_t > get_alphas()
Definition: LibLinearMTL.h:267
Features that support dot products among other operations.
Definition: DotFeatures.h:41
int32_t get_max_iterations()
Definition: LibLinearMTL.h:164
mapped sparse matrix for representing graph relations of tasks
Definition: LibLinearMTL.h:35
std::vector< std::map< index_t, float64_t > > data
Definition: LibLinearMTL.h:84
void set_linear_term(SGVector< float64_t > linear_term)
Definition: LibLinearMTL.h:182
double float64_t
Definition: common.h:48
SGVector< float64_t > m_linear_term
Definition: LibLinearMTL.h:323
index_t num_rows
Definition: SGMatrix.h:301
virtual bool train_machine(CFeatures *data=NULL)
void set_graph_laplacian(SGMatrix< float64_t > lap)
Definition: LibLinearMTL.h:218
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:61
virtual float64_t compute_duality_gap()
void set_task_indicator_rhs(SGVector< int32_t > ti)
Definition: LibLinearMTL.h:206
SGVector< int32_t > task_indicator_rhs
Definition: LibLinearMTL.h:335
SGMatrix< float64_t > get_W()
Definition: LibLinearMTL.h:236
template class SGSparseVectorEntry
Definition: File.h:23
The class Features is the base class of all feature objects.
Definition: Features.h:62
SGVector< int32_t > task_indicator_lhs
Definition: LibLinearMTL.h:332
MappedSparseMatrix task_similarity_matrix
Definition: LibLinearMTL.h:340
void set_num_tasks(int32_t nt)
Definition: LibLinearMTL.h:176
SGMatrix< float64_t > graph_laplacian
Definition: LibLinearMTL.h:343
void set_max_iterations(int32_t max_iter=1000)
Definition: LibLinearMTL.h:170
virtual const char * get_name() const
Definition: LibLinearMTL.h:161
void set_from_sparse(const SGSparseMatrix< float64_t > &sgm)
Definition: LibLinearMTL.h:62
T * get_column_vector(index_t col) const
Definition: SGMatrix.h:52
index_t vlen
Definition: SGVector.h:706
void set_epsilon(float64_t eps)
Definition: LibLinearMTL.h:140
SGVector< float64_t > alphas
Definition: LibLinearMTL.h:326
SGSparseVectorEntry< T > * features

SHOGUN Machine Learning Toolbox - Documentation