SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
14 #include <shogun/lib/config.h>
15 
17 #include <shogun/kernel/Kernel.h>
18 #include <set>
19 #include <string>
20 #include <vector>
21 
22 namespace shogun
23 {
24 
25 
37 {
38 
39 public:
40 
44  scale(1.0), normalization_constant(1.0)
45  {
46  }
47 
54  CMultitaskKernelMaskNormalizer(std::vector<int32_t> task_lhs,
55  std::vector<int32_t> task_rhs,
56  std::vector<int32_t> active_tasks_vec)
58  {
59 
60 
61  set_task_vector_lhs(task_lhs);
62  set_task_vector_rhs(task_rhs);
63 
64  // set active tasks
65  for (int32_t i = 0; i != (int32_t)(active_tasks_vec.size()); ++i)
66  {
67  active_tasks.insert(active_tasks_vec[i]);
68  }
69 
70  }
71 
72 
75  {
76  }
77 
80  virtual bool init(CKernel* k)
81  {
82  ASSERT(k)
83  int32_t num_lhs = k->get_num_vec_lhs();
84  int32_t num_rhs = k->get_num_vec_rhs();
85  ASSERT(num_lhs>0)
86  ASSERT(num_rhs>0)
87 
88 
89  //same as first-element normalizer
90  CFeatures* old_lhs=k->lhs;
91  CFeatures* old_rhs=k->rhs;
92  k->lhs=old_lhs;
93  k->rhs=old_lhs;
94 
95  if (std::string(k->get_name()) == "WeightedDegree") {
96  SG_INFO("using first-element normalization\n")
97  scale=k->compute(0, 0);
98  } else {
99  SG_INFO("no inner normalization for non-WDK kernel\n")
100  scale=1.0;
101  }
102 
103  k->lhs=old_lhs;
104  k->rhs=old_rhs;
105 
106 
107  return true;
108  }
109 
110 
111 
117  virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
118  {
119 
120  //lookup tasks
121  int32_t task_idx_lhs = task_vector_lhs[idx_lhs];
122  int32_t task_idx_rhs = task_vector_rhs[idx_rhs];
123 
124  //lookup similarity
125  float64_t task_similarity = get_similarity(task_idx_lhs, task_idx_rhs);
126 
127  //take task similarity into account
128  float64_t similarity = (value/scale) * task_similarity;
129 
130 
131  return similarity;
132 
133  }
134 
139  virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
140  {
141  SG_ERROR("normalize_lhs not implemented")
142  return 0;
143  }
144 
149  virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
150  {
151  SG_ERROR("normalize_rhs not implemented")
152  return 0;
153  }
154 
156  std::vector<int32_t> get_task_vector_lhs() const
157  {
158  return task_vector_lhs;
159  }
160 
161 
163  void set_task_vector_lhs(std::vector<int32_t> vec)
164  {
165 
166  task_vector_lhs.clear();
167 
168  for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
169  {
170  task_vector_lhs.push_back(vec[i]);
171  }
172 
173  }
174 
177  std::vector<int32_t> get_task_vector_rhs() const
178  {
179  return task_vector_rhs;
180  }
181 
182 
184  void set_task_vector_rhs(std::vector<int32_t> vec)
185  {
186 
187  task_vector_rhs.clear();
188 
189  for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
190  {
191  task_vector_rhs.push_back(vec[i]);
192  }
193 
194  }
195 
197  void set_task_vector(std::vector<int32_t> vec)
198  {
199  set_task_vector_lhs(vec);
200  set_task_vector_rhs(vec);
201  }
202 
203 
209  float64_t get_similarity(int32_t task_lhs, int32_t task_rhs)
210  {
211 
212  const bool lhs_is_in = active_tasks.find(task_lhs) != active_tasks.end();
213  const bool rhs_is_in = active_tasks.find(task_rhs) != active_tasks.end();
214 
215  float64_t similarity = 0.0;
216 
217  if (lhs_is_in && rhs_is_in)
218  {
219  similarity = 1.0 / normalization_constant;
220  }
221 
222  return similarity;
223 
224  }
225 
229  std::vector<int32_t> get_active_tasks()
230  {
231 
232  std::vector<int32_t> active_tasks_vec;
233 
234  // set active tasks
235  for (std::set<int32_t>::const_iterator it=active_tasks.begin(); it!=active_tasks.end(); it++)
236  {
237  active_tasks_vec.push_back(*it);
238  }
239 
240  return active_tasks_vec;
241  }
242 
247  {
248  return normalization_constant;
249  }
250 
255  {
256  normalization_constant = constant;
257 
259  return 0.0;
260  }
261 
263  virtual const char* get_name() const
264  {
265  return "MultitaskKernelMaskNormalizer";
266  }
267 
272  {
273  return dynamic_cast<CMultitaskKernelMaskNormalizer*>(n);
274  }
275 
276 protected:
278  std::set<int32_t> active_tasks;
279 
281  std::vector<int32_t> task_vector_lhs;
282 
284  std::vector<int32_t> task_vector_rhs;
285 
288 
291 
292 };
293 }
294 #endif
virtual const char * get_name() const =0
#define SG_INFO(...)
Definition: SGIO.h:118
virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
virtual float64_t compute(int32_t x, int32_t y)=0
The MultitaskKernel allows Multitask Learning via a modified kernel function.
void set_task_vector_lhs(std::vector< int32_t > vec)
void set_task_vector_rhs(std::vector< int32_t > vec)
#define SG_ERROR(...)
Definition: SGIO.h:129
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:139
CMultitaskKernelMaskNormalizer(std::vector< int32_t > task_lhs, std::vector< int32_t > task_rhs, std::vector< int32_t > active_tasks_vec)
float64_t get_similarity(int32_t task_lhs, int32_t task_rhs)
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:517
#define ASSERT(x)
Definition: SGIO.h:201
std::vector< int32_t > get_task_vector_rhs() const
double float64_t
Definition: common.h:50
std::vector< int32_t > get_task_vector_lhs() const
The class Kernel Normalizer defines a function to post-process kernel values.
virtual int32_t get_num_vec_rhs()
Definition: Kernel.h:526
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1062
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:1060
The class Features is the base class of all feature objects.
Definition: Features.h:68
void set_task_vector(std::vector< int32_t > vec)
CMultitaskKernelMaskNormalizer * KernelNormalizerToMultitaskKernelMaskNormalizer(CKernelNormalizer *n)
virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
The Kernel base class.
Definition: Kernel.h:159
float64_t set_normalization_constant(float64_t constant)

SHOGUN Machine Learning Toolbox - Documentation