SHOGUN  v2.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 {
71  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  for (int32_t i=0; i<n_tasks; i++)
99  tasks_indices[i].~SGVector<index_t>();
100  SG_FREE(tasks_indices);
101 }
102 
104 {
105  int n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
106  ASSERT((int)m_tasks_indices.size()==n_tasks);
107  vector< vector<index_t> > cutted_task_indices;
108  for (int32_t i=0; i<n_tasks; i++)
109  cutted_task_indices.push_back(vector<index_t>());
110  for (int32_t i=0; i<indices.vlen; i++)
111  {
112  for (int32_t j=0; j<n_tasks; j++)
113  {
114  if (m_tasks_indices[j].count(indices[i]))
115  {
116  cutted_task_indices[j].push_back(indices[i]);
117  break;
118  }
119  }
120  }
121  SGVector<index_t>* tasks = SG_MALLOC(SGVector<index_t>, n_tasks);
122  for (int32_t i=0; i<n_tasks; i++)
123  {
124  new (&tasks[i]) SGVector<index_t>(cutted_task_indices[i].size());
125  for (int32_t j=0; j<(int)cutted_task_indices[i].size(); j++)
126  tasks[i][j] = cutted_task_indices[i][j];
127  //tasks[i].display_vector();
128  }
129  bool res = train_locked_implementation(tasks);
130  for (int32_t i=0; i<n_tasks; i++)
131  tasks[i].~SGVector<index_t>();
132  SG_FREE(tasks);
133  return res;
134 }
135 
137 {
139  return false;
140 }
141 
143 {
144  int n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
145  SGVector<float64_t> result(indices.vlen);
146  result.zero();
147  for (int32_t i=0; i<indices.vlen; i++)
148  {
149  for (int32_t j=0; j<n_tasks; j++)
150  {
151  if (m_tasks_indices[j].count(indices[i]))
152  {
153  set_current_task(j);
154  result[i] = apply_one(indices[i]);
155  break;
156  }
157  }
158  }
159  return new CBinaryLabels(result);
160 }
161 
163 {
165  return 0.0;
166 }
167 
169 {
170  if (data)
171  {
172  if (!data->has_property(FP_DOT))
173  SG_ERROR("Specified features are not of type CDotFeatures\n");
174 
175  set_features((CDotFeatures*) data);
176  }
177 
178  if (!features)
179  return SGVector<float64_t>();
180 
181  int32_t num=features->get_num_vectors();
182  ASSERT(num>0);
183  float64_t* out=SG_MALLOC(float64_t, num);
184  for (int32_t i=0; i<num; i++)
185  out[i] = apply_one(i);
186 
187  return SGVector<float64_t>(out,num);
188 }
189 
191 {
193  for (int32_t i=0; i<w_.vlen; i++)
194  w_[i] = m_tasks_w(i,m_current_task);
195  return w_;
196 }
197 
199 {
200  for (int32_t i=0; i<m_tasks_w.num_rows; i++)
201  m_tasks_w(i,m_current_task) = src_w[i];
202 }
203 
205 {
207 }
208 
210 {
211  return m_tasks_c[m_current_task];
212 }
213 
215 {
216  int n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
217  SGVector<index_t>* tasks_indices = ((CTaskGroup*)m_task_relation)->get_tasks_indices();
218 
220  map<index_t,index_t> subset_inv_map = map<index_t,index_t>();
221  for (int32_t i=0; i<sstack->get_size(); i++)
222  subset_inv_map[sstack->subset_idx_conversion(i)] = i;
223 
224  SGVector<index_t>* subset_tasks_indices = SG_MALLOC(SGVector<index_t>, n_tasks);
225  for (int32_t i=0; i<n_tasks; i++)
226  {
227  new (&subset_tasks_indices[i]) SGVector<index_t>();
228  SGVector<index_t> task = tasks_indices[i];
229  //task.display_vector("task");
230  vector<index_t> cutted = vector<index_t>();
231  for (int32_t j=0; j<task.vlen; j++)
232  {
233  if (subset_inv_map.count(task[j]))
234  cutted.push_back(subset_inv_map[task[j]]);
235  }
236  SGVector<index_t> cutted_task(cutted.size());
237  for (int32_t j=0; j<cutted_task.vlen; j++)
238  cutted_task[j] = cutted[j];
239  //cutted_task.display_vector("cutted");
240  subset_tasks_indices[i] = cutted_task;
241  }
242  for (int32_t i=0; i<n_tasks; i++)
243  tasks_indices[i].~SGVector<index_t>();
244  SG_FREE(tasks_indices);
245 
246  return subset_tasks_indices;
247 }
248 
249 
250 }

SHOGUN Machine Learning Toolbox - Documentation