SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RidgeKernelNormalizer.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 Soeren Sonnenburg
8  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #ifndef _RIDGEKERNELNORMALIZER_H___
12 #define _RIDGEKERNELNORMALIZER_H___
13 
15 
16 namespace shogun
17 {
44 {
45  public:
58  {
59  SG_ADD(&scale, "scale", "Scale quotient by which kernel is scaled.",
60  MS_AVAILABLE);
61  SG_ADD(&ridge, "ridge", "Ridge added to diagonal.", MS_AVAILABLE);
62 
63  scale=c;
64  ridge=r;
65  }
66 
69  {
70  }
71 
74  virtual bool init(CKernel* k)
75  {
76  if (scale<=0)
77  {
78  ASSERT(k);
79  int32_t num=k->get_num_vec_lhs();
80  ASSERT(num>0);
81 
82  CFeatures* old_lhs=k->lhs;
83  CFeatures* old_rhs=k->rhs;
84  k->lhs=old_lhs;
85  k->rhs=old_lhs;
86 
87  float64_t sum=0;
88  for (int32_t i=0; i<num; i++)
89  sum+=k->compute(i, i);
90 
91  scale=sum/num;
92  k->lhs=old_lhs;
93  k->rhs=old_rhs;
94  }
95 
96  ridge*=scale;
97  return true;
98  }
99 
105  inline virtual float64_t normalize(
106  float64_t value, int32_t idx_lhs, int32_t idx_rhs)
107  {
108  if (idx_lhs==idx_rhs)
109  return value+ridge;
110  else
111  return value;
112  }
113 
118  inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
119  {
120  SG_ERROR("linadd not supported with Ridge normalization.\n");
121  return 0;
122  }
123 
128  inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
129  {
130  SG_ERROR("linadd not supported with Ridge normalization.\n");
131  return 0;
132  }
133 
135  inline virtual const char* get_name() const { return "RidgeKernelNormalizer"; }
136 
137  protected:
142 };
143 }
144 #endif // _RIDGEKERNELNORMALIZER_H___

SHOGUN Machine Learning Toolbox - Documentation