SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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
virtual float64_t compute(int32_t x, int32_t y)=0
DiceKernelNormalizer performs kernel normalization inspired by the Dice coefficient (see http://en...
#define SG_ERROR(...)
Definition: SGIO.h:129
Parameter * m_parameters
Definition: SGObject.h:546
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:517
#define ASSERT(x)
Definition: SGIO.h:201
The CommWordString kernel may be used to compute the spectrum kernel from strings that have been mapp...
CDiceKernelNormalizer(bool use_opt_diag=false)
double float64_t
Definition: common.h:50
The class Kernel Normalizer defines a function to post-process kernel values.
bool alloc_and_compute_diag(CKernel *k, float64_t *&v, int32_t num)
virtual int32_t get_num_vec_rhs()
Definition: Kernel.h:526
virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
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
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
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
virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
The Kernel base class.
Definition: Kernel.h:159
virtual const char * get_name() const
#define SG_ADD(...)
Definition: SGObject.h:84
virtual bool init(CKernel *k)
virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)

SHOGUN Machine Learning Toolbox - Documentation