SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Chi2Kernel.cpp
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) 1999-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/lib/common.h>
14 #include <shogun/io/SGIO.h>
15 
16 using namespace shogun;
17 
18 void
19 CChi2Kernel::init()
20 {
21  SG_ADD(&width, "width", "Kernel width.", MS_AVAILABLE);
22 }
23 
25 : CDotKernel(0), width(0)
26 {
27  init();
28 }
29 
31 : CDotKernel(size), width(w)
32 {
33  init();
34 }
35 
38 : CDotKernel(size), width(w)
39 {
40  init();
41  init(l,r);
42 }
43 
45 {
46  cleanup();
47 }
48 
49 bool CChi2Kernel::init(CFeatures* l, CFeatures* r)
50 {
51  bool result=CDotKernel::init(l,r);
53  return result;
54 }
55 
56 float64_t CChi2Kernel::compute(int32_t idx_a, int32_t idx_b)
57 {
58  int32_t alen, blen;
59  bool afree, bfree;
60 
61  float64_t* avec=
62  ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
63  float64_t* bvec=
64  ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
65  ASSERT(alen==blen)
66 
67  float64_t result=0;
68  for (int32_t i=0; i<alen; i++)
69  {
70  float64_t n=avec[i]-bvec[i];
71  float64_t d=avec[i]+bvec[i];
72  if (d!=0)
73  result+=n*n/d;
74  }
75 
76  result=exp(-result/width);
77 
78  ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
79  ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
80 
81  return result;
82 }
83 
85 {
86  return width;
87 }
88 
90 {
91  if (kernel->get_kernel_type()!=K_CHI2)
92  {
93  SG_SERROR("Provided kernel is "
94  "not of type CChi2Kernel!\n");
95  }
96 
97  /* since an additional reference is returned */
98  SG_REF(kernel);
99  return (CChi2Kernel*)kernel;
100 }
static CChi2Kernel * obtain_from_generic(CKernel *kernel)
Definition: Chi2Kernel.cpp:89
virtual void cleanup()
Definition: Kernel.cpp:173
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
Definition: Chi2Kernel.cpp:56
float64_t kernel(int32_t idx_a, int32_t idx_b)
Definition: Kernel.h:207
#define SG_REF(x)
Definition: SGObject.h:54
The Chi2 kernel operating on realvalued vectors computes the chi-squared distance between sets of his...
Definition: Chi2Kernel.h:34
virtual float64_t get_width()
Definition: Chi2Kernel.cpp:84
Template class DotKernel is the base class for kernels working on DotFeatures.
Definition: DotKernel.h:31
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
virtual bool init_normalizer()
Definition: Kernel.cpp:168
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1062
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
#define SG_SERROR(...)
Definition: SGIO.h:179
The Kernel base class.
Definition: Kernel.h:159
virtual ~CChi2Kernel()
Definition: Chi2Kernel.cpp:44
#define SG_ADD(...)
Definition: SGObject.h:84

SHOGUN Machine Learning Toolbox - Documentation