SHOGUN  3.2.1
 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 
14 #include <shogun/lib/config.h>
15 
18 
19 namespace shogun
20 {
29 {
30  public:
35  CDiceKernelNormalizer(bool use_opt_diag=false) : CKernelNormalizer(),
36  diag_lhs(NULL), num_diag_lhs(0), diag_rhs(NULL), num_diag_rhs(0),
38  {
40  "K(x,x) for left hand side examples.");
42  "K(x,x) for right hand side examples.");
44  "use_optimized_diagonal_computation",
45  "flat if optimized diagonal computation is used", MS_NOT_AVAILABLE);
46  }
47 
50  {
51  SG_FREE(diag_lhs);
52  SG_FREE(diag_rhs);
53  }
54 
57  virtual bool init(CKernel* k)
58  {
59  ASSERT(k)
64 
65  CFeatures* old_lhs=k->lhs;
66  CFeatures* old_rhs=k->rhs;
67 
68  k->lhs=old_lhs;
69  k->rhs=old_lhs;
71 
72  k->lhs=old_rhs;
73  k->rhs=old_rhs;
75 
76  k->lhs=old_lhs;
77  k->rhs=old_rhs;
78 
79  return r1 && r2;
80  }
81 
88  float64_t value, int32_t idx_lhs, int32_t idx_rhs)
89  {
90  float64_t diag_sum=diag_lhs[idx_lhs]*diag_rhs[idx_rhs];
91  return 2*value/diag_sum;
92  }
93 
98  virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
99  {
100  SG_ERROR("linadd not supported with Dice normalization.\n")
101  return 0;
102  }
103 
108  virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
109  {
110  SG_ERROR("linadd not supported with Dice normalization.\n")
111  return 0;
112  }
113 
119  virtual const char* get_name() const {
120  return "DiceKernelNormalizer"; }
121 
122  public:
127  bool alloc_and_compute_diag(CKernel* k, float64_t* &v, int32_t num)
128  {
129  SG_FREE(v);
130  v=SG_MALLOC(float64_t, num);
131 
132  for (int32_t i=0; i<num; i++)
133  {
134  if (k->get_kernel_type() == K_COMMWORDSTRING)
135  {
137  v[i]=((CCommWordStringKernel*) k)->compute_diag(i);
138  else
139  v[i]=((CCommWordStringKernel*) k)->compute_helper(i,i, true);
140  }
141  else
142  v[i]=k->compute(i,i);
143 
144  if (v[i]==0.0)
145  v[i]=1e-16; /* avoid divide by zero exception */
146  }
147 
148  return (v!=NULL);
149  }
150 
151  protected:
155  int32_t num_diag_lhs;
156 
160  int32_t num_diag_rhs;
161 
164 };
165 }
166 #endif

SHOGUN Machine Learning Toolbox - Documentation