SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
14 #include <shogun/lib/config.h>
15 
17 
18 namespace shogun
19 {
46 {
47  public:
60  {
61  SG_ADD(&scale, "scale", "Scale quotient by which kernel is scaled.",
62  MS_AVAILABLE);
63  SG_ADD(&ridge, "ridge", "Ridge added to diagonal.", MS_AVAILABLE);
64 
65  scale=c;
66  ridge=r;
67  }
68 
71  {
72  }
73 
76  virtual bool init(CKernel* k)
77  {
78  if (scale<=0)
79  {
80  ASSERT(k)
81  int32_t num=k->get_num_vec_lhs();
82  ASSERT(num>0)
83 
84  CFeatures* old_lhs=k->lhs;
85  CFeatures* old_rhs=k->rhs;
86  k->lhs=old_lhs;
87  k->rhs=old_lhs;
88 
89  float64_t sum=0;
90  for (int32_t i=0; i<num; i++)
91  sum+=k->compute(i, i);
92 
93  scale=sum/num;
94  k->lhs=old_lhs;
95  k->rhs=old_rhs;
96  }
97 
98  ridge*=scale;
99  return true;
100  }
101 
108  float64_t value, int32_t idx_lhs, int32_t idx_rhs)
109  {
110  if (idx_lhs==idx_rhs)
111  return value+ridge;
112  else
113  return value;
114  }
115 
120  virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
121  {
122  SG_ERROR("linadd not supported with Ridge normalization.\n")
123  return 0;
124  }
125 
130  virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
131  {
132  SG_ERROR("linadd not supported with Ridge normalization.\n")
133  return 0;
134  }
135 
137  virtual const char* get_name() const { return "RidgeKernelNormalizer"; }
138 
139  protected:
144 };
145 }
146 #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:129
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:517
float64_t ridge
the constant ridge to be added to the kernel diagonal
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
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:1062
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)
#define SG_ADD(...)
Definition: SGObject.h:84
virtual const char * get_name() const

SHOGUN Machine Learning Toolbox - Documentation