SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DiceKernelNormalizer.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 _DICEKERNELNORMALIZER_H___
12 #define _DICEKERNELNORMALIZER_H___
13 
16 
17 namespace shogun
18 {
27 {
28  public:
33  CDiceKernelNormalizer(bool use_opt_diag=false) : CKernelNormalizer(),
34  diag_lhs(NULL), num_diag_lhs(0), diag_rhs(NULL), num_diag_rhs(0),
36  {
38  "K(x,x) for left hand side examples.");
40  "K(x,x) for right hand side examples.");
42  "use_optimized_diagonal_computation",
43  "flat if optimized diagonal computation is used", MS_NOT_AVAILABLE);
44  }
45 
48  {
51  }
52 
55  virtual bool init(CKernel* k)
56  {
57  ASSERT(k);
62 
63  CFeatures* old_lhs=k->lhs;
64  CFeatures* old_rhs=k->rhs;
65 
66  k->lhs=old_lhs;
67  k->rhs=old_lhs;
69 
70  k->lhs=old_rhs;
71  k->rhs=old_rhs;
73 
74  k->lhs=old_lhs;
75  k->rhs=old_rhs;
76 
77  return r1 && r2;
78  }
79 
85  inline virtual float64_t normalize(
86  float64_t value, int32_t idx_lhs, int32_t idx_rhs)
87  {
88  float64_t diag_sum=diag_lhs[idx_lhs]*diag_rhs[idx_rhs];
89  return 2*value/diag_sum;
90  }
91 
96  inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
97  {
98  SG_ERROR("linadd not supported with Dice normalization.\n");
99  return 0;
100  }
101 
106  inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
107  {
108  SG_ERROR("linadd not supported with Dice normalization.\n");
109  return 0;
110  }
111 
117  virtual const char* get_name() const {
118  return "DiceKernelNormalizer"; }
119 
120  public:
125  bool alloc_and_compute_diag(CKernel* k, float64_t* &v, int32_t num)
126  {
127  SG_FREE(v);
128  v=SG_MALLOC(float64_t, num);
129 
130  for (int32_t i=0; i<num; i++)
131  {
132  if (k->get_kernel_type() == K_COMMWORDSTRING)
133  {
135  v[i]=((CCommWordStringKernel*) k)->compute_diag(i);
136  else
137  v[i]=((CCommWordStringKernel*) k)->compute_helper(i,i, true);
138  }
139  else
140  v[i]=k->compute(i,i);
141 
142  if (v[i]==0.0)
143  v[i]=1e-16; /* avoid divide by zero exception */
144  }
145 
146  return (v!=NULL);
147  }
148 
149  protected:
153  int32_t num_diag_lhs;
154 
158  int32_t num_diag_rhs;
159 
162 };
163 }
164 #endif

SHOGUN Machine Learning Toolbox - Documentation