SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MultitaskLinearMachine.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 
13 
14 #include <map>
15 #include <vector>
16 
17 using namespace std;
18 
19 namespace shogun
20 {
21 
22 CMultitaskLinearMachine::CMultitaskLinearMachine() :
23  CLinearMachine(), m_current_task(0),
24  m_task_relation(NULL)
25 {
26  register_parameters();
27 }
28 
30  CDotFeatures* train_features,
31  CLabels* train_labels, CTaskRelation* task_relation) :
32  CLinearMachine(), m_current_task(0), m_task_relation(NULL)
33 {
34  set_features(train_features);
35  set_labels(train_labels);
36  set_task_relation(task_relation);
37  register_parameters();
38 }
39 
41 {
43 }
44 
45 void CMultitaskLinearMachine::register_parameters()
46 {
47  SG_ADD((CSGObject**)&m_task_relation, "task_relation", "task relation", MS_NOT_AVAILABLE);
48 }
49 
51 {
52  return m_current_task;
53 }
54 
56 {
57  ASSERT(task>=0)
59  m_current_task = task;
60 }
61 
63 {
65  return m_task_relation;
66 }
67 
69 {
70  SG_REF(task_relation);
72  m_task_relation = task_relation;
73 }
74 
76 {
78  return false;
79 }
80 
82 {
83  set_features((CDotFeatures*)features_);
84  int n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
85  SGVector<index_t>* tasks_indices = ((CTaskGroup*)m_task_relation)->get_tasks_indices();
86 
87  m_tasks_indices.clear();
88  for (int32_t i=0; i<n_tasks; i++)
89  {
90  set<index_t> indices_set;
91  SGVector<index_t> task_indices = tasks_indices[i];
92  for (int32_t j=0; j<task_indices.vlen; j++)
93  indices_set.insert(task_indices[j]);
94 
95  m_tasks_indices.push_back(indices_set);
96  }
97 
98  SG_FREE(tasks_indices);
99 }
100 
102 {
103  int n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
104  ASSERT((int)m_tasks_indices.size()==n_tasks)
105  vector< vector<index_t> > cutted_task_indices;
106  for (int32_t i=0; i<n_tasks; i++)
107  cutted_task_indices.push_back(vector<index_t>());
108  for (int32_t i=0; i<indices.vlen; i++)
109  {
110  for (int32_t j=0; j<n_tasks; j++)
111  {
112  if (m_tasks_indices[j].count(indices[i]))
113  {
114  cutted_task_indices[j].push_back(indices[i]);
115  break;
116  }
117  }
118  }
119  SGVector<index_t>* tasks = SG_MALLOC(SGVector<index_t>, n_tasks);
120  for (int32_t i=0; i<n_tasks; i++)
121  {
122  tasks[i]=SGVector<index_t>(cutted_task_indices[i].size());
123  for (int32_t j=0; j<(int)cutted_task_indices[i].size(); j++)
124  tasks[i][j] = cutted_task_indices[i][j];
125  //tasks[i].display_vector();
126  }
127  bool res = train_locked_implementation(tasks);
128  SG_FREE(tasks);
129  return res;
130 }
131 
133 {
135  return false;
136 }
137 
139 {
140  int n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
141  SGVector<float64_t> result(indices.vlen);
142  result.zero();
143  for (int32_t i=0; i<indices.vlen; i++)
144  {
145  for (int32_t j=0; j<n_tasks; j++)
146  {
147  if (m_tasks_indices[j].count(indices[i]))
148  {
149  set_current_task(j);
150  result[i] = apply_one(indices[i]);
151  break;
152  }
153  }
154  }
155  return new CBinaryLabels(result);
156 }
157 
159 {
161  return 0.0;
162 }
163 
165 {
166  if (data)
167  {
168  if (!data->has_property(FP_DOT))
169  SG_ERROR("Specified features are not of type CDotFeatures\n")
170 
171  set_features((CDotFeatures*) data);
172  }
173 
174  if (!features)
175  return SGVector<float64_t>();
176 
177  int32_t num=features->get_num_vectors();
178  ASSERT(num>0)
179  float64_t* out=SG_MALLOC(float64_t, num);
180  for (int32_t i=0; i<num; i++)
181  out[i] = apply_one(i);
182 
183  return SGVector<float64_t>(out,num);
184 }
185 
187 {
189  for (int32_t i=0; i<w_.vlen; i++)
190  w_[i] = m_tasks_w(i,m_current_task);
191  return w_;
192 }
193 
195 {
196  for (int32_t i=0; i<m_tasks_w.num_rows; i++)
197  m_tasks_w(i,m_current_task) = src_w[i];
198 }
199 
201 {
203 }
204 
206 {
207  return m_tasks_c[m_current_task];
208 }
209 
211 {
212  int n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
213  SGVector<index_t>* tasks_indices = ((CTaskGroup*)m_task_relation)->get_tasks_indices();
214 
216  map<index_t,index_t> subset_inv_map = map<index_t,index_t>();
217  for (int32_t i=0; i<sstack->get_size(); i++)
218  subset_inv_map[sstack->subset_idx_conversion(i)] = i;
219 
220  SGVector<index_t>* subset_tasks_indices = SG_MALLOC(SGVector<index_t>, n_tasks);
221  for (int32_t i=0; i<n_tasks; i++)
222  {
223  SGVector<index_t> task = tasks_indices[i];
224  //task.display_vector("task");
225  vector<index_t> cutted = vector<index_t>();
226  for (int32_t j=0; j<task.vlen; j++)
227  {
228  if (subset_inv_map.count(task[j]))
229  cutted.push_back(subset_inv_map[task[j]]);
230  }
231  SGVector<index_t> cutted_task(cutted.size());
232  for (int32_t j=0; j<cutted_task.vlen; j++)
233  cutted_task[j] = cutted[j];
234  //cutted_task.display_vector("cutted");
235  subset_tasks_indices[i] = cutted_task;
236  }
237  SG_FREE(tasks_indices);
238 
239  return subset_tasks_indices;
240 }
241 
242 
243 }

SHOGUN Machine Learning Toolbox - Documentation