SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MultitaskKernelMaskPairNormalizer.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 _MULTITASKKERNELMASKPAIRNORMALIZER_H___
12 #define _MULTITASKKERNELMASKPAIRNORMALIZER_H___
13 
15 #include <shogun/kernel/Kernel.h>
16 
17 #include <string>
18 
19 namespace shogun
20 {
21 
22 
28 {
29 
30 public:
31 
36  {
37  }
38 
43  CMultitaskKernelMaskPairNormalizer(std::vector<int32_t> task_vector_,
44  std::vector<std::pair<int32_t, int32_t> > active_pairs_) :
45  scale(1.0), normalization_constant(1.0)
46  {
47 
48  set_task_vector(task_vector_);
49  active_pairs = active_pairs_;
50 
51  }
52 
53 
56  {
57  }
58 
61  virtual bool init(CKernel* k)
62  {
63  ASSERT(k);
64  int32_t num_lhs = k->get_num_vec_lhs();
65  int32_t num_rhs = k->get_num_vec_rhs();
66  ASSERT(num_lhs>0);
67  ASSERT(num_rhs>0);
68 
69 
70  //same as first-element normalizer
71  CFeatures* old_lhs=k->lhs;
72  CFeatures* old_rhs=k->rhs;
73  k->lhs=old_lhs;
74  k->rhs=old_lhs;
75 
76 
77  if (std::string(k->get_name()) == "WeightedDegree") {
78  SG_INFO("using first-element normalization\n");
79  scale=k->compute(0, 0);
80  } else {
81  SG_INFO("no inner normalization for non-WDK kernel\n");
82  scale=1.0;
83  }
84 
85  k->lhs=old_lhs;
86  k->rhs=old_rhs;
87 
88 
89  return true;
90  }
91 
92 
93 
99  inline virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
100  {
101 
102  //lookup tasks
103  int32_t task_idx_lhs = task_vector_lhs[idx_lhs];
104  int32_t task_idx_rhs = task_vector_rhs[idx_rhs];
105 
106  //lookup similarity
107  float64_t task_similarity = get_similarity(task_idx_lhs, task_idx_rhs);
108 
109  //take task similarity into account
110  float64_t similarity = (value/scale) * task_similarity;
111 
112 
113  return similarity;
114 
115  }
116 
121  inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
122  {
123  SG_ERROR("normalize_lhs not implemented");
124  return 0;
125  }
126 
131  inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
132  {
133  SG_ERROR("normalize_rhs not implemented");
134  return 0;
135  }
136 
138  std::vector<int32_t> get_task_vector_lhs() const
139  {
140  return task_vector_lhs;
141  }
142 
143 
145  void set_task_vector_lhs(std::vector<int32_t> vec)
146  {
147 
148  task_vector_lhs.clear();
149 
150  for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
151  {
152  task_vector_lhs.push_back(vec[i]);
153  }
154 
155  }
156 
159  std::vector<int32_t> get_task_vector_rhs() const
160  {
161  return task_vector_rhs;
162  }
163 
164 
166  void set_task_vector_rhs(std::vector<int32_t> vec)
167  {
168 
169  task_vector_rhs.clear();
170 
171  for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
172  {
173  task_vector_rhs.push_back(vec[i]);
174  }
175 
176  }
177 
179  void set_task_vector(std::vector<int32_t> vec)
180  {
181  set_task_vector_lhs(vec);
182  set_task_vector_rhs(vec);
183  }
184 
185 
191  float64_t get_similarity(int32_t task_lhs, int32_t task_rhs)
192  {
193 
194  float64_t similarity = 0.0;
195 
196  for (int32_t i=0; i!=static_cast<int>(active_pairs.size()); i++)
197  {
198  std::pair<int32_t, int32_t> block = active_pairs[i];
199 
200  // ignore order of pair
201  if ((block.first==task_lhs && block.second==task_rhs) ||
202  (block.first==task_rhs && block.second==task_lhs))
203  {
204  similarity = 1.0 / normalization_constant;
205  break;
206  }
207  }
208 
209 
210  return similarity;
211 
212  }
213 
215  std::vector<std::pair<int32_t, int32_t> > get_active_pairs()
216  {
217  return active_pairs;
218  }
219 
222  {
223  return normalization_constant;
224  }
225 
228  {
229  normalization_constant = constant;
230 
232  return 0.0;
233  }
234 
235 
237  inline virtual const char* get_name() const
238  {
239  return "MultitaskKernelMaskPairNormalizer";
240  }
241 
246  {
247  return dynamic_cast<shogun::CMultitaskKernelMaskPairNormalizer*>(n);
248  }
249 
250 protected:
251 
253  std::vector<std::pair<int32_t, int32_t> > active_pairs;
254 
256  std::vector<int32_t> task_vector_lhs;
257 
259  std::vector<int32_t> task_vector_rhs;
260 
263 
266 
267 };
268 }
269 #endif

SHOGUN Machine Learning Toolbox - Documentation