SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
14 #include <shogun/lib/config.h>
15 
16 #include <shogun/kernel/Kernel.h>
18 #include <algorithm>
19 #include <vector>
20 
21 
22 
23 namespace shogun
24 {
35 {
36 
37 public:
38 
42  {
43  }
44 
49  CMultitaskKernelNormalizer(std::vector<int32_t> task_vector)
50  : CKernelNormalizer(), scale(1.0)
51  {
52 
53  num_tasks = get_num_unique_tasks(task_vector);
54 
55  // set both sides equally
56  set_task_vector(task_vector);
57 
58  // init similarity matrix
59  similarity_matrix = std::vector<float64_t>(num_tasks * num_tasks);
60 
61  }
62 
65  {
66  }
67 
70  virtual bool init(CKernel* k)
71  {
72 
73  //same as first-element normalizer
74  CFeatures* old_lhs=k->lhs;
75  CFeatures* old_rhs=k->rhs;
76  k->lhs=old_lhs;
77  k->rhs=old_lhs;
78 
79  if (strcmp(k->get_name(), "WeightedDegree") == 0) {
80  SG_INFO("using first-element normalization\n")
81  scale=k->compute(0, 0);
82  } else {
83  SG_INFO("no inner normalization for non-WDK kernel\n")
84  scale=1.0;
85  }
86 
87  k->lhs=old_lhs;
88  k->rhs=old_rhs;
89 
90  ASSERT(k)
91  int32_t num_lhs = k->get_num_vec_lhs();
92  int32_t num_rhs = k->get_num_vec_rhs();
93  ASSERT(num_lhs>0)
94  ASSERT(num_rhs>0)
95 
96  //std::cout << "scale: " << scale << std::endl;
97 
98  return true;
99  }
100 
106  int32_t get_num_unique_tasks(std::vector<int32_t> vec) {
107 
108  //sort
109  std::sort(vec.begin(), vec.end());
110 
111  //reorder tasks with unique prefix
112  std::vector<int32_t>::iterator endLocation = std::unique(vec.begin(), vec.end());
113 
114  //count unique tasks
115  int32_t num_vec = std::distance(vec.begin(), endLocation);
116 
117  return num_vec;
118 
119  }
120 
126  virtual float64_t normalize(float64_t value, int32_t idx_lhs,
127  int32_t idx_rhs)
128  {
129 
130  //lookup tasks
131  int32_t task_idx_lhs = task_vector_lhs[idx_lhs];
132  int32_t task_idx_rhs = task_vector_rhs[idx_rhs];
133 
134  //lookup similarity
135  float64_t task_similarity = get_task_similarity(task_idx_lhs,
136  task_idx_rhs);
137 
138  //take task similarity into account
139  float64_t similarity = (value/scale) * task_similarity;
140 
141 
142  return similarity;
143 
144  }
145 
150  virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
151  {
152  SG_ERROR("normalize_lhs not implemented")
153  return 0;
154  }
155 
160  virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
161  {
162  SG_ERROR("normalize_rhs not implemented")
163  return 0;
164  }
165 
166 public:
167 
169  std::vector<int32_t> get_task_vector_lhs() const
170  {
171  return task_vector_lhs;
172  }
173 
175  void set_task_vector_lhs(std::vector<int32_t> vec)
176  {
177  task_vector_lhs = vec;
178  }
179 
181  std::vector<int32_t> get_task_vector_rhs() const
182  {
183  return task_vector_rhs;
184  }
185 
187  void set_task_vector_rhs(std::vector<int32_t> vec)
188  {
189  task_vector_rhs = vec;
190  }
191 
193  void set_task_vector(std::vector<int32_t> vec)
194  {
195  task_vector_lhs = vec;
196  task_vector_rhs = vec;
197  }
198 
204  float64_t get_task_similarity(int32_t task_lhs, int32_t task_rhs)
205  {
206 
207  ASSERT(task_lhs < num_tasks && task_lhs >= 0)
208  ASSERT(task_rhs < num_tasks && task_rhs >= 0)
209 
210  return similarity_matrix[task_lhs * num_tasks + task_rhs];
211 
212  }
213 
219  void set_task_similarity(int32_t task_lhs, int32_t task_rhs,
220  float64_t similarity)
221  {
222 
223  ASSERT(task_lhs < num_tasks && task_lhs >= 0)
224  ASSERT(task_rhs < num_tasks && task_rhs >= 0)
225 
226  similarity_matrix[task_lhs * num_tasks + task_rhs] = similarity;
227 
228  }
229 
231  virtual const char* get_name() const
232  {
233  return "MultitaskKernelNormalizer";
234  }
235 
241  {
242  return dynamic_cast<CMultitaskKernelNormalizer*>(n);
243  }
244 
245 
246 protected:
247 
249  std::vector<float64_t> similarity_matrix;
250 
252  int32_t num_tasks;
253 
255  std::vector<int32_t> task_vector_lhs;
256 
258  std::vector<int32_t> task_vector_rhs;
259 
262 
263 };
264 }
265 #endif
virtual const char * get_name() const =0
float distance(CJLCoverTreePoint p1, CJLCoverTreePoint p2, float64_t upper_bound)
The MultitaskKernel allows Multitask Learning via a modified kernel function.
#define SG_INFO(...)
Definition: SGIO.h:118
virtual const char * get_name() const
virtual float64_t compute(int32_t x, int32_t y)=0
virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
CMultitaskKernelNormalizer(std::vector< int32_t > task_vector)
float64_t get_task_similarity(int32_t task_lhs, int32_t task_rhs)
#define SG_ERROR(...)
Definition: SGIO.h:129
std::vector< int32_t > get_task_vector_lhs() const
void set_task_vector_rhs(std::vector< int32_t > vec)
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:516
void set_task_vector_lhs(std::vector< int32_t > vec)
#define ASSERT(x)
Definition: SGIO.h:201
virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
double float64_t
Definition: common.h:50
void set_task_vector(std::vector< int32_t > vec)
The class Kernel Normalizer defines a function to post-process kernel values.
virtual int32_t get_num_vec_rhs()
Definition: Kernel.h:525
void set_task_similarity(int32_t task_lhs, int32_t task_rhs, float64_t similarity)
CMultitaskKernelNormalizer * KernelNormalizerToMultitaskKernelNormalizer(CKernelNormalizer *n)
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1061
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:1059
The class Features is the base class of all feature objects.
Definition: Features.h:68
The Kernel base class.
Definition: Kernel.h:158
int32_t get_num_unique_tasks(std::vector< int32_t > vec)
std::vector< int32_t > get_task_vector_rhs() const

SHOGUN Machine Learning Toolbox - Documentation