SHOGUN  3.2.1
 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 
14 #include <shogun/lib/config.h>
15 
17 #include <shogun/kernel/Kernel.h>
18 #include <algorithm>
19 #include <vector>
20 
21 
22 namespace shogun
23 {
28 {
29 
30 public:
33  {
34  num_tasks = 0;
35  num_tasksqr = 0;
36  num_betas = 0;
37  }
38 
41  CMultitaskKernelPlifNormalizer(std::vector<float64_t> support_, std::vector<int32_t> task_vector)
43  {
44 
45  num_betas = static_cast<int>(support_.size());
46 
47  support = support_;
48 
49  // init support points values with constant function
50  betas = std::vector<float64_t>(num_betas);
51  for (int i=0; i!=num_betas; i++)
52  {
53  betas[i] = 1;
54  }
55 
56  num_tasks = get_num_unique_tasks(task_vector);
58 
59  // set both sides equally
60  set_task_vector(task_vector);
61 
62  // init distance matrix
63  distance_matrix = std::vector<float64_t>(num_tasksqr);
64 
65  // init similarity matrix
66  similarity_matrix = std::vector<float64_t>(num_tasksqr);
67 
68  }
69 
70 
76  virtual float64_t normalize(float64_t value, int32_t idx_lhs,
77  int32_t idx_rhs)
78  {
79 
80  //lookup tasks
81  int32_t task_idx_lhs = task_vector_lhs[idx_lhs];
82  int32_t task_idx_rhs = task_vector_rhs[idx_rhs];
83 
84  //lookup similarity
85  float64_t task_similarity = get_task_similarity(task_idx_lhs,
86  task_idx_rhs);
87 
88  //take task similarity into account
89  float64_t similarity = (value/scale) * task_similarity;
90 
91 
92  return similarity;
93 
94  }
95 
101  int32_t get_num_unique_tasks(std::vector<int32_t> vec) {
102 
103  //sort
104  std::sort(vec.begin(), vec.end());
105 
106  //reorder tasks with unique prefix
107  std::vector<int32_t>::iterator endLocation = std::unique(vec.begin(), vec.end());
108 
109  //count unique tasks
110  int32_t num_vec = std::distance(vec.begin(), endLocation);
111 
112  return num_vec;
113 
114  }
115 
116 
119  {
120  }
121 
122 
125  {
126 
127 
128  for (int32_t i=0; i!=num_tasks; i++)
129  {
130  for (int32_t j=0; j!=num_tasks; j++)
131  {
132 
133  float64_t similarity = compute_task_similarity(i, j);
134  set_task_similarity(i,j,similarity);
135 
136  }
137 
138  }
139  }
140 
141 
143  float64_t compute_task_similarity(int32_t task_a, int32_t task_b)
144  {
145 
146  float64_t distance = get_task_distance(task_a, task_b);
147  float64_t similarity = -1;
148 
149  int32_t upper_bound_idx = -1;
150 
151 
152  // determine interval
153  for (int i=1; i!=num_betas; i++)
154  {
155  if (distance <= support[i])
156  {
157  upper_bound_idx = i;
158  break;
159  }
160  }
161 
162  // perform interpolation (constant for beyond upper bound)
163  if (upper_bound_idx == -1)
164  {
165 
166  similarity = betas[num_betas-1];
167 
168  } else {
169 
170  int32_t lower_bound_idx = upper_bound_idx - 1;
171  float64_t interval_size = support[upper_bound_idx] - support[lower_bound_idx];
172 
173  float64_t factor_lower = 1 - (distance - support[lower_bound_idx]) / interval_size;
174  float64_t factor_upper = 1 - factor_lower;
175 
176  similarity = factor_lower*betas[lower_bound_idx] + factor_upper*betas[upper_bound_idx];
177 
178  }
179 
180  return similarity;
181 
182  }
183 
184 
185 public:
186 
188  virtual std::vector<int32_t> get_task_vector_lhs() const
189  {
190  return task_vector_lhs;
191  }
192 
194  virtual void set_task_vector_lhs(std::vector<int32_t> vec)
195  {
196  task_vector_lhs = vec;
197  }
198 
200  virtual std::vector<int32_t> get_task_vector_rhs() const
201  {
202  return task_vector_rhs;
203  }
204 
206  virtual void set_task_vector_rhs(std::vector<int32_t> vec)
207  {
208  task_vector_rhs = vec;
209  }
210 
212  virtual void set_task_vector(std::vector<int32_t> vec)
213  {
214  task_vector_lhs = vec;
215  task_vector_rhs = vec;
216  }
217 
223  float64_t get_task_distance(int32_t task_lhs, int32_t task_rhs)
224  {
225 
226  ASSERT(task_lhs < num_tasks && task_lhs >= 0)
227  ASSERT(task_rhs < num_tasks && task_rhs >= 0)
228 
229  return distance_matrix[task_lhs * num_tasks + task_rhs];
230 
231  }
232 
238  void set_task_distance(int32_t task_lhs, int32_t task_rhs,
240  {
241 
242  ASSERT(task_lhs < num_tasks && task_lhs >= 0)
243  ASSERT(task_rhs < num_tasks && task_rhs >= 0)
244 
245  distance_matrix[task_lhs * num_tasks + task_rhs] = distance;
246 
247  }
248 
254  float64_t get_task_similarity(int32_t task_lhs, int32_t task_rhs)
255  {
256 
257  ASSERT(task_lhs < num_tasks && task_lhs >= 0)
258  ASSERT(task_rhs < num_tasks && task_rhs >= 0)
259 
260  return similarity_matrix[task_lhs * num_tasks + task_rhs];
261 
262  }
263 
269  void set_task_similarity(int32_t task_lhs, int32_t task_rhs,
270  float64_t similarity)
271  {
272 
273  ASSERT(task_lhs < num_tasks && task_lhs >= 0)
274  ASSERT(task_rhs < num_tasks && task_rhs >= 0)
275 
276  similarity_matrix[task_lhs * num_tasks + task_rhs] = similarity;
277 
278  }
279 
283  float64_t get_beta(int32_t idx)
284  {
285 
286  return betas[idx];
287 
288  }
289 
294  void set_beta(int32_t idx, float64_t weight)
295  {
296 
297  betas[idx] = weight;
298 
299  update_cache();
300 
301  }
302 
306  int32_t get_num_betas()
307  {
308 
309  return num_betas;
310 
311  }
312 
313 
315  virtual const char* get_name() const
316  {
317  return "MultitaskKernelPlifNormalizer";
318  }
319 
324  {
325  return dynamic_cast<shogun::CMultitaskKernelPlifNormalizer*>(n);
326  }
327 
328 protected:
331  virtual void register_params()
332  {
333  SG_ADD(&num_tasks, "num_tasks", "the number of tasks", MS_NOT_AVAILABLE);
334  SG_ADD(&num_betas, "num_betas", "the number of weights", MS_NOT_AVAILABLE);
335  m_parameters->add_vector((SGString<float64_t>**)&distance_matrix, &num_tasksqr, "distance_matrix", "distance between tasks");
336  m_parameters->add_vector((SGString<float64_t>**)&similarity_matrix, &num_tasksqr, "similarity_matrix", "similarity between tasks");
337  m_parameters->add_vector((SGString<float64_t>**)&betas, &num_betas, "num_betas", "weights");
338  m_parameters->add_vector((SGString<float64_t>**)&support, &num_betas, "support", "support points");
339  }
340 
342  int32_t num_tasks;
344  int32_t num_tasksqr;
345 
347  std::vector<int32_t> task_vector_lhs;
348 
350  std::vector<int32_t> task_vector_rhs;
351 
353  std::vector<float64_t> distance_matrix;
354 
356  std::vector<float64_t> similarity_matrix;
357 
359  int32_t num_betas;
360 
362  std::vector<float64_t> betas;
363 
365  std::vector<float64_t> support;
366 
367 };
368 }
369 #endif

SHOGUN Machine Learning Toolbox - Documentation