SHOGUN  v3.0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SqrtDiagKernelNormalizer.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 _SQRTDIAGKERNELNORMALIZER_H___
12 #define _SQRTDIAGKERNELNORMALIZER_H___
13 
16 
17 namespace shogun
18 {
30 {
31  public:
36  CSqrtDiagKernelNormalizer(bool use_opt_diag=false): CKernelNormalizer(),
40  {
42  "sqrt(K(x,x)) for left hand side examples.");
44  "sqrt(K(x,x)) for right hand side examples.");
46  "use_optimized_diagonal_computation",
47  "flat if optimized diagonal computation is used", MS_NOT_AVAILABLE);
48  }
49 
52  {
53  SG_FREE(sqrtdiag_lhs);
54  SG_FREE(sqrtdiag_rhs);
55  }
56 
59  virtual bool init(CKernel* k)
60  {
61  ASSERT(k)
66 
67  CFeatures* old_lhs=k->lhs;
68  CFeatures* old_rhs=k->rhs;
69 
70  k->lhs=old_lhs;
71  k->rhs=old_lhs;
73 
74  k->lhs=old_rhs;
75  k->rhs=old_rhs;
77 
78  k->lhs=old_lhs;
79  k->rhs=old_rhs;
80 
81  return r1 && r2;
82  }
83 
90  float64_t value, int32_t idx_lhs, int32_t idx_rhs)
91  {
92  float64_t sqrt_both=sqrtdiag_lhs[idx_lhs]*sqrtdiag_rhs[idx_rhs];
93  return value/sqrt_both;
94  }
95 
100  virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
101  {
102  return value/sqrtdiag_lhs[idx_lhs];
103  }
104 
109  virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
110  {
111  return value/sqrtdiag_rhs[idx_rhs];
112  }
113 
114  public:
119  bool alloc_and_compute_diag(CKernel* k, float64_t* &v, int32_t num)
120  {
121  SG_FREE(v);
122  v=SG_MALLOC(float64_t, num);
123 
124  for (int32_t i=0; i<num; i++)
125  {
126  if (k->get_kernel_type() == K_COMMWORDSTRING)
127  {
129  v[i]=sqrt(((CCommWordStringKernel*) k)->compute_diag(i));
130  else
131  v[i]=sqrt(((CCommWordStringKernel*) k)->compute_helper(i,i, true));
132  }
133  else
134  v[i]=sqrt(k->compute(i,i));
135 
136  if (v[i]==0.0)
137  v[i]=1e-16; /* avoid divide by zero exception */
138  }
139 
140  return (v!=NULL);
141  }
142 
144  virtual const char* get_name() const { return "SqrtDiagKernelNormalizer"; }
145 
146  protected:
149 
152 
155 
158 
161 };
162 }
163 #endif
virtual float64_t compute(int32_t x, int32_t y)=0
bool alloc_and_compute_diag(CKernel *k, float64_t *&v, int32_t num)
Parameter * m_parameters
Definition: SGObject.h:522
virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:355
virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
#define ASSERT(x)
Definition: SGIO.h:203
The CommWordString kernel may be used to compute the spectrum kernel from strings that have been mapp...
double float64_t
Definition: common.h:48
The class Kernel Normalizer defines a function to post-process kernel values.
virtual int32_t get_num_vec_rhs()
Definition: Kernel.h:364
virtual const char * get_name() const
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:706
void add_vector(bool **param, index_t *length, const char *name, const char *description="")
Definition: Parameter.cpp:324
SqrtDiagKernelNormalizer divides by the Square Root of the product of the diagonal elements...
virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
virtual EKernelType get_kernel_type()=0
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:704
The class Features is the base class of all feature objects.
Definition: Features.h:62
CSqrtDiagKernelNormalizer(bool use_opt_diag=false)
The Kernel base class.
Definition: Kernel.h:150
#define SG_ADD(...)
Definition: SGObject.h:83

SHOGUN Machine Learning Toolbox - Documentation