SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 
18 
19 
20 namespace shogun
21 {
32 {
33 
34 public:
35 
39  {
40  }
41 
46  CMultitaskKernelNormalizer(std::vector<int32_t> task_vector)
47  : CKernelNormalizer(), scale(1.0)
48  {
49 
50  num_tasks = get_num_unique_tasks(task_vector);
51 
52  // set both sides equally
53  set_task_vector(task_vector);
54 
55  // init similarity matrix
56  similarity_matrix = std::vector<float64_t>(num_tasks * num_tasks);
57 
58  }
59 
62  {
63  }
64 
67  virtual bool init(CKernel* k)
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  if (strcmp(k->get_name(), "WeightedDegree") == 0) {
77  SG_INFO("using first-element normalization\n");
78  scale=k->compute(0, 0);
79  } else {
80  SG_INFO("no inner normalization for non-WDK kernel\n");
81  scale=1.0;
82  }
83 
84  k->lhs=old_lhs;
85  k->rhs=old_rhs;
86 
87  ASSERT(k);
88  int32_t num_lhs = k->get_num_vec_lhs();
89  int32_t num_rhs = k->get_num_vec_rhs();
90  ASSERT(num_lhs>0);
91  ASSERT(num_rhs>0);
92 
93  //std::cout << "scale: " << scale << std::endl;
94 
95  return true;
96  }
97 
103  int32_t get_num_unique_tasks(std::vector<int32_t> vec) {
104 
105  //sort
106  std::sort(vec.begin(), vec.end());
107 
108  //reorder tasks with unique prefix
109  std::vector<int32_t>::iterator endLocation = std::unique(vec.begin(), vec.end());
110 
111  //count unique tasks
112  int32_t num_vec = std::distance(vec.begin(), endLocation);
113 
114  return num_vec;
115 
116  }
117 
123  inline virtual float64_t normalize(float64_t value, int32_t idx_lhs,
124  int32_t idx_rhs)
125  {
126 
127  //lookup tasks
128  int32_t task_idx_lhs = task_vector_lhs[idx_lhs];
129  int32_t task_idx_rhs = task_vector_rhs[idx_rhs];
130 
131  //lookup similarity
132  float64_t task_similarity = get_task_similarity(task_idx_lhs,
133  task_idx_rhs);
134 
135  //take task similarity into account
136  float64_t similarity = (value/scale) * task_similarity;
137 
138 
139  return similarity;
140 
141  }
142 
147  inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
148  {
149  SG_ERROR("normalize_lhs not implemented");
150  return 0;
151  }
152 
157  inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
158  {
159  SG_ERROR("normalize_rhs not implemented");
160  return 0;
161  }
162 
163 public:
164 
166  std::vector<int32_t> get_task_vector_lhs() const
167  {
168  return task_vector_lhs;
169  }
170 
172  void set_task_vector_lhs(std::vector<int32_t> vec)
173  {
174  task_vector_lhs = vec;
175  }
176 
178  std::vector<int32_t> get_task_vector_rhs() const
179  {
180  return task_vector_rhs;
181  }
182 
184  void set_task_vector_rhs(std::vector<int32_t> vec)
185  {
186  task_vector_rhs = vec;
187  }
188 
190  void set_task_vector(std::vector<int32_t> vec)
191  {
192  task_vector_lhs = vec;
193  task_vector_rhs = vec;
194  }
195 
201  float64_t get_task_similarity(int32_t task_lhs, int32_t task_rhs)
202  {
203 
204  ASSERT(task_lhs < num_tasks && task_lhs >= 0);
205  ASSERT(task_rhs < num_tasks && task_rhs >= 0);
206 
207  return similarity_matrix[task_lhs * num_tasks + task_rhs];
208 
209  }
210 
216  void set_task_similarity(int32_t task_lhs, int32_t task_rhs,
217  float64_t similarity)
218  {
219 
220  ASSERT(task_lhs < num_tasks && task_lhs >= 0);
221  ASSERT(task_rhs < num_tasks && task_rhs >= 0);
222 
223  similarity_matrix[task_lhs * num_tasks + task_rhs] = similarity;
224 
225  }
226 
228  inline virtual const char* get_name() const
229  {
230  return "MultitaskKernelNormalizer";
231  }
232 
238  {
239  return dynamic_cast<CMultitaskKernelNormalizer*>(n);
240  }
241 
242 
243 protected:
244 
246  std::vector<float64_t> similarity_matrix;
247 
249  int32_t num_tasks;
250 
252  std::vector<int32_t> task_vector_lhs;
253 
255  std::vector<int32_t> task_vector_rhs;
256 
259 
260 };
261 }
262 #endif

SHOGUN Machine Learning Toolbox - Documentation