SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MultitaskKernelNormalizer.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 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2009 Christian Widmer
8  * Copyright (C) 2009 Max-Planck-Society
9  */
10 
11 #ifndef _MULTITASKKERNELNORMALIZER_H___
12 #define _MULTITASKKERNELNORMALIZER_H___
13 
15 #include <shogun/kernel/Kernel.h>
16 #include <algorithm>
17 #include <vector>
18 
19 
20 
21 namespace shogun
22 {
33 {
34 
35 public:
36 
40  {
41  }
42 
47  CMultitaskKernelNormalizer(std::vector<int32_t> task_vector)
48  : CKernelNormalizer(), scale(1.0)
49  {
50 
51  num_tasks = get_num_unique_tasks(task_vector);
52 
53  // set both sides equally
54  set_task_vector(task_vector);
55 
56  // init similarity matrix
57  similarity_matrix = std::vector<float64_t>(num_tasks * num_tasks);
58 
59  }
60 
63  {
64  }
65 
68  virtual bool init(CKernel* k)
69  {
70 
71  //same as first-element normalizer
72  CFeatures* old_lhs=k->lhs;
73  CFeatures* old_rhs=k->rhs;
74  k->lhs=old_lhs;
75  k->rhs=old_lhs;
76 
77  if (strcmp(k->get_name(), "WeightedDegree") == 0) {
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  ASSERT(k);
89  int32_t num_lhs = k->get_num_vec_lhs();
90  int32_t num_rhs = k->get_num_vec_rhs();
91  ASSERT(num_lhs>0);
92  ASSERT(num_rhs>0);
93 
94  //std::cout << "scale: " << scale << std::endl;
95 
96  return true;
97  }
98 
104  int32_t get_num_unique_tasks(std::vector<int32_t> vec) {
105 
106  //sort
107  std::sort(vec.begin(), vec.end());
108 
109  //reorder tasks with unique prefix
110  std::vector<int32_t>::iterator endLocation = std::unique(vec.begin(), vec.end());
111 
112  //count unique tasks
113  int32_t num_vec = std::distance(vec.begin(), endLocation);
114 
115  return num_vec;
116 
117  }
118 
124  inline virtual float64_t normalize(float64_t value, int32_t idx_lhs,
125  int32_t idx_rhs)
126  {
127 
128  //lookup tasks
129  int32_t task_idx_lhs = task_vector_lhs[idx_lhs];
130  int32_t task_idx_rhs = task_vector_rhs[idx_rhs];
131 
132  //lookup similarity
133  float64_t task_similarity = get_task_similarity(task_idx_lhs,
134  task_idx_rhs);
135 
136  //take task similarity into account
137  float64_t similarity = (value/scale) * task_similarity;
138 
139 
140  return similarity;
141 
142  }
143 
148  inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
149  {
150  SG_ERROR("normalize_lhs not implemented");
151  return 0;
152  }
153 
158  inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
159  {
160  SG_ERROR("normalize_rhs not implemented");
161  return 0;
162  }
163 
164 public:
165 
167  std::vector<int32_t> get_task_vector_lhs() const
168  {
169  return task_vector_lhs;
170  }
171 
173  void set_task_vector_lhs(std::vector<int32_t> vec)
174  {
175  task_vector_lhs = vec;
176  }
177 
179  std::vector<int32_t> get_task_vector_rhs() const
180  {
181  return task_vector_rhs;
182  }
183 
185  void set_task_vector_rhs(std::vector<int32_t> vec)
186  {
187  task_vector_rhs = vec;
188  }
189 
191  void set_task_vector(std::vector<int32_t> vec)
192  {
193  task_vector_lhs = vec;
194  task_vector_rhs = vec;
195  }
196 
202  float64_t get_task_similarity(int32_t task_lhs, int32_t task_rhs)
203  {
204 
205  ASSERT(task_lhs < num_tasks && task_lhs >= 0);
206  ASSERT(task_rhs < num_tasks && task_rhs >= 0);
207 
208  return similarity_matrix[task_lhs * num_tasks + task_rhs];
209 
210  }
211 
217  void set_task_similarity(int32_t task_lhs, int32_t task_rhs,
218  float64_t similarity)
219  {
220 
221  ASSERT(task_lhs < num_tasks && task_lhs >= 0);
222  ASSERT(task_rhs < num_tasks && task_rhs >= 0);
223 
224  similarity_matrix[task_lhs * num_tasks + task_rhs] = similarity;
225 
226  }
227 
229  inline virtual const char* get_name() const
230  {
231  return "MultitaskKernelNormalizer";
232  }
233 
239  {
240  return dynamic_cast<CMultitaskKernelNormalizer*>(n);
241  }
242 
243 
244 protected:
245 
247  std::vector<float64_t> similarity_matrix;
248 
250  int32_t num_tasks;
251 
253  std::vector<int32_t> task_vector_lhs;
254 
256  std::vector<int32_t> task_vector_rhs;
257 
260 
261 };
262 }
263 #endif

SHOGUN Machine Learning Toolbox - Documentation