SHOGUN  v2.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 
19 namespace shogun
20 {
21 
22 
34 {
35 
36 public:
37 
41  scale(1.0), normalization_constant(1.0)
42  {
43  }
44 
51  CMultitaskKernelMaskNormalizer(std::vector<int32_t> task_lhs,
52  std::vector<int32_t> task_rhs,
53  std::vector<int32_t> active_tasks_vec)
55  {
56 
57 
58  set_task_vector_lhs(task_lhs);
59  set_task_vector_rhs(task_rhs);
60 
61  // set active tasks
62  for (int32_t i = 0; i != (int32_t)(active_tasks_vec.size()); ++i)
63  {
64  active_tasks.insert(active_tasks_vec[i]);
65  }
66 
67  }
68 
69 
72  {
73  }
74 
77  virtual bool init(CKernel* k)
78  {
79  ASSERT(k);
80  int32_t num_lhs = k->get_num_vec_lhs();
81  int32_t num_rhs = k->get_num_vec_rhs();
82  ASSERT(num_lhs>0);
83  ASSERT(num_rhs>0);
84 
85 
86  //same as first-element normalizer
87  CFeatures* old_lhs=k->lhs;
88  CFeatures* old_rhs=k->rhs;
89  k->lhs=old_lhs;
90  k->rhs=old_lhs;
91 
92  if (std::string(k->get_name()) == "WeightedDegree") {
93  SG_INFO("using first-element normalization\n");
94  scale=k->compute(0, 0);
95  } else {
96  SG_INFO("no inner normalization for non-WDK kernel\n");
97  scale=1.0;
98  }
99 
100  k->lhs=old_lhs;
101  k->rhs=old_rhs;
102 
103 
104  return true;
105  }
106 
107 
108 
114  inline virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
115  {
116 
117  //lookup tasks
118  int32_t task_idx_lhs = task_vector_lhs[idx_lhs];
119  int32_t task_idx_rhs = task_vector_rhs[idx_rhs];
120 
121  //lookup similarity
122  float64_t task_similarity = get_similarity(task_idx_lhs, task_idx_rhs);
123 
124  //take task similarity into account
125  float64_t similarity = (value/scale) * task_similarity;
126 
127 
128  return similarity;
129 
130  }
131 
136  inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
137  {
138  SG_ERROR("normalize_lhs not implemented");
139  return 0;
140  }
141 
146  inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
147  {
148  SG_ERROR("normalize_rhs not implemented");
149  return 0;
150  }
151 
153  std::vector<int32_t> get_task_vector_lhs() const
154  {
155  return task_vector_lhs;
156  }
157 
158 
160  void set_task_vector_lhs(std::vector<int32_t> vec)
161  {
162 
163  task_vector_lhs.clear();
164 
165  for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
166  {
167  task_vector_lhs.push_back(vec[i]);
168  }
169 
170  }
171 
174  std::vector<int32_t> get_task_vector_rhs() const
175  {
176  return task_vector_rhs;
177  }
178 
179 
181  void set_task_vector_rhs(std::vector<int32_t> vec)
182  {
183 
184  task_vector_rhs.clear();
185 
186  for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
187  {
188  task_vector_rhs.push_back(vec[i]);
189  }
190 
191  }
192 
194  void set_task_vector(std::vector<int32_t> vec)
195  {
196  set_task_vector_lhs(vec);
197  set_task_vector_rhs(vec);
198  }
199 
200 
206  float64_t get_similarity(int32_t task_lhs, int32_t task_rhs)
207  {
208 
209  const bool lhs_is_in = active_tasks.find(task_lhs) != active_tasks.end();
210  const bool rhs_is_in = active_tasks.find(task_rhs) != active_tasks.end();
211 
212  float64_t similarity = 0.0;
213 
214  if (lhs_is_in && rhs_is_in)
215  {
216  similarity = 1.0 / normalization_constant;
217  }
218 
219  return similarity;
220 
221  }
222 
226  std::vector<int32_t> get_active_tasks()
227  {
228 
229  std::vector<int32_t> active_tasks_vec;
230 
231  // set active tasks
232  for (std::set<int32_t>::const_iterator it=active_tasks.begin(); it!=active_tasks.end(); it++)
233  {
234  active_tasks_vec.push_back(*it);
235  }
236 
237  return active_tasks_vec;
238  }
239 
244  {
245  return normalization_constant;
246  }
247 
252  {
253  normalization_constant = constant;
254 
256  return 0.0;
257  }
258 
260  inline virtual const char* get_name() const
261  {
262  return "MultitaskKernelMaskNormalizer";
263  }
264 
269  {
270  return dynamic_cast<CMultitaskKernelMaskNormalizer*>(n);
271  }
272 
273 protected:
275  std::set<int32_t> active_tasks;
276 
278  std::vector<int32_t> task_vector_lhs;
279 
281  std::vector<int32_t> task_vector_rhs;
282 
285 
288 
289 };
290 }
291 #endif

SHOGUN Machine Learning Toolbox - Documentation