SHOGUN  4.1.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 #ifdef HAVE_LINALG_LIB
17 #endif
18 
19 using namespace shogun;
20 
22 {
23  initialize();
24 }
25 
27 {
28 }
29 
30 void CGaussianARDKernel::initialize()
31 {
32  set_width(1.0);
33  SG_ADD(&m_width, "width", "Kernel width", MS_AVAILABLE, GRADIENT_AVAILABLE);
34 }
35 
36 #ifdef HAVE_LINALG_LIB
38  : CLinearARDKernel(size)
39 {
40  initialize();
41  set_width(width);
42 }
43 
45  CDotFeatures* r, int32_t size, float64_t width)
46  : CLinearARDKernel(size)
47 {
48  initialize();
49  set_width(width);
50 }
51 
52 bool CGaussianARDKernel::init(CFeatures* l, CFeatures* r)
53 {
54  return CLinearARDKernel::init(l,r);
55 }
56 
58 {
59  if (kernel->get_kernel_type()!=K_GAUSSIANARD)
60  {
61  SG_SERROR("Provided kernel is not of type CGaussianARDKernel!\n");
62  }
63 
64  /* since an additional reference is returned */
65  SG_REF(kernel);
66  return (CGaussianARDKernel*)kernel;
67 }
68 
69 float64_t CGaussianARDKernel::compute(int32_t idx_a, int32_t idx_b)
70 {
71  float64_t result=distance(idx_a,idx_b);
72  return CMath::exp(-result);
73 }
74 
76  const TParameter* param, index_t index)
77 {
78  REQUIRE(lhs && rhs, "Features not set!\n")
79 
80  if (!strcmp(param->m_name, "weights"))
81  {
82  SGMatrix<float64_t> derivative(num_lhs, num_rhs);
83  for (index_t j=0; j<num_lhs; j++)
84  {
85  SGVector<float64_t> avec=((CDotFeatures *)lhs)->get_computed_dot_feature_vector(j);
86  for (index_t k=0; k<num_rhs; k++)
87  {
88  SGVector<float64_t> bvec=((CDotFeatures *)rhs)->get_computed_dot_feature_vector(k);
89  linalg::add(avec, bvec, bvec, 1.0, -1.0);
91  derivative(j,k)=compute_gradient_helper(bvec, bvec, scale, index);
92  }
93  }
94  return derivative;
95  }
96  else if (!strcmp(param->m_name, "width"))
97  {
98  SGMatrix<float64_t> derivative(num_lhs, num_rhs);
99 
100  for (index_t j=0; j<num_lhs; j++)
101  {
102  for (index_t k=0; k<num_rhs; k++)
103  {
104  float64_t tmp=kernel(j,k);
105  derivative(j,k)=-tmp*CMath::log(tmp)/m_width;
106  }
107  }
108 
109  return derivative;
110  }
111  else
112  {
113  SG_ERROR("Can't compute derivative wrt %s parameter\n", param->m_name);
114  return SGMatrix<float64_t>();
115  }
116 }
117 
118 float64_t CGaussianARDKernel::distance(int32_t idx_a, int32_t idx_b)
119 {
120  REQUIRE(rhs, "Right features (rhs) not set!\n")
121  REQUIRE(lhs, "Left features (lhs) not set!\n")
122 
123  SGVector<float64_t> avec=((CDotFeatures *)lhs)->get_computed_dot_feature_vector(idx_a);
124  SGVector<float64_t> bvec=((CDotFeatures *)rhs)->get_computed_dot_feature_vector(idx_b);
125  linalg::add(avec, bvec, avec, 1.0, -1.0);
126  float64_t result=compute_helper(avec, avec);
127  return result/m_width;
128 }
129 #endif /* HAVE_LINALG_LIB */

SHOGUN Machine Learning Toolbox - Documentation