SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
14 #include <shogun/lib/config.h>
15 
18 
19 namespace shogun
20 {
32 {
33  public:
38  CSqrtDiagKernelNormalizer(bool use_opt_diag=false): CKernelNormalizer(),
42  {
44  "sqrt(K(x,x)) for left hand side examples.");
46  "sqrt(K(x,x)) for right hand side examples.");
48  "use_optimized_diagonal_computation",
49  "flat if optimized diagonal computation is used", MS_NOT_AVAILABLE);
50  }
51 
54  {
55  SG_FREE(sqrtdiag_lhs);
56  SG_FREE(sqrtdiag_rhs);
57  }
58 
61  virtual bool init(CKernel* k)
62  {
63  ASSERT(k)
68 
69  CFeatures* old_lhs=k->lhs;
70  CFeatures* old_rhs=k->rhs;
71 
72  k->lhs=old_lhs;
73  k->rhs=old_lhs;
75 
76  k->lhs=old_rhs;
77  k->rhs=old_rhs;
79 
80  k->lhs=old_lhs;
81  k->rhs=old_rhs;
82 
83  return r1 && r2;
84  }
85 
92  float64_t value, int32_t idx_lhs, int32_t idx_rhs)
93  {
94  float64_t sqrt_both=sqrtdiag_lhs[idx_lhs]*sqrtdiag_rhs[idx_rhs];
95  return value/sqrt_both;
96  }
97 
102  virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
103  {
104  return value/sqrtdiag_lhs[idx_lhs];
105  }
106 
111  virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
112  {
113  return value/sqrtdiag_rhs[idx_rhs];
114  }
115 
116  public:
121  bool alloc_and_compute_diag(CKernel* k, float64_t* &v, int32_t num)
122  {
123  SG_FREE(v);
124  v=SG_MALLOC(float64_t, num);
125 
126  for (int32_t i=0; i<num; i++)
127  {
128  if (k->get_kernel_type() == K_COMMWORDSTRING)
129  {
131  v[i]=sqrt(((CCommWordStringKernel*) k)->compute_diag(i));
132  else
133  v[i]=sqrt(((CCommWordStringKernel*) k)->compute_helper(i,i, true));
134  }
135  else
136  v[i]=sqrt(k->compute(i,i));
137 
138  if (v[i]==0.0)
139  v[i]=1e-16; /* avoid divide by zero exception */
140  }
141 
142  return (v!=NULL);
143  }
144 
146  virtual const char* get_name() const { return "SqrtDiagKernelNormalizer"; }
147 
148  protected:
151 
154 
157 
160 
163 };
164 }
165 #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:546
virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:517
virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
#define ASSERT(x)
Definition: SGIO.h:201
The CommWordString kernel may be used to compute the spectrum kernel from strings that have been mapp...
double float64_t
Definition: common.h:50
The class Kernel Normalizer defines a function to post-process kernel values.
virtual int32_t get_num_vec_rhs()
Definition: Kernel.h:526
virtual const char * get_name() const
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1062
void add_vector(bool **param, index_t *length, const char *name, const char *description="")
Definition: Parameter.cpp:334
SqrtDiagKernelNormalizer divides by the Square Root of the product of the diagonal elements...
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
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:1060
The class Features is the base class of all feature objects.
Definition: Features.h:68
CSqrtDiagKernelNormalizer(bool use_opt_diag=false)
The Kernel base class.
Definition: Kernel.h:159
#define SG_ADD(...)
Definition: SGObject.h:84

SHOGUN Machine Learning Toolbox - Documentation