SHOGUN  3.2.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 
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

SHOGUN Machine Learning Toolbox - Documentation