SHOGUN  4.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GaussianARDKernel.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) 2015 Wu Lin
8  * Written (W) 2012 Jacob Walker
9  *
10  * Adapted from WeightedDegreeRBFKernel.cpp
11  */
12 
14 
15 using namespace shogun;
16 
18 {
19  init();
20 }
21 
23 {
24 }
25 
26 void CGaussianARDKernel::init()
27 {
28  set_width(1.0);
29  SG_ADD(&m_width, "width", "Kernel width", MS_AVAILABLE, GRADIENT_AVAILABLE);
30 }
31 
32 #ifdef HAVE_LINALG_LIB
35  : CLinearARDKernel(size)
36 {
37  init();
38  set_width(width);
39 }
40 
42  CDotFeatures* r, int32_t size, float64_t width)
43  : CLinearARDKernel(size)
44 {
45  init();
46  set_width(width);
47 }
48 
49 bool CGaussianARDKernel::init(CFeatures* l, CFeatures* r)
50 {
51  return CLinearARDKernel::init(l,r);
52 }
53 
55 {
56  if (kernel->get_kernel_type()!=K_GAUSSIANARD)
57  {
58  SG_SERROR("Provided kernel is not of type CGaussianARDKernel!\n");
59  }
60 
61  /* since an additional reference is returned */
62  SG_REF(kernel);
63  return (CGaussianARDKernel*)kernel;
64 }
65 
66 float64_t CGaussianARDKernel::compute(int32_t idx_a, int32_t idx_b)
67 {
68  float64_t result=distance(idx_a,idx_b);
69  return CMath::exp(-result);
70 }
71 
73  const TParameter* param, index_t index)
74 {
75  REQUIRE(lhs && rhs, "Features not set!\n")
76 
77  if (!strcmp(param->m_name, "weights"))
78  {
79  SGMatrix<float64_t> derivative(num_lhs, num_rhs);
80  for (index_t j=0; j<num_lhs; j++)
81  {
82  SGVector<float64_t> avec=((CDotFeatures *)lhs)->get_computed_dot_feature_vector(j);
83  for (index_t k=0; k<num_rhs; k++)
84  {
85  SGVector<float64_t> bvec=((CDotFeatures *)rhs)->get_computed_dot_feature_vector(k);
86  linalg::add(avec, bvec, bvec, 1.0, -1.0);
87  float64_t scale=-kernel(j,k)/m_width;
88  derivative(j,k)=compute_gradient_helper(bvec, bvec, scale, index);
89  }
90  }
91  return derivative;
92  }
93  else if (!strcmp(param->m_name, "width"))
94  {
95  SGMatrix<float64_t> derivative(num_lhs, num_rhs);
96 
97  for (index_t j=0; j<num_lhs; j++)
98  {
99  for (index_t k=0; k<num_rhs; k++)
100  {
101  float64_t tmp=kernel(j,k);
102  derivative(j,k)=-tmp*CMath::log(tmp)/m_width;
103  }
104  }
105 
106  return derivative;
107  }
108  else
109  {
110  SG_ERROR("Can't compute derivative wrt %s parameter\n", param->m_name);
111  return SGMatrix<float64_t>();
112  }
113 }
114 
115 float64_t CGaussianARDKernel::distance(int32_t idx_a, int32_t idx_b)
116 {
117  REQUIRE(rhs, "Right features (rhs) not set!\n")
118 
119  SGVector<float64_t> avec=((CDotFeatures *)lhs)->get_computed_dot_feature_vector(idx_a);
120  SGVector<float64_t> bvec=((CDotFeatures *)rhs)->get_computed_dot_feature_vector(idx_b);
121  linalg::add(avec, bvec, avec, 1.0, -1.0);
122  float64_t result=compute_helper(avec, avec);
123  return result/m_width;
124 }
125 #endif /* HAVE_LINALG_LIB */
float distance(CJLCoverTreePoint p1, CJLCoverTreePoint p2, float64_t upper_bound)
int32_t index_t
Definition: common.h:62
int32_t num_rhs
number of feature vectors on right hand side
Definition: Kernel.h:1057
Linear Kernel with Automatic Relevance Detection computed on CDotFeatures.
parameter struct
Definition: Parameter.h:32
#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:205
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
Definition: DotKernel.h:123
Features that support dot products among other operations.
Definition: DotFeatures.h:44
#define SG_REF(x)
Definition: SGObject.h:51
Gaussian Kernel with Automatic Relevance Detection computed on CDotFeatures.
virtual void set_width(float64_t w)
void add(Matrix A, Matrix B, Matrix C, typename Matrix::Scalar alpha=1.0, typename Matrix::Scalar beta=1.0)
Definition: Core.h:58
double float64_t
Definition: common.h:50
int32_t num_lhs
number of feature vectors on left hand side
Definition: Kernel.h:1055
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1049
static CKernel * obtain_from_generic(CSGObject *kernel)
Definition: Kernel.cpp:883
virtual EKernelType get_kernel_type()=0
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:1047
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
virtual SGMatrix< float64_t > get_parameter_gradient(const TParameter *param, index_t index=-1)
Definition: Kernel.h:851
static float64_t log(float64_t v)
Definition: Math.h:922
The Kernel base class.
Definition: Kernel.h:157
#define SG_ADD(...)
Definition: SGObject.h:81

SHOGUN Machine Learning Toolbox - Documentation