SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MultitaskKernelMaskPairNormalizer.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 _MULTITASKKERNELMASKPAIRNORMALIZER_H___
12 #define _MULTITASKKERNELMASKPAIRNORMALIZER_H___
13 
14 #include <shogun/lib/config.h>
15 
17 #include <shogun/kernel/Kernel.h>
18 
19 #include <string>
20 #include <vector>
21 #include <utility>
22 
23 namespace shogun
24 {
25 
26 
32 {
33 
34 public:
35 
40  {
41  }
42 
47  CMultitaskKernelMaskPairNormalizer(std::vector<int32_t> task_vector_,
48  std::vector<std::pair<int32_t, int32_t> > active_pairs_) :
49  scale(1.0), normalization_constant(1.0)
50  {
51 
52  set_task_vector(task_vector_);
53  active_pairs = active_pairs_;
54 
55  }
56 
57 
60  {
61  }
62 
65  virtual bool init(CKernel* k)
66  {
67  ASSERT(k)
68  int32_t num_lhs = k->get_num_vec_lhs();
69  int32_t num_rhs = k->get_num_vec_rhs();
70  ASSERT(num_lhs>0)
71  ASSERT(num_rhs>0)
72 
73 
74  //same as first-element normalizer
75  CFeatures* old_lhs=k->lhs;
76  CFeatures* old_rhs=k->rhs;
77  k->lhs=old_lhs;
78  k->rhs=old_lhs;
79 
80 
81  if (std::string(k->get_name()) == "WeightedDegree") {
82  SG_INFO("using first-element normalization\n")
83  scale=k->compute(0, 0);
84  } else {
85  SG_INFO("no inner normalization for non-WDK kernel\n")
86  scale=1.0;
87  }
88 
89  k->lhs=old_lhs;
90  k->rhs=old_rhs;
91 
92 
93  return true;
94  }
95 
96 
97 
103  virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
104  {
105 
106  //lookup tasks
107  int32_t task_idx_lhs = task_vector_lhs[idx_lhs];
108  int32_t task_idx_rhs = task_vector_rhs[idx_rhs];
109 
110  //lookup similarity
111  float64_t task_similarity = get_similarity(task_idx_lhs, task_idx_rhs);
112 
113  //take task similarity into account
114  float64_t similarity = (value/scale) * task_similarity;
115 
116 
117  return similarity;
118 
119  }
120 
125  virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
126  {
127  SG_ERROR("normalize_lhs not implemented")
128  return 0;
129  }
130 
135  virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
136  {
137  SG_ERROR("normalize_rhs not implemented")
138  return 0;
139  }
140 
142  std::vector<int32_t> get_task_vector_lhs() const
143  {
144  return task_vector_lhs;
145  }
146 
147 
149  void set_task_vector_lhs(std::vector<int32_t> vec)
150  {
151 
152  task_vector_lhs.clear();
153 
154  for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
155  {
156  task_vector_lhs.push_back(vec[i]);
157  }
158 
159  }
160 
163  std::vector<int32_t> get_task_vector_rhs() const
164  {
165  return task_vector_rhs;
166  }
167 
168 
170  void set_task_vector_rhs(std::vector<int32_t> vec)
171  {
172 
173  task_vector_rhs.clear();
174 
175  for (int32_t i = 0; i != (int32_t)(vec.size()); ++i)
176  {
177  task_vector_rhs.push_back(vec[i]);
178  }
179 
180  }
181 
183  void set_task_vector(std::vector<int32_t> vec)
184  {
185  set_task_vector_lhs(vec);
186  set_task_vector_rhs(vec);
187  }
188 
189 
195  float64_t get_similarity(int32_t task_lhs, int32_t task_rhs)
196  {
197 
198  float64_t similarity = 0.0;
199 
200  for (int32_t i=0; i!=static_cast<int>(active_pairs.size()); i++)
201  {
202  std::pair<int32_t, int32_t> block = active_pairs[i];
203 
204  // ignore order of pair
205  if ((block.first==task_lhs && block.second==task_rhs) ||
206  (block.first==task_rhs && block.second==task_lhs))
207  {
208  similarity = 1.0 / normalization_constant;
209  break;
210  }
211  }
212 
213 
214  return similarity;
215 
216  }
217 
219  std::vector<std::pair<int32_t, int32_t> > get_active_pairs()
220  {
221  return active_pairs;
222  }
223 
226  {
227  return normalization_constant;
228  }
229 
232  {
233  normalization_constant = constant;
234 
236  return 0.0;
237  }
238 
239 
241  virtual const char* get_name() const
242  {
243  return "MultitaskKernelMaskPairNormalizer";
244  }
245 
250  {
251  return dynamic_cast<shogun::CMultitaskKernelMaskPairNormalizer*>(n);
252  }
253 
254 protected:
255 
257  std::vector<std::pair<int32_t, int32_t> > active_pairs;
258 
260  std::vector<int32_t> task_vector_lhs;
261 
263  std::vector<int32_t> task_vector_rhs;
264 
267 
270 
271 };
272 }
273 #endif
virtual const char * get_name() const =0
#define SG_INFO(...)
Definition: SGIO.h:118
virtual float64_t compute(int32_t x, int32_t y)=0
float64_t get_similarity(int32_t task_lhs, int32_t task_rhs)
std::vector< std::pair< int32_t, int32_t > > active_pairs
#define SG_ERROR(...)
Definition: SGIO.h:129
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:139
virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:516
CMultitaskKernelMaskPairNormalizer * KernelNormalizerToMultitaskKernelMaskPairNormalizer(CKernelNormalizer *n)
virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
#define ASSERT(x)
Definition: SGIO.h:201
CMultitaskKernelMaskPairNormalizer(std::vector< int32_t > task_vector_, std::vector< std::pair< int32_t, int32_t > > active_pairs_)
double float64_t
Definition: common.h:50
std::vector< std::pair< int32_t, int32_t > > get_active_pairs()
The MultitaskKernel allows Multitask Learning via a modified kernel function.
The class Kernel Normalizer defines a function to post-process kernel values.
virtual int32_t get_num_vec_rhs()
Definition: Kernel.h:525
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
virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
The class Features is the base class of all feature objects.
Definition: Features.h:68
The Kernel base class.
Definition: Kernel.h:158
Block< Matrix > block(Matrix matrix, index_t row_begin, index_t col_begin, index_t row_size, index_t col_size)
Definition: Block.h:102

SHOGUN Machine Learning Toolbox - Documentation