SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
15 
16 namespace shogun
17 {
42 {
43  public:
48  ktest_row_means(NULL), num_ktest(0)
49  {
51  "num_ktrain", "Train row means.");
53  "num_ktest","Test row means.");
54  }
55 
58  {
59  SG_FREE(ktrain_row_means);
60  SG_FREE(ktest_row_means);
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  CFeatures* old_lhs=k->lhs;
74  CFeatures* old_rhs=k->rhs;
75 
76  /* compute mean for each row of the train matrix*/
77  k->lhs=old_lhs;
78  k->rhs=old_lhs;
79 
80  bool r1=alloc_and_compute_row_means(k, ktrain_row_means, num_lhs,num_lhs);
81 
82  /* compute mean for each row of the test matrix */
83  k->lhs=old_lhs;
84  k->rhs=old_rhs;
85 
86  bool r2=alloc_and_compute_row_means(k, ktest_row_means, num_lhs,num_rhs);
87 
88  /* compute train kernel matrix mean */
89  ktrain_mean=0;
90  for (int32_t i=0;i<num_lhs;i++)
91  ktrain_mean += (ktrain_row_means[i]/num_lhs);
92 
93  k->lhs=old_lhs;
94  k->rhs=old_rhs;
95 
96  return r1 && r2;
97  }
98 
105  float64_t value, int32_t idx_lhs, int32_t idx_rhs)
106  {
107  value += (-ktrain_row_means[idx_lhs] - ktest_row_means[idx_rhs] + ktrain_mean);
108  return value;
109  }
110 
115  virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
116  {
117  SG_ERROR("normalize_lhs not implemented")
118  return 0;
119  }
120 
125  virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
126  {
127  SG_ERROR("normalize_rhs not implemented")
128  return 0;
129  }
130 
135  bool alloc_and_compute_row_means(CKernel* k, float64_t* &v, int32_t num_lhs, int32_t num_rhs)
136  {
137  SG_FREE(v);
138  v=SG_MALLOC(float64_t, num_rhs);
139 
140  for (int32_t i=0; i<num_rhs; i++)
141  {
142  v[i]=0;
143  for (int32_t j=0; j<num_lhs; j++)
144  v[i] += ( k->compute(j,i)/num_lhs );
145  }
146  return (v!=NULL);
147  }
148 
150  virtual const char* get_name() const { return "ZeroMeanCenterKernelNormalizer"; }
151 
152  protected:
155 
157  int32_t num_ktrain;
158 
161 
163  int32_t num_ktest;
164 
167 };
168 }
169 #endif

SHOGUN Machine Learning Toolbox - Documentation