SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
MultitaskLeastSquaresRegression.cpp
浏览该文件的文档.
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  * Copyright (C) 2012 Sergey Lisitsyn
8  */
9 
15 
16 namespace shogun
17 {
18 
21 {
22  initialize_parameters();
23  register_parameters();
24 }
25 
27  float64_t z, CDotFeatures* train_features,
28  CRegressionLabels* train_labels, CTaskRelation* task_relation) :
29  CMultitaskLinearMachine(train_features,(CLabels*)train_labels,task_relation)
30 {
31  set_z(z);
32  initialize_parameters();
33  register_parameters();
34 }
35 
37 {
38 }
39 
40 void CMultitaskLeastSquaresRegression::register_parameters()
41 {
42  SG_ADD(&m_z, "z", "regularization coefficient", MS_AVAILABLE);
43  SG_ADD(&m_q, "q", "q of L1/Lq", MS_AVAILABLE);
44  SG_ADD(&m_termination, "termination", "termination", MS_NOT_AVAILABLE);
45  SG_ADD(&m_regularization, "regularization", "regularization", MS_NOT_AVAILABLE);
46  SG_ADD(&m_tolerance, "tolerance", "tolerance", MS_NOT_AVAILABLE);
47  SG_ADD(&m_max_iter, "max_iter", "maximum number of iterations", MS_NOT_AVAILABLE);
48 }
49 
50 void CMultitaskLeastSquaresRegression::initialize_parameters()
51 {
52  set_z(0.0);
53  set_q(2.0);
54  set_termination(0);
56  set_tolerance(1e-3);
57  set_max_iter(1000);
58 }
59 
61 {
63  return false;
64 }
65 
67 {
69  return dot + m_tasks_c[m_current_task];
70 }
71 
73 {
74  return m_max_iter;
75 }
77 {
78  return m_regularization;
79 }
81 {
82  return m_termination;
83 }
85 {
86  return m_tolerance;
87 }
89 {
90  return m_z;
91 }
93 {
94  return m_q;
95 }
96 
98 {
99  ASSERT(max_iter>=0)
100  m_max_iter = max_iter;
101 }
103 {
104  ASSERT(regularization==0 || regularization==1)
105  m_regularization = regularization;
106 }
108 {
109  ASSERT(termination>=0 && termination<=4)
110  m_termination = termination;
111 }
113 {
114  ASSERT(tolerance>0.0)
115  m_tolerance = tolerance;
116 }
118 {
119  m_z = z;
120 }
122 {
123  m_q = q;
124 }
125 
127 {
128  if (data && (CDotFeatures*)data)
129  set_features((CDotFeatures*)data);
130 
133 
134  SGVector<float64_t> y = ((CRegressionLabels*)m_labels)->get_labels();
135 
136  slep_options options = slep_options::default_options();
137  options.n_tasks = m_task_relation->get_num_tasks();
138  options.tasks_indices = m_task_relation->get_tasks_indices();
139  options.q = m_q;
140  options.regularization = m_regularization;
141  options.termination = m_termination;
142  options.tolerance = m_tolerance;
143  options.max_iter = m_max_iter;
144 
145  ETaskRelationType relation_type = m_task_relation->get_relation_type();
146  switch (relation_type)
147  {
148  case TASK_GROUP:
149  {
150  //CTaskGroup* task_group = (CTaskGroup*)m_task_relation;
151  options.mode = MULTITASK_GROUP;
152  options.loss = LEAST_SQUARES;
153  m_tasks_w = slep_solver(features, y.vector, m_z, options).w;
154  m_tasks_c = SGVector<float64_t>(options.n_tasks);
155  m_tasks_c.zero();
156  }
157  break;
158  case TASK_TREE:
159  {
160  CTaskTree* task_tree = (CTaskTree*)m_task_relation;
161  SGVector<float64_t> ind_t = task_tree->get_SLEP_ind_t();
162  options.ind_t = ind_t.vector;
163  options.n_nodes = ind_t.vlen/3;
164  options.mode = MULTITASK_TREE;
165  options.loss = LEAST_SQUARES;
166  m_tasks_w = slep_solver(features, y.vector, m_z, options).w;
167  m_tasks_c = SGVector<float64_t>(options.n_tasks);
168  m_tasks_c.zero();
169  }
170  break;
171  default:
172  SG_ERROR("Not supported task relation type\n")
173  }
174 
175  SG_FREE(options.tasks_indices);
176 
177  return true;
178 }
179 
180 }
Real Labels are real-valued labels.
Vector::Scalar dot(Vector a, Vector b)
Definition: Redux.h:56
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
virtual float64_t dense_dot(int32_t vec_idx1, const float64_t *vec2, int32_t vec2_len)=0
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:139
Features that support dot products among other operations.
Definition: DotFeatures.h:44
index_t num_rows
Definition: SGMatrix.h:376
index_t vlen
Definition: SGVector.h:494
SGVector< float64_t > get_SLEP_ind_t()
Definition: TaskTree.cpp:173
#define ASSERT(x)
Definition: SGIO.h:201
virtual ETaskRelationType get_relation_type() const =0
double float64_t
Definition: common.h:50
virtual void set_features(CDotFeatures *feat)
T * get_column_vector(index_t col) const
Definition: SGMatrix.h:115
virtual int32_t get_num_tasks() const =0
slep_result_t slep_solver(CDotFeatures *features, double *y, double z, const slep_options &options)
class MultitaskLinearMachine, a base class for linear multitask classifiers
CDotFeatures * features
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The class Features is the base class of all feature objects.
Definition: Features.h:68
class TaskTree used to represent a tree of tasks. Tree is constructed via task with subtasks (and sub...
Definition: TaskTree.h:27
used to represent tasks in multitask learning
Definition: TaskRelation.h:31
#define SG_ADD(...)
Definition: SGObject.h:81
virtual bool train_locked_implementation(SGVector< index_t > *tasks)
virtual SGVector< index_t > * get_tasks_indices() const =0

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