SHOGUN  v2.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  * (W) 2012 Jacob Walker
8  *
9  * Adapted from WeightedDegreeRBFKernel.cpp
10  *
11  */
12 
13 #include <shogun/lib/common.h>
16 #include <shogun/io/SGIO.h>
17 
18 using namespace shogun;
19 
22 {
23  init();
24 }
25 
26 
28 : CLinearARDKernel(size), m_width(width)
29 {
30  init();
31 }
32 
35  int32_t size, float64_t width)
36 : CLinearARDKernel(size), m_width(width)
37 {
38  init();
39 }
40 
41 bool CGaussianARDKernel::init(CFeatures* l, CFeatures* r)
42 {
43  return CLinearARDKernel::init(l,r);
44 }
45 
46 void CGaussianARDKernel::init()
47 {
48  m_width = 2.0;
49 
50  SG_ADD(&m_width, "width", "Kernel Width", MS_AVAILABLE);
51 }
52 
54 {
55 }
56 
57 float64_t CGaussianARDKernel::compute(int32_t idx_a, int32_t idx_b)
58 {
59  if (!lhs || !rhs)
60  SG_ERROR("Features not set!\n");
61 
63  = ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a);
65  = ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b);
66 
67  REQUIRE(avec.vlen==bvec.vlen, "Number of Right and Left Hand "\
68  "Features Must be the Same./n");
69 
70  float64_t result=0;
71 
72  for (index_t i = 0; i < avec.vlen; i++)
73  result += CMath::pow((avec[i]-bvec[i])*m_weights[i], 2);
74 
75  return CMath::exp(-result/m_width);
76 }
77 
79  CSGObject* obj, index_t index)
80 {
81  if (!lhs || !rhs)
82  SG_ERROR("Features not set!\n");
83 
84  if (!strcmp(param->m_name, "weights") && obj == this)
85  {
87 
88  for (index_t j = 0; j < num_lhs; j++)
89  {
90  for (index_t k = 0; k < num_rhs; k++)
91  {
93  = ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(j);
95  = ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(k);
96 
97  REQUIRE(avec.vlen==bvec.vlen, "Number of Right and Left Hand "\
98  "Features Must be the Same./n");
99 
100  float64_t element = compute(j,k);
101  float64_t product =
102  CMath::pow((avec[index]-bvec[index]), 2)
103  *(m_weights[index]/m_width);
104 
105  derivative(j,k) = -2*element*product;
106  }
107  }
108 
109  return derivative;
110  }
111 
112  else if (!strcmp(param->m_name, "width") && obj == this)
113  {
114  SGMatrix<float64_t> derivative(num_lhs, num_rhs);
115 
116  for (index_t j = 0; j < num_lhs; j++)
117  {
118  for (index_t k = 0; k < num_rhs; k++)
119  {
121  = ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(j);
123  = ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(k);
124 
125  REQUIRE(avec.vlen==bvec.vlen, "Number of Right and Left Hand "\
126  "Features Must be the Same./n");
127 
128  float64_t result=0;
129 
130  for (index_t i = 0; i < avec.vlen; i++)
131  result += CMath::pow((avec[i]-bvec[i])*m_weights[i], 2);
132 
133  derivative(j,k) = CMath::exp(-result/m_width)*
134  result/(m_width*m_width);
135  }
136  }
137 
138  return derivative;
139  }
140 
141 
142  else
143  return SGMatrix<float64_t>();
144 }

SHOGUN Machine Learning Toolbox - Documentation