SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GaussianKernel.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-2010 Soeren Sonnenburg
8  * Written (W) 2011 Abhinav Maurya
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  * Copyright (C) 2010 Berlin Institute of Technology
11  */
12 
13 #include <shogun/lib/common.h>
14 #include <shogun/base/Parameter.h>
18 #include <shogun/io/SGIO.h>
19 
20 using namespace shogun;
21 
23  : CDotKernel()
24 {
25  init();
26 }
27 
29 : CDotKernel(size)
30 {
31  init();
32  set_width(w);
33 }
34 
36  CDotFeatures* l, CDotFeatures* r, float64_t w, int32_t size)
37 : CDotKernel(size)
38 {
39  init();
40  set_width(w);
41  init(l,r);
42 }
43 
45 {
46  cleanup();
47 }
48 
50 {
51  if (sq_lhs != sq_rhs)
52  SG_FREE(sq_rhs);
53  sq_rhs = NULL;
54 
55  SG_FREE(sq_lhs);
56  sq_lhs = NULL;
57 
59 }
60 
61 void CGaussianKernel::precompute_squared_helper(float64_t* &buf, CDotFeatures* df)
62 {
63  ASSERT(df);
64  int32_t num_vec=df->get_num_vectors();
65  buf=SG_MALLOC(float64_t, num_vec);
66 
67  for (int32_t i=0; i<num_vec; i++)
68  buf[i]=df->dot(i,df, i);
69 }
70 
71 bool CGaussianKernel::init(CFeatures* l, CFeatures* r)
72 {
74  cleanup();
75 
76  CDotKernel::init(l, r);
77  precompute_squared();
78  return init_normalizer();
79 }
80 
81 float64_t CGaussianKernel::compute(int32_t idx_a, int32_t idx_b)
82 {
83  if (!m_compact)
84  {
85  float64_t result=sq_lhs[idx_a]+sq_rhs[idx_b]
86  -2*CDotKernel::compute(idx_a, idx_b);
87  return CMath::exp(-result/width);
88  }
89 
90  int32_t len_features, power;
91  len_features=((CSimpleFeatures<float64_t>*) lhs)->get_num_features();
92  power=(len_features%2==0) ? (len_features+1):len_features;
93 
94  float64_t result=sq_lhs[idx_a]+sq_rhs[idx_b]-2*CDotKernel::compute(idx_a,idx_b);
95  float64_t result_multiplier=1-(sqrt(result/width))/3;
96 
97  if (result_multiplier<=0)
98  result_multiplier=0;
99  else
100  result_multiplier=pow(result_multiplier, power);
101 
102  return result_multiplier*exp(-result/width);
103 }
104 
106 {
108  precompute_squared();
109 }
110 
111 void CGaussianKernel::precompute_squared()
112 {
113  if (!lhs || !rhs)
114  return;
115 
116  precompute_squared_helper(sq_lhs, (CDotFeatures*) lhs);
117 
118  if (lhs==rhs)
119  sq_rhs=sq_lhs;
120  else
121  precompute_squared_helper(sq_rhs, (CDotFeatures*) rhs);
122 }
123 
124 void CGaussianKernel::init()
125 {
126  set_width(1.0);
127  set_compact_enabled(false);
128  sq_lhs=NULL;
129  sq_rhs=NULL;
130  SG_ADD(&width, "width", "Kernel width.", MS_AVAILABLE);
131  SG_ADD(&m_compact, "compact", "Compact Enabled Option.", MS_AVAILABLE);
132 }
virtual void load_serializable_post()
Definition: Kernel.cpp:384
void set_compact_enabled(bool compact)
virtual void cleanup()
Definition: Kernel.cpp:138
virtual void load_serializable_post()
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
virtual float64_t dot(int32_t vec_idx1, CDotFeatures *df, int32_t vec_idx2)=0
virtual int32_t get_num_vectors() const =0
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
Definition: DotKernel.h:121
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
Features that support dot products among other operations.
Definition: DotFeatures.h:41
Template class DotKernel is the base class for kernels working on DotFeatures.
Definition: DotKernel.h:29
#define ASSERT(x)
Definition: SGIO.h:102
double float64_t
Definition: common.h:56
#define SG_ADD(param, name, description, ms_available)
Definition: SGObject.h:52
#define SG_FREE(ptr)
Definition: memory.h:39
virtual bool init_normalizer()
Definition: Kernel.cpp:133
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:810
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:808
The class Features is the base class of all feature objects.
Definition: Features.h:56
static float64_t exp(float64_t x)
Definition: Math.h:368
virtual bool init(CFeatures *l, CFeatures *r)
#define SG_MALLOC(type, len)
Definition: memory.h:36
virtual void set_width(float64_t w)

SHOGUN Machine Learning Toolbox - Documentation