SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
10 
12 #ifdef USE_GPL_SHOGUN
17 
18 namespace shogun
19 {
20 
21 CMultitaskLeastSquaresRegression::CMultitaskLeastSquaresRegression() :
22  CMultitaskLinearMachine()
23 {
24  initialize_parameters();
25  register_parameters();
26 }
27 
28 CMultitaskLeastSquaresRegression::CMultitaskLeastSquaresRegression(
29  float64_t z, CDotFeatures* train_features,
30  CRegressionLabels* train_labels, CTaskRelation* task_relation) :
31  CMultitaskLinearMachine(train_features,(CLabels*)train_labels,task_relation)
32 {
33  set_z(z);
34  initialize_parameters();
35  register_parameters();
36 }
37 
38 CMultitaskLeastSquaresRegression::~CMultitaskLeastSquaresRegression()
39 {
40 }
41 
42 void CMultitaskLeastSquaresRegression::register_parameters()
43 {
44  SG_ADD(&m_z, "z", "regularization coefficient", MS_AVAILABLE);
45  SG_ADD(&m_q, "q", "q of L1/Lq", MS_AVAILABLE);
46  SG_ADD(&m_termination, "termination", "termination", MS_NOT_AVAILABLE);
47  SG_ADD(&m_regularization, "regularization", "regularization", MS_NOT_AVAILABLE);
48  SG_ADD(&m_tolerance, "tolerance", "tolerance", MS_NOT_AVAILABLE);
49  SG_ADD(&m_max_iter, "max_iter", "maximum number of iterations", MS_NOT_AVAILABLE);
50 }
51 
52 void CMultitaskLeastSquaresRegression::initialize_parameters()
53 {
54  set_z(0.0);
55  set_q(2.0);
56  set_termination(0);
57  set_regularization(0);
58  set_tolerance(1e-3);
59  set_max_iter(1000);
60 }
61 
62 bool CMultitaskLeastSquaresRegression::train_locked_implementation(SGVector<index_t>* tasks)
63 {
65  return false;
66 }
67 
68 float64_t CMultitaskLeastSquaresRegression::apply_one(int32_t i)
69 {
70  float64_t dot = features->dense_dot(i,m_tasks_w.get_column_vector(m_current_task),m_tasks_w.num_rows);
71  return dot + m_tasks_c[m_current_task];
72 }
73 
74 int32_t CMultitaskLeastSquaresRegression::get_max_iter() const
75 {
76  return m_max_iter;
77 }
78 int32_t CMultitaskLeastSquaresRegression::get_regularization() const
79 {
80  return m_regularization;
81 }
82 int32_t CMultitaskLeastSquaresRegression::get_termination() const
83 {
84  return m_termination;
85 }
86 float64_t CMultitaskLeastSquaresRegression::get_tolerance() const
87 {
88  return m_tolerance;
89 }
90 float64_t CMultitaskLeastSquaresRegression::get_z() const
91 {
92  return m_z;
93 }
94 float64_t CMultitaskLeastSquaresRegression::get_q() const
95 {
96  return m_q;
97 }
98 
99 void CMultitaskLeastSquaresRegression::set_max_iter(int32_t max_iter)
100 {
101  ASSERT(max_iter>=0)
102  m_max_iter = max_iter;
103 }
104 void CMultitaskLeastSquaresRegression::set_regularization(int32_t regularization)
105 {
106  ASSERT(regularization==0 || regularization==1)
107  m_regularization = regularization;
108 }
109 void CMultitaskLeastSquaresRegression::set_termination(int32_t termination)
110 {
111  ASSERT(termination>=0 && termination<=4)
112  m_termination = termination;
113 }
114 void CMultitaskLeastSquaresRegression::set_tolerance(float64_t tolerance)
115 {
116  ASSERT(tolerance>0.0)
117  m_tolerance = tolerance;
118 }
119 void CMultitaskLeastSquaresRegression::set_z(float64_t z)
120 {
121  m_z = z;
122 }
123 void CMultitaskLeastSquaresRegression::set_q(float64_t q)
124 {
125  m_q = q;
126 }
127 
128 bool CMultitaskLeastSquaresRegression::train_machine(CFeatures* data)
129 {
130  if (data && (CDotFeatures*)data)
131  set_features((CDotFeatures*)data);
132 
133  ASSERT(features)
134  ASSERT(m_labels)
135 
136  SGVector<float64_t> y = ((CRegressionLabels*)m_labels)->get_labels();
137 
138  slep_options options = slep_options::default_options();
139  options.n_tasks = m_task_relation->get_num_tasks();
140  options.tasks_indices = m_task_relation->get_tasks_indices();
141  options.q = m_q;
142  options.regularization = m_regularization;
143  options.termination = m_termination;
144  options.tolerance = m_tolerance;
145  options.max_iter = m_max_iter;
146 
147  ETaskRelationType relation_type = m_task_relation->get_relation_type();
148  switch (relation_type)
149  {
150  case TASK_GROUP:
151  {
152  //CTaskGroup* task_group = (CTaskGroup*)m_task_relation;
153  options.mode = MULTITASK_GROUP;
154  options.loss = LEAST_SQUARES;
155  m_tasks_w = slep_solver(features, y.vector, m_z, options).w;
156  m_tasks_c = SGVector<float64_t>(options.n_tasks);
157  m_tasks_c.zero();
158  }
159  break;
160  case TASK_TREE:
161  {
162  CTaskTree* task_tree = (CTaskTree*)m_task_relation;
163  SGVector<float64_t> ind_t = task_tree->get_SLEP_ind_t();
164  options.ind_t = ind_t.vector;
165  options.n_nodes = ind_t.vlen/3;
166  options.mode = MULTITASK_TREE;
167  options.loss = LEAST_SQUARES;
168  m_tasks_w = slep_solver(features, y.vector, m_z, options).w;
169  m_tasks_c = SGVector<float64_t>(options.n_tasks);
170  m_tasks_c.zero();
171  }
172  break;
173  default:
174  SG_ERROR("Not supported task relation type\n")
175  }
176 
177  SG_FREE(options.tasks_indices);
178 
179  return true;
180 }
181 
182 }
183 
184 #endif //USE_GPL_SHOGUN
Vector::Scalar dot(Vector a, Vector b)
Definition: Redux.h:58
#define SG_ERROR(...)
Definition: SGIO.h:129
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:139
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
#define SG_ADD(...)
Definition: SGObject.h:81

SHOGUN Machine Learning Toolbox - Documentation