SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MultitaskKernelPlifNormalizer.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 _MULTITASKKERNELPLIFNORMALIZER_H___
12 #define _MULTITASKKERNELPLIFNORMALIZER_H___
13 
15 #include <shogun/kernel/Kernel.h>
16 #include <algorithm>
17 #include <vector>
18 
19 
20 namespace shogun
21 {
26 {
27 
28 public:
31  {
32  num_tasks = 0;
33  num_tasksqr = 0;
34  num_betas = 0;
35  }
36 
39  CMultitaskKernelPlifNormalizer(std::vector<float64_t> support_, std::vector<int32_t> task_vector)
41  {
42 
43  num_betas = static_cast<int>(support_.size());
44 
45  support = support_;
46 
47  // init support points values with constant function
48  betas = std::vector<float64_t>(num_betas);
49  for (int i=0; i!=num_betas; i++)
50  {
51  betas[i] = 1;
52  }
53 
54  num_tasks = get_num_unique_tasks(task_vector);
56 
57  // set both sides equally
58  set_task_vector(task_vector);
59 
60  // init distance matrix
61  distance_matrix = std::vector<float64_t>(num_tasksqr);
62 
63  // init similarity matrix
64  similarity_matrix = std::vector<float64_t>(num_tasksqr);
65 
66  }
67 
68 
74  inline virtual float64_t normalize(float64_t value, int32_t idx_lhs,
75  int32_t idx_rhs)
76  {
77 
78  //lookup tasks
79  int32_t task_idx_lhs = task_vector_lhs[idx_lhs];
80  int32_t task_idx_rhs = task_vector_rhs[idx_rhs];
81 
82  //lookup similarity
83  float64_t task_similarity = get_task_similarity(task_idx_lhs,
84  task_idx_rhs);
85 
86  //take task similarity into account
87  float64_t similarity = (value/scale) * task_similarity;
88 
89 
90  return similarity;
91 
92  }
93 
99  int32_t get_num_unique_tasks(std::vector<int32_t> vec) {
100 
101  //sort
102  std::sort(vec.begin(), vec.end());
103 
104  //reorder tasks with unique prefix
105  std::vector<int32_t>::iterator endLocation = std::unique(vec.begin(), vec.end());
106 
107  //count unique tasks
108  int32_t num_vec = std::distance(vec.begin(), endLocation);
109 
110  return num_vec;
111 
112  }
113 
114 
117  {
118  }
119 
120 
123  {
124 
125 
126  for (int32_t i=0; i!=num_tasks; i++)
127  {
128  for (int32_t j=0; j!=num_tasks; j++)
129  {
130 
131  float64_t similarity = compute_task_similarity(i, j);
132  set_task_similarity(i,j,similarity);
133 
134  }
135 
136  }
137  }
138 
139 
141  float64_t compute_task_similarity(int32_t task_a, int32_t task_b)
142  {
143 
144  float64_t distance = get_task_distance(task_a, task_b);
145  float64_t similarity = -1;
146 
147  int32_t upper_bound_idx = -1;
148 
149 
150  // determine interval
151  for (int i=1; i!=num_betas; i++)
152  {
153  if (distance <= support[i])
154  {
155  upper_bound_idx = i;
156  break;
157  }
158  }
159 
160  // perform interpolation (constant for beyond upper bound)
161  if (upper_bound_idx == -1)
162  {
163 
164  similarity = betas[num_betas-1];
165 
166  } else {
167 
168  int32_t lower_bound_idx = upper_bound_idx - 1;
169  float64_t interval_size = support[upper_bound_idx] - support[lower_bound_idx];
170 
171  float64_t factor_lower = 1 - (distance - support[lower_bound_idx]) / interval_size;
172  float64_t factor_upper = 1 - factor_lower;
173 
174  similarity = factor_lower*betas[lower_bound_idx] + factor_upper*betas[upper_bound_idx];
175 
176  }
177 
178  return similarity;
179 
180  }
181 
182 
183 public:
184 
186  virtual std::vector<int32_t> get_task_vector_lhs() const
187  {
188  return task_vector_lhs;
189  }
190 
192  virtual void set_task_vector_lhs(std::vector<int32_t> vec)
193  {
194  task_vector_lhs = vec;
195  }
196 
198  virtual std::vector<int32_t> get_task_vector_rhs() const
199  {
200  return task_vector_rhs;
201  }
202 
204  virtual void set_task_vector_rhs(std::vector<int32_t> vec)
205  {
206  task_vector_rhs = vec;
207  }
208 
210  virtual void set_task_vector(std::vector<int32_t> vec)
211  {
212  task_vector_lhs = vec;
213  task_vector_rhs = vec;
214  }
215 
221  float64_t get_task_distance(int32_t task_lhs, int32_t task_rhs)
222  {
223 
224  ASSERT(task_lhs < num_tasks && task_lhs >= 0);
225  ASSERT(task_rhs < num_tasks && task_rhs >= 0);
226 
227  return distance_matrix[task_lhs * num_tasks + task_rhs];
228 
229  }
230 
236  void set_task_distance(int32_t task_lhs, int32_t task_rhs,
238  {
239 
240  ASSERT(task_lhs < num_tasks && task_lhs >= 0);
241  ASSERT(task_rhs < num_tasks && task_rhs >= 0);
242 
243  distance_matrix[task_lhs * num_tasks + task_rhs] = distance;
244 
245  }
246 
252  float64_t get_task_similarity(int32_t task_lhs, int32_t task_rhs)
253  {
254 
255  ASSERT(task_lhs < num_tasks && task_lhs >= 0);
256  ASSERT(task_rhs < num_tasks && task_rhs >= 0);
257 
258  return similarity_matrix[task_lhs * num_tasks + task_rhs];
259 
260  }
261 
267  void set_task_similarity(int32_t task_lhs, int32_t task_rhs,
268  float64_t similarity)
269  {
270 
271  ASSERT(task_lhs < num_tasks && task_lhs >= 0);
272  ASSERT(task_rhs < num_tasks && task_rhs >= 0);
273 
274  similarity_matrix[task_lhs * num_tasks + task_rhs] = similarity;
275 
276  }
277 
281  float64_t get_beta(int32_t idx)
282  {
283 
284  return betas[idx];
285 
286  }
287 
292  void set_beta(int32_t idx, float64_t weight)
293  {
294 
295  betas[idx] = weight;
296 
297  update_cache();
298 
299  }
300 
304  int32_t get_num_betas()
305  {
306 
307  return num_betas;
308 
309  }
310 
311 
313  inline virtual const char* get_name() const
314  {
315  return "MultitaskKernelPlifNormalizer";
316  }
317 
322  {
323  return dynamic_cast<shogun::CMultitaskKernelPlifNormalizer*>(n);
324  }
325 
326 protected:
329  virtual void register_params()
330  {
331  SG_ADD(&num_tasks, "num_tasks", "the number of tasks", MS_NOT_AVAILABLE);
332  SG_ADD(&num_betas, "num_betas", "the number of weights", MS_NOT_AVAILABLE);
333  m_parameters->add_vector((SGString<float64_t>**)&distance_matrix, &num_tasksqr, "distance_matrix", "distance between tasks");
334  m_parameters->add_vector((SGString<float64_t>**)&similarity_matrix, &num_tasksqr, "similarity_matrix", "similarity between tasks");
335  m_parameters->add_vector((SGString<float64_t>**)&betas, &num_betas, "num_betas", "weights");
336  m_parameters->add_vector((SGString<float64_t>**)&support, &num_betas, "support", "support points");
337  }
338 
340  int32_t num_tasks;
342  int32_t num_tasksqr;
343 
345  std::vector<int32_t> task_vector_lhs;
346 
348  std::vector<int32_t> task_vector_rhs;
349 
351  std::vector<float64_t> distance_matrix;
352 
354  std::vector<float64_t> similarity_matrix;
355 
357  int32_t num_betas;
358 
360  std::vector<float64_t> betas;
361 
363  std::vector<float64_t> support;
364 
365 };
366 }
367 #endif

SHOGUN Machine Learning Toolbox - Documentation