SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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  m_parameters->add(&scale, "scale",
60  "Scale quotient by which kernel is scaled.");
61  m_parameters->add(&ridge, "ridge", "Ridge added to diagonal.");
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___
CRidgeKernelNormalizer(float64_t r=1e-10, float64_t c=0.0)
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:75
Parameter * m_parameters
Definition: SGObject.h:297
float64_t scale
scaling parameter (avg of diagonal)
virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:437
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:23
float64_t ridge
the constant ridge to be added to the kernel diagonal
#define ASSERT(x)
Definition: SGIO.h:102
double float64_t
Definition: common.h:56
Normalize the kernel by adding a constant term to its diagonal. This aids kernels to become positive ...
The class Kernel Normalizer defines a function to post-process kernel values.
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:810
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:808
The class Features is the base class of all feature objects.
Definition: Features.h:56
The Kernel base class.
Definition: Kernel.h:172
virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
virtual const char * get_name() const

SHOGUN Machine Learning Toolbox - Documentation