SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MultitaskKernelMaskNormalizer.h
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 2 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2010 Christian Widmer
8  * Copyright (C) 2010 Max-Planck-Society
9  */
10 
11 #ifndef _MULTITASKKERNELMASKNORMALIZER_H___
12 #define _MULTITASKKERNELMASKNORMALIZER_H___
13 
15 #include <shogun/kernel/Kernel.h>
16 #include <set>
17 #include <string>
18 #include <vector>
19 
20 namespace shogun
21 {
22 
23 
35 {
36 
37 public:
38 
42  scale(1.0), normalization_constant(1.0)
43  {
44  }
45 
52  CMultitaskKernelMaskNormalizer(std::vector<int32_t> task_lhs,
53  std::vector<int32_t> task_rhs,
54  std::vector<int32_t> active_tasks_vec)
56  {
57 
58 
59  set_task_vector_lhs(task_lhs);
60  set_task_vector_rhs(task_rhs);
61 
62  // set active tasks
63  for (int32_t i = 0; i != (int32_t)(active_tasks_vec.size()); ++i)
64  {
65  active_tasks.insert(active_tasks_vec[i]);
66  }
67 
68  }
69 
70 
73  {
74  }
75 
78  virtual bool init(CKernel* k)
79  {
80  ASSERT(k)
81  int32_t num_lhs = k->get_num_vec_lhs();
82  int32_t num_rhs = k->get_num_vec_rhs();
83  ASSERT(num_lhs>0)
84  ASSERT(num_rhs>0)
85 
86 
87  //same as first-element normalizer
88  CFeatures* old_lhs=k->lhs;
89  CFeatures* old_rhs=k->rhs;
90  k->lhs=old_lhs;
91  k->rhs=old_lhs;
92 
93  if (std::string(k->get_name()) == "WeightedDegree") {
94  SG_INFO("using first-element normalization\n")
95  scale=k->compute(0, 0);
96  } else {
97  SG_INFO("no inner normalization for non-WDK kernel\n")
98  scale=1.0;
99  }
100 
101  k->lhs=old_lhs;
102  k->rhs=old_rhs;
103 
104 
105  return true;
106  }
107 
108 
109 
115  virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
116  {
117 
118  //lookup tasks
119  int32_t task_idx_lhs = task_vector_lhs[idx_lhs];
120  int32_t task_idx_rhs = task_vector_rhs[idx_rhs];
121 
122  //lookup similarity
123  float64_t task_similarity = get_similarity(task_idx_lhs, task_idx_rhs);
124 
125  //take task similarity into account
126  float64_t similarity = (value/scale) * task_similarity;
127 
128 
129  return similarity;
130 
131  }
132 
137  virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
138  {
139  SG_ERROR("normalize_lhs not implemented")
140  return 0;
141  }
142 
147  virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
148  {
149  SG_ERROR("normalize_rhs not implemented")
150  return 0;
151  }
152 
154  std::vector<int32_t> get_task_vector_lhs() const
155  {
156  return task_vector_lhs;
157  }
158 
159 
161  void set_task_vector_lhs(std::vector<int32_t> vec)
162  {
163 
164  task_vector_lhs.clear();
165 
166  for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
167  {
168  task_vector_lhs.push_back(vec[i]);
169  }
170 
171  }
172 
175  std::vector<int32_t> get_task_vector_rhs() const
176  {
177  return task_vector_rhs;
178  }
179 
180 
182  void set_task_vector_rhs(std::vector<int32_t> vec)
183  {
184 
185  task_vector_rhs.clear();
186 
187  for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
188  {
189  task_vector_rhs.push_back(vec[i]);
190  }
191 
192  }
193 
195  void set_task_vector(std::vector<int32_t> vec)
196  {
197  set_task_vector_lhs(vec);
198  set_task_vector_rhs(vec);
199  }
200 
201 
207  float64_t get_similarity(int32_t task_lhs, int32_t task_rhs)
208  {
209 
210  const bool lhs_is_in = active_tasks.find(task_lhs) != active_tasks.end();
211  const bool rhs_is_in = active_tasks.find(task_rhs) != active_tasks.end();
212 
213  float64_t similarity = 0.0;
214 
215  if (lhs_is_in && rhs_is_in)
216  {
217  similarity = 1.0 / normalization_constant;
218  }
219 
220  return similarity;
221 
222  }
223 
227  std::vector<int32_t> get_active_tasks()
228  {
229 
230  std::vector<int32_t> active_tasks_vec;
231 
232  // set active tasks
233  for (std::set<int32_t>::const_iterator it=active_tasks.begin(); it!=active_tasks.end(); it++)
234  {
235  active_tasks_vec.push_back(*it);
236  }
237 
238  return active_tasks_vec;
239  }
240 
245  {
246  return normalization_constant;
247  }
248 
253  {
254  normalization_constant = constant;
255 
257  return 0.0;
258  }
259 
261  virtual const char* get_name() const
262  {
263  return "MultitaskKernelMaskNormalizer";
264  }
265 
270  {
271  return dynamic_cast<CMultitaskKernelMaskNormalizer*>(n);
272  }
273 
274 protected:
276  std::set<int32_t> active_tasks;
277 
279  std::vector<int32_t> task_vector_lhs;
280 
282  std::vector<int32_t> task_vector_rhs;
283 
286 
289 
290 };
291 }
292 #endif

SHOGUN Machine Learning Toolbox - Documentation