SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MultitaskLeastSquaresRegression.cpp
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  * 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 
131  ASSERT(features);
132  ASSERT(m_labels);
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  for (int32_t i=0; i<options.n_tasks; i++)
176  options.tasks_indices[i].~SGVector<index_t>();
177  SG_FREE(options.tasks_indices);
178 
179  return true;
180 }
181 
182 }

SHOGUN Machine Learning Toolbox - Documentation