SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ZeroMeanCenterKernelNormalizer.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) 2010 Gorden Jemwa
8  * Copyright (C) 2010 University of Stellenbosch
9  */
10 
11 #ifndef _ZEROMEANCENTERKERNELNORMALIZER_H___
12 #define _ZEROMEANCENTERKERNELNORMALIZER_H___
13 
14 #include <shogun/lib/config.h>
15 
17 
18 namespace shogun
19 {
44 {
45  public:
50  ktest_row_means(NULL), num_ktest(0)
51  {
53  "num_ktrain", "Train row means.");
55  "num_ktest","Test row means.");
56  }
57 
60  {
61  SG_FREE(ktrain_row_means);
62  SG_FREE(ktest_row_means);
63  }
64 
67  virtual bool init(CKernel* k)
68  {
69  ASSERT(k)
70  int32_t num_lhs=k->get_num_vec_lhs();
71  int32_t num_rhs=k->get_num_vec_rhs();
72  ASSERT(num_lhs>0)
73  ASSERT(num_rhs>0)
74 
75  CFeatures* old_lhs=k->lhs;
76  CFeatures* old_rhs=k->rhs;
77 
78  /* compute mean for each row of the train matrix*/
79  k->lhs=old_lhs;
80  k->rhs=old_lhs;
81 
82  bool r1=alloc_and_compute_row_means(k, ktrain_row_means, num_lhs,num_lhs);
83 
84  /* compute mean for each row of the test matrix */
85  k->lhs=old_lhs;
86  k->rhs=old_rhs;
87 
88  bool r2=alloc_and_compute_row_means(k, ktest_row_means, num_lhs,num_rhs);
89 
90  /* compute train kernel matrix mean */
91  ktrain_mean=0;
92  for (int32_t i=0;i<num_lhs;i++)
93  ktrain_mean += (ktrain_row_means[i]/num_lhs);
94 
95  k->lhs=old_lhs;
96  k->rhs=old_rhs;
97 
98  return r1 && r2;
99  }
100 
107  float64_t value, int32_t idx_lhs, int32_t idx_rhs)
108  {
109  value += (-ktrain_row_means[idx_lhs] - ktest_row_means[idx_rhs] + ktrain_mean);
110  return value;
111  }
112 
117  virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
118  {
119  SG_ERROR("normalize_lhs not implemented")
120  return 0;
121  }
122 
127  virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
128  {
129  SG_ERROR("normalize_rhs not implemented")
130  return 0;
131  }
132 
137  bool alloc_and_compute_row_means(CKernel* k, float64_t* &v, int32_t num_lhs, int32_t num_rhs)
138  {
139  SG_FREE(v);
140  v=SG_MALLOC(float64_t, num_rhs);
141 
142  for (int32_t i=0; i<num_rhs; i++)
143  {
144  v[i]=0;
145  for (int32_t j=0; j<num_lhs; j++)
146  v[i] += ( k->compute(j,i)/num_lhs );
147  }
148  return (v!=NULL);
149  }
150 
152  virtual const char* get_name() const { return "ZeroMeanCenterKernelNormalizer"; }
153 
154  protected:
157 
159  int32_t num_ktrain;
160 
163 
165  int32_t num_ktest;
166 
169 };
170 }
171 #endif
virtual float64_t compute(int32_t x, int32_t y)=0
virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
#define SG_ERROR(...)
Definition: SGIO.h:129
Parameter * m_parameters
Definition: SGObject.h:546
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:517
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
The class Kernel Normalizer defines a function to post-process kernel values.
ZeroMeanCenterKernelNormalizer centers the kernel in feature space.
virtual int32_t get_num_vec_rhs()
Definition: Kernel.h:526
bool alloc_and_compute_row_means(CKernel *k, float64_t *&v, int32_t num_lhs, int32_t num_rhs)
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1062
void add_vector(bool **param, index_t *length, const char *name, const char *description="")
Definition: Parameter.cpp:334
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:1060
The class Features is the base class of all feature objects.
Definition: Features.h:68
The Kernel base class.
Definition: Kernel.h:159
virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)

SHOGUN Machine Learning Toolbox - Documentation