SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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>
19 #include <shogun/io/SGIO.h>
20 
21 using namespace shogun;
22 
24 {
25  init();
26 }
27 
29 {
30  init();
31  set_width(w);
32 }
33 
35  float64_t w, int32_t size) : CDotKernel(size)
36 {
37  init();
38  set_width(w);
39  init(l,r);
40 }
41 
43 {
44  cleanup();
45 }
46 
48 {
49  if (kernel->get_kernel_type()!=K_GAUSSIAN)
50  {
51  SG_SERROR("CGaussianKernel::obtain_from_generic(): provided kernel is "
52  "not of type CGaussianKernel!\n");
53  }
54 
55  /* since an additional reference is returned */
56  SG_REF(kernel);
57  return (CGaussianKernel*)kernel;
58 }
59 
60 #include <typeinfo>
62 {
63  // TODO: remove this after all the classes get shallow_copy properly implemented
64  // this assert is to avoid any subclass of CGaussianKernel accidentally called
65  // with the implement here
66  ASSERT(typeid(*this) == typeid(CGaussianKernel))
68  if (lhs)
69  {
70  ker->init(lhs, rhs);
71  }
72  return ker;
73 }
74 
76 {
77  if (sq_lhs != sq_rhs)
78  SG_FREE(sq_rhs);
79  sq_rhs = NULL;
80 
81  SG_FREE(sq_lhs);
82  sq_lhs = NULL;
83 
85 }
86 
87 void CGaussianKernel::precompute_squared_helper(float64_t* &buf, CDotFeatures* df)
88 {
89  ASSERT(df)
90  int32_t num_vec=df->get_num_vectors();
91  buf=SG_MALLOC(float64_t, num_vec);
92 
93  for (int32_t i=0; i<num_vec; i++)
94  buf[i]=df->dot(i,df, i);
95 }
96 
98 {
100  cleanup();
101 
102  CDotKernel::init(l, r);
103  precompute_squared();
104  return init_normalizer();
105 }
106 
107 float64_t CGaussianKernel::compute(int32_t idx_a, int32_t idx_b)
108 {
109  if (!m_compact)
110  {
111  float64_t result=sq_lhs[idx_a]+sq_rhs[idx_b]
112  -2*CDotKernel::compute(idx_a, idx_b);
113  return CMath::exp(-result/width);
114  }
115 
116  int32_t len_features, power;
117  len_features=((CDenseFeatures<float64_t>*) lhs)->get_num_features();
118  power=(len_features%2==0) ? (len_features+1):len_features;
119 
120  float64_t result=sq_lhs[idx_a]+sq_rhs[idx_b]-2*CDotKernel::compute(idx_a,idx_b);
121  float64_t result_multiplier=1-(sqrt(result/width))/3;
122 
123  if (result_multiplier<=0)
124  result_multiplier=0;
125  else
126  result_multiplier=pow(result_multiplier, power);
127 
128  return result_multiplier*exp(-result/width);
129 }
130 
132 {
134  precompute_squared();
135 }
136 
137 void CGaussianKernel::precompute_squared()
138 {
139  if (!lhs || !rhs)
140  return;
141 
142  precompute_squared_helper(sq_lhs, (CDotFeatures*) lhs);
143 
144  if (lhs==rhs)
145  sq_rhs=sq_lhs;
146  else
147  precompute_squared_helper(sq_rhs, (CDotFeatures*) rhs);
148 }
149 
151  const TParameter* param, index_t index)
152 {
153  REQUIRE(lhs && rhs, "Features not set!\n")
154 
155  if (!strcmp(param->m_name, "width"))
156  {
158 
159  for (int j=0; j<num_lhs; j++)
160  for (int k=0; k<num_rhs; k++)
161  {
162  float64_t element=sq_lhs[j]+sq_rhs[k]-2*CDotKernel::compute(j,k);
163  derivative(j,k)=exp(-element/width)*element/(width*width);
164  }
165 
166  return derivative;
167  }
168  else
169  {
170  SG_ERROR("Can't compute derivative wrt %s parameter\n", param->m_name);
171  return SGMatrix<float64_t>();
172  }
173 }
174 
175 void CGaussianKernel::init()
176 {
177  set_width(1.0);
178  set_compact_enabled(false);
179  sq_lhs=NULL;
180  sq_rhs=NULL;
181  SG_ADD(&width, "width", "Kernel width", MS_AVAILABLE, GRADIENT_AVAILABLE);
182  SG_ADD(&m_compact, "compact", "Compact enabled option", MS_AVAILABLE);
183 }

SHOGUN Machine Learning Toolbox - Documentation