SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
LibLinearMTL.h
浏览该文件的文档.
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___
void set_bias_enabled(bool enable_bias)
Definition: LibLinearMTL.h:153
const float64_t operator()(index_t i_row, index_t i_col) const
Definition: LibLinearMTL.h:45
void set_task_indicator_lhs(SGVector< int32_t > ti)
Definition: LibLinearMTL.h:201
EMachineType
Definition: Machine.h:33
void set_C(float64_t c_neg, float64_t c_pos)
Definition: LibLinearMTL.h:123
class to implement LibLinear
Definition: LibLinearMTL.h:91
void set_task_similarity_matrix(SGSparseMatrix< float64_t > tsm)
Definition: LibLinearMTL.h:213
SGMatrix< float64_t > get_V()
Definition: LibLinearMTL.h:228
int32_t index_t
Definition: common.h:62
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
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:116
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
float64_t get_epsilon()
Definition: LibLinearMTL.h:147
SGMatrix< float64_t > V
Definition: LibLinearMTL.h:347
SGVector< float64_t > get_alphas()
Definition: LibLinearMTL.h:268
Features that support dot products among other operations.
Definition: DotFeatures.h:44
index_t num_rows
Definition: SGMatrix.h:376
int32_t get_max_iterations()
Definition: LibLinearMTL.h:165
mapped sparse matrix for representing graph relations of tasks
Definition: LibLinearMTL.h:36
index_t vlen
Definition: SGVector.h:494
std::vector< std::map< index_t, float64_t > > data
Definition: LibLinearMTL.h:85
void set_linear_term(SGVector< float64_t > linear_term)
Definition: LibLinearMTL.h:183
double float64_t
Definition: common.h:50
SGVector< float64_t > m_linear_term
Definition: LibLinearMTL.h:324
virtual bool train_machine(CFeatures *data=NULL)
void set_graph_laplacian(SGMatrix< float64_t > lap)
Definition: LibLinearMTL.h:219
T * get_column_vector(index_t col) const
Definition: SGMatrix.h:115
SGSparseVector< T > * sparse_matrix
array of sparse vectors of size num_vectors
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:63
SGSparseVectorEntry< T > * features
virtual float64_t compute_duality_gap()
void set_task_indicator_rhs(SGVector< int32_t > ti)
Definition: LibLinearMTL.h:207
SGVector< int32_t > task_indicator_rhs
Definition: LibLinearMTL.h:336
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
SGMatrix< float64_t > get_W()
Definition: LibLinearMTL.h:237
The class Features is the base class of all feature objects.
Definition: Features.h:68
SGVector< int32_t > task_indicator_lhs
Definition: LibLinearMTL.h:333
MappedSparseMatrix task_similarity_matrix
Definition: LibLinearMTL.h:341
void set_num_tasks(int32_t nt)
Definition: LibLinearMTL.h:177
SGMatrix< float64_t > graph_laplacian
Definition: LibLinearMTL.h:344
void set_max_iterations(int32_t max_iter=1000)
Definition: LibLinearMTL.h:171
index_t num_vectors
total number of vectors
virtual const char * get_name() const
Definition: LibLinearMTL.h:162
void set_from_sparse(const SGSparseMatrix< float64_t > &sgm)
Definition: LibLinearMTL.h:63
void set_epsilon(float64_t eps)
Definition: LibLinearMTL.h:141
SGVector< float64_t > alphas
Definition: LibLinearMTL.h:327

SHOGUN 机器学习工具包 - 项目文档