SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
10 
12 #ifdef USE_GPL_SHOGUN
15 
16 #include <map>
17 #include <vector>
18 
19 using namespace std;
20 
21 namespace shogun
22 {
23 
24 CMultitaskLinearMachine::CMultitaskLinearMachine() :
25  CLinearMachine(), m_current_task(0),
26  m_task_relation(NULL)
27 {
28  register_parameters();
29 }
30 
31 CMultitaskLinearMachine::CMultitaskLinearMachine(
32  CDotFeatures* train_features,
33  CLabels* train_labels, CTaskRelation* task_relation) :
34  CLinearMachine(), m_current_task(0), m_task_relation(NULL)
35 {
36  set_features(train_features);
37  set_labels(train_labels);
38  set_task_relation(task_relation);
39  register_parameters();
40 }
41 
42 CMultitaskLinearMachine::~CMultitaskLinearMachine()
43 {
44  SG_UNREF(m_task_relation);
45 }
46 
47 void CMultitaskLinearMachine::register_parameters()
48 {
49  SG_ADD((CSGObject**)&m_task_relation, "task_relation", "task relation", MS_NOT_AVAILABLE);
50 }
51 
52 int32_t CMultitaskLinearMachine::get_current_task() const
53 {
54  return m_current_task;
55 }
56 
57 void CMultitaskLinearMachine::set_current_task(int32_t task)
58 {
59  ASSERT(task>=0)
60  ASSERT(task<m_tasks_w.num_cols)
61  m_current_task = task;
62 }
63 
64 CTaskRelation* CMultitaskLinearMachine::get_task_relation() const
65 {
66  SG_REF(m_task_relation);
67  return m_task_relation;
68 }
69 
70 void CMultitaskLinearMachine::set_task_relation(CTaskRelation* task_relation)
71 {
72  SG_REF(task_relation);
73  SG_UNREF(m_task_relation);
74  m_task_relation = task_relation;
75 }
76 
77 bool CMultitaskLinearMachine::train_machine(CFeatures* data)
78 {
80  return false;
81 }
82 
83 void CMultitaskLinearMachine::post_lock(CLabels* labels, CFeatures* features_)
84 {
85  set_features((CDotFeatures*)features_);
86  int n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
87  SGVector<index_t>* tasks_indices = ((CTaskGroup*)m_task_relation)->get_tasks_indices();
88 
89  m_tasks_indices.clear();
90  for (int32_t i=0; i<n_tasks; i++)
91  {
92  set<index_t> indices_set;
93  SGVector<index_t> task_indices = tasks_indices[i];
94  for (int32_t j=0; j<task_indices.vlen; j++)
95  indices_set.insert(task_indices[j]);
96 
97  m_tasks_indices.push_back(indices_set);
98  }
99 
100  SG_FREE(tasks_indices);
101 }
102 
103 bool CMultitaskLinearMachine::train_locked(SGVector<index_t> indices)
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  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  SG_FREE(tasks);
131  return res;
132 }
133 
134 bool CMultitaskLinearMachine::train_locked_implementation(SGVector<index_t>* tasks)
135 {
137  return false;
138 }
139 
140 CBinaryLabels* CMultitaskLinearMachine::apply_locked_binary(SGVector<index_t> indices)
141 {
142  int n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
143  SGVector<float64_t> result(indices.vlen);
144  result.zero();
145  for (int32_t i=0; i<indices.vlen; i++)
146  {
147  for (int32_t j=0; j<n_tasks; j++)
148  {
149  if (m_tasks_indices[j].count(indices[i]))
150  {
151  set_current_task(j);
152  result[i] = apply_one(indices[i]);
153  break;
154  }
155  }
156  }
157  return new CBinaryLabels(result);
158 }
159 
160 float64_t CMultitaskLinearMachine::apply_one(int32_t i)
161 {
163  return 0.0;
164 }
165 
166 SGVector<float64_t> CMultitaskLinearMachine::apply_get_outputs(CFeatures* data)
167 {
168  if (data)
169  {
170  if (!data->has_property(FP_DOT))
171  SG_ERROR("Specified features are not of type CDotFeatures\n")
172 
173  set_features((CDotFeatures*) data);
174  }
175 
176  if (!features)
177  return SGVector<float64_t>();
178 
179  int32_t num=features->get_num_vectors();
180  ASSERT(num>0)
181  float64_t* out=SG_MALLOC(float64_t, num);
182  for (int32_t i=0; i<num; i++)
183  out[i] = apply_one(i);
184 
185  return SGVector<float64_t>(out,num);
186 }
187 
188 SGVector<float64_t> CMultitaskLinearMachine::get_w() const
189 {
190  SGVector<float64_t> w_(m_tasks_w.num_rows);
191  for (int32_t i=0; i<w_.vlen; i++)
192  w_[i] = m_tasks_w(i,m_current_task);
193  return w_;
194 }
195 
196 void CMultitaskLinearMachine::set_w(const SGVector<float64_t> src_w)
197 {
198  for (int32_t i=0; i<m_tasks_w.num_rows; i++)
199  m_tasks_w(i,m_current_task) = src_w[i];
200 }
201 
202 void CMultitaskLinearMachine::set_bias(float64_t b)
203 {
204  m_tasks_c[m_current_task] = b;
205 }
206 
207 float64_t CMultitaskLinearMachine::get_bias()
208 {
209  return m_tasks_c[m_current_task];
210 }
211 
212 SGVector<index_t>* CMultitaskLinearMachine::get_subset_tasks_indices()
213 {
214  int n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
215  SGVector<index_t>* tasks_indices = ((CTaskGroup*)m_task_relation)->get_tasks_indices();
216 
217  CSubsetStack* sstack = features->get_subset_stack();
218  map<index_t,index_t> subset_inv_map = map<index_t,index_t>();
219  for (int32_t i=0; i<sstack->get_size(); i++)
220  subset_inv_map[sstack->subset_idx_conversion(i)] = i;
221 
222  SG_UNREF(sstack);
223  sstack=NULL;
224 
225  SGVector<index_t>* subset_tasks_indices = SG_MALLOC(SGVector<index_t>, n_tasks);
226  for (int32_t i=0; i<n_tasks; i++)
227  {
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  SG_FREE(tasks_indices);
243 
244  return subset_tasks_indices;
245 }
246 
247 
248 }
249 #endif //USE_GPL_SHOGUN
#define SG_ERROR(...)
Definition: SGIO.h:129
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:139
#define SG_REF(x)
Definition: SGObject.h:51
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
#define SG_UNREF(x)
Definition: SGObject.h:52
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