SHOGUN  4.2.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  float64_t result=distance(idx_a,idx_b);
121  return CMath::exp(-result);
122 }
123 
125 {
127  precompute_squared();
128 }
129 
130 void CGaussianKernel::precompute_squared()
131 {
132  if (!lhs || !rhs)
133  return;
134 
135  precompute_squared_helper(sq_lhs, (CDotFeatures*) lhs);
136 
137  if (lhs==rhs)
138  sq_rhs=sq_lhs;
139  else
140  precompute_squared_helper(sq_rhs, (CDotFeatures*) rhs);
141 }
142 
144  const TParameter* param, index_t index)
145 {
146  REQUIRE(lhs && rhs, "Features not set!\n")
147 
148  if (!strcmp(param->m_name, "log_width"))
149  {
151 
152  for (int j=0; j<num_lhs; j++)
153  for (int k=0; k<num_rhs; k++)
154  {
155  float64_t element=distance(j,k);
156  derivative(j,k)=exp(-element)*element*2.0;
157  }
158 
159  return derivative;
160  }
161  else
162  {
163  SG_ERROR("Can't compute derivative wrt %s parameter\n", param->m_name);
164  return SGMatrix<float64_t>();
165  }
166 }
167 
168 void CGaussianKernel::init()
169 {
170  set_width(1.0);
171  sq_lhs=NULL;
172  sq_rhs=NULL;
173  SG_ADD(&m_log_width, "log_width", "Kernel width in log domain", MS_AVAILABLE, GRADIENT_AVAILABLE);
174 }
175 
176 float64_t CGaussianKernel::distance(int32_t idx_a, int32_t idx_b)
177 {
178  return (sq_lhs[idx_a]+sq_rhs[idx_b]-2*CDotKernel::compute(idx_a,idx_b))/get_width();
179 }
virtual void load_serializable_post()
Definition: Kernel.cpp:929
virtual void cleanup()
Definition: Kernel.cpp:173
int32_t index_t
Definition: common.h:62
int32_t num_rhs
number of feature vectors on right hand side
Definition: Kernel.h:1070
Vector::Scalar dot(Vector a, Vector b)
Definition: Redux.h:58
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:207
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
Definition: DotKernel.h:134
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:1048
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:1068
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:168
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1062
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:1060
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:159
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