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  * Written (W) 2016 Soumyajit De
11  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
12  * Copyright (C) 2010 Berlin Institute of Technology
13  */
14 
15 #include <shogun/lib/common.h>
20 
21 using namespace shogun;
22 
24 {
25  register_params();
26 }
27 
29 {
30  register_params();
31  set_width(w);
32 }
33 
35 {
36  register_params();
37  set_width(w);
38  set_cache_size(size);
39 }
40 
42 {
43  register_params();
44  set_width(w);
45  set_cache_size(size);
46  init(l, r);
47 }
48 
50 {
51  cleanup();
52 }
53 
55 {
57  "Provided kernel (%s) must be of type CGaussianKernel!\n", kernel->get_name());
58 
59  SG_REF(kernel);
60  return (CGaussianKernel*)kernel;
61 }
62 
63 #include <typeinfo>
65 {
66  // TODO: remove this after all the classes get shallow_copy properly implemented
67  // this assert is to avoid any subclass of CGaussianKernel accidentally called
68  // with the implement here
69  ASSERT(typeid(*this) == typeid(CGaussianKernel))
71  if (lhs && rhs)
72  {
73  ker->init(lhs, rhs);
74  ker->m_distance->init(lhs, rhs);
75  }
76  return ker;
77 }
78 
80 {
83 }
84 
85 bool CGaussianKernel::init(CFeatures* l, CFeatures* r)
86 {
87  cleanup();
88  CShiftInvariantKernel::init(l, r);
89  m_distance->init(l, r);
90  return init_normalizer();
91 }
92 
94 {
95  REQUIRE(w>0, "width (%f) must be positive\n",w);
96  m_log_width=CMath::log(w/2.0)/2.0;
97 }
98 
100 {
101  return CMath::exp(m_log_width*2.0)*2.0;
102 }
103 
105 {
106  REQUIRE(lhs, "Left hand side features must be set!\n")
107  REQUIRE(rhs, "Rightt hand side features must be set!\n")
108 
109  if (!strcmp(param->m_name, "log_width"))
110  {
112  for (int k=0; k<num_rhs; k++)
113  {
114 #pragma omp parallel for
115  for (int j=0; j<num_lhs; j++)
116  {
117  float64_t element=distance(j, k);
118  derivative(j, k)=CMath::exp(-element)*element*2.0;
119  }
120  }
121  return derivative;
122  }
123  else
124  {
125  SG_ERROR("Can't compute derivative wrt %s parameter\n", param->m_name);
126  return SGMatrix<float64_t>();
127  }
128 }
129 
130 float64_t CGaussianKernel::compute(int32_t idx_a, int32_t idx_b)
131 {
132  float64_t result=distance(idx_a, idx_b);
133  return CMath::exp(-result);
134 }
135 
137 {
139  if (lhs && rhs)
140  m_distance->init(lhs, rhs);
141 }
142 
143 float64_t CGaussianKernel::distance(int32_t idx_a, int32_t idx_b) const
144 {
145  const float64_t inv_width=1.0/get_width();
146  return CShiftInvariantKernel::distance(idx_a, idx_b)*inv_width;
147 }
148 
149 void CGaussianKernel::register_params()
150 {
151  set_width(1.0);
152  set_cache_size(10);
153 
155  dist->set_disable_sqrt(true);
156  m_distance=dist;
158 
159  SG_ADD(&m_log_width, "log_width", "Kernel width in log domain", MS_AVAILABLE, GRADIENT_AVAILABLE);
160 }
virtual const char * get_name() const =0
virtual void load_serializable_post()
Definition: Kernel.cpp:929
virtual void cleanup()
Definition: Kernel.cpp:173
Base class for the family of kernel functions that only depend on the difference of the inputs...
virtual void cleanup()=0
int32_t index_t
Definition: common.h:62
int32_t num_rhs
number of feature vectors on right hand side
Definition: Kernel.h:1070
virtual void load_serializable_post()
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
parameter struct
#define SG_ERROR(...)
Definition: SGIO.h:129
#define REQUIRE(x,...)
Definition: SGIO.h:206
void set_cache_size(int32_t size)
Definition: Kernel.h:587
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)
Features that support dot products among other operations.
Definition: DotFeatures.h:44
#define SG_REF(x)
Definition: SGObject.h:54
int32_t cache_size
cache_size in MB
Definition: Kernel.h:1048
#define ASSERT(x)
Definition: SGIO.h:201
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
virtual void set_disable_sqrt(bool state)
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
virtual float64_t distance(int32_t idx_a, int32_t idx_b) const
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
static float64_t exp(float64_t x)
Definition: Math.h:621
virtual float64_t distance(int32_t idx_a, int32_t idx_b) const
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:84
virtual void set_width(float64_t w)
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Distance.cpp:78
virtual float64_t get_width() const
class EuclideanDistance

SHOGUN Machine Learning Toolbox - Documentation