SHOGUN  v2.0.0
 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  {
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 
89  inline virtual float64_t normalize(
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  inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
101  {
102  return value/sqrtdiag_lhs[idx_lhs];
103  }
104 
109  inline 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  inline virtual const char* get_name() const { return "SqrtDiagKernelNormalizer"; }
145 
146  protected:
149 
152 
155 
158 
161 };
162 }
163 #endif

SHOGUN Machine Learning Toolbox - Documentation