SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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  * Written (W) 2012 Heiko Strathmann
10  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
11  * Copyright (C) 2010 Berlin Institute of Technology
12  */
13 
14 #include <shogun/lib/common.h>
15 #include <shogun/base/Parameter.h>
18 #include <shogun/io/SGIO.h>
20 
21 using namespace shogun;
22 
24 {
25  REQUIRE(w>0, "width (%f) must be positive\n",w);
26  m_log_width=CMath::log(w/2.0)/2.0;
27 }
28 
30 {
31  return CMath::exp(m_log_width*2.0)*2.0;
32 }
33 
35 {
36  init();
37 }
38 
40 {
41  init();
42  set_width(w);
43 }
44 
46  float64_t w, int32_t size) : CDotKernel(size)
47 {
48  init();
49  set_width(w);
50  init(l,r);
51 }
52 
54 {
55  cleanup();
56 }
57 
59 {
60  if (kernel->get_kernel_type()!=K_GAUSSIAN)
61  {
62  SG_SERROR("CGaussianKernel::obtain_from_generic(): provided kernel is "
63  "not of type CGaussianKernel!\n");
64  }
65 
66  /* since an additional reference is returned */
67  SG_REF(kernel);
68  return (CGaussianKernel*)kernel;
69 }
70 
71 #include <typeinfo>
73 {
74  // TODO: remove this after all the classes get shallow_copy properly implemented
75  // this assert is to avoid any subclass of CGaussianKernel accidentally called
76  // with the implement here
77  ASSERT(typeid(*this) == typeid(CGaussianKernel))
79  if (lhs)
80  {
81  ker->init(lhs, rhs);
82  }
83  return ker;
84 }
85 
87 {
88  if (sq_lhs != sq_rhs)
89  SG_FREE(sq_rhs);
90  sq_rhs = NULL;
91 
92  SG_FREE(sq_lhs);
93  sq_lhs = NULL;
94 
96 }
97 
98 void CGaussianKernel::precompute_squared_helper(float64_t* &buf, CDotFeatures* df)
99 {
100  ASSERT(df)
101  int32_t num_vec=df->get_num_vectors();
102  buf=SG_MALLOC(float64_t, num_vec);
103 
104  for (int32_t i=0; i<num_vec; i++)
105  buf[i]=df->dot(i,df, i);
106 }
107 
109 {
111  cleanup();
112 
113  CDotKernel::init(l, r);
114  precompute_squared();
115  return init_normalizer();
116 }
117 
118 float64_t CGaussianKernel::compute(int32_t idx_a, int32_t idx_b)
119 {
120  if (!m_compact)
121  {
122  float64_t result=distance(idx_a,idx_b);
123  return CMath::exp(-result);
124  }
125 
126  int32_t len_features, power;
127  len_features=((CDotFeatures*) lhs)->get_dim_feature_space();
128  power=(len_features%2==0) ? (len_features+1):len_features;
129 
130  float64_t result=distance(idx_a,idx_b);
131  float64_t result_multiplier=1-(sqrt(result))/3;
132 
133  if (result_multiplier<=0)
134  result_multiplier=0;
135  else
136  result_multiplier=pow(result_multiplier, power);
137 
138  return result_multiplier*exp(-result);
139 }
140 
142 {
144  precompute_squared();
145 }
146 
147 void CGaussianKernel::precompute_squared()
148 {
149  if (!lhs || !rhs)
150  return;
151 
152  precompute_squared_helper(sq_lhs, (CDotFeatures*) lhs);
153 
154  if (lhs==rhs)
155  sq_rhs=sq_lhs;
156  else
157  precompute_squared_helper(sq_rhs, (CDotFeatures*) rhs);
158 }
159 
161  const TParameter* param, index_t index)
162 {
163  REQUIRE(lhs && rhs, "Features not set!\n")
164 
165  if (!strcmp(param->m_name, "log_width"))
166  {
168 
169  for (int j=0; j<num_lhs; j++)
170  for (int k=0; k<num_rhs; k++)
171  {
172  float64_t element=distance(j,k);
173  derivative(j,k)=exp(-element)*element*2.0;
174  }
175 
176  return derivative;
177  }
178  else
179  {
180  SG_ERROR("Can't compute derivative wrt %s parameter\n", param->m_name);
181  return SGMatrix<float64_t>();
182  }
183 }
184 
185 void CGaussianKernel::init()
186 {
187  set_width(1.0);
188  set_compact_enabled(false);
189  sq_lhs=NULL;
190  sq_rhs=NULL;
191  SG_ADD(&m_log_width, "log_width", "Kernel width in log domain", MS_AVAILABLE, GRADIENT_AVAILABLE);
192  SG_ADD(&m_compact, "compact", "Compact enabled option", MS_AVAILABLE);
193 }
194 
195 float64_t CGaussianKernel::distance(int32_t idx_a, int32_t idx_b)
196 {
197  return (sq_lhs[idx_a]+sq_rhs[idx_b]-2*CDotKernel::compute(idx_a,idx_b))/get_width();
198 }
virtual void load_serializable_post()
Definition: Kernel.cpp:917
void set_compact_enabled(bool compact)
virtual void cleanup()
Definition: Kernel.cpp:162
int32_t index_t
Definition: common.h:62
int32_t num_rhs
number of feature vectors on right hand side
Definition: Kernel.h:1069
Vector::Scalar dot(Vector a, Vector b)
Definition: Redux.h:56
virtual void load_serializable_post()
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
virtual float64_t distance(int32_t idx_a, int32_t idx_b)
parameter struct
#define SG_ERROR(...)
Definition: SGIO.h:129
#define REQUIRE(x,...)
Definition: SGIO.h:206
float64_t kernel(int32_t idx_a, int32_t idx_b)
Definition: Kernel.h:206
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
Definition: DotKernel.h:123
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
Features that support dot products among other operations.
Definition: DotFeatures.h:44
#define SG_REF(x)
Definition: SGObject.h:51
int32_t cache_size
cache_size in MB
Definition: Kernel.h:1047
Template class DotKernel is the base class for kernels working on DotFeatures.
Definition: DotKernel.h:31
#define ASSERT(x)
Definition: SGIO.h:201
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
virtual SGMatrix< float64_t > get_parameter_gradient(const TParameter *param, index_t index=-1)
double float64_t
Definition: common.h:50
int32_t num_lhs
number of feature vectors on left hand side
Definition: Kernel.h:1067
virtual CSGObject * shallow_copy() const
The well known Gaussian kernel (swiss army knife for SVMs) computed on CDotFeatures.
virtual bool init_normalizer()
Definition: Kernel.cpp:157
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1061
static CGaussianKernel * obtain_from_generic(CKernel *kernel)
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:1059
The class Features is the base class of all feature objects.
Definition: Features.h:68
#define SG_SERROR(...)
Definition: SGIO.h:179
static float64_t exp(float64_t x)
Definition: Math.h:621
static float64_t log(float64_t v)
Definition: Math.h:922
The Kernel base class.
Definition: Kernel.h:158
virtual bool init(CFeatures *l, CFeatures *r)
#define SG_ADD(...)
Definition: SGObject.h:81
virtual void set_width(float64_t w)
virtual float64_t get_width() const

SHOGUN Machine Learning Toolbox - Documentation