SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GaussianShiftKernel.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) 2008 Gunnar Raetsch
8  * Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/lib/common.h>
14 #include <shogun/io/SGIO.h>
15 
16 using namespace shogun;
17 
19 : CGaussianKernel(), max_shift(0), shift_step(0)
20 {
21  init();
22 }
23 
25  int32_t size, float64_t w, int32_t ms, int32_t ss)
26 : CGaussianKernel(size, w), max_shift(ms), shift_step(ss)
27 {
28  init();
29 }
30 
32  CDenseFeatures<float64_t>* l, CDenseFeatures<float64_t>* r, float64_t w, int32_t ms, int32_t ss,
33  int32_t size)
34 : CGaussianKernel(l, r, w, size), max_shift(ms), shift_step(ss)
35 {
36  init();
37  init(l,r);
38 }
39 
41 {
42 }
43 
44 float64_t CGaussianShiftKernel::compute(int32_t idx_a, int32_t idx_b)
45 {
46  int32_t alen, blen;
47  bool afree, bfree;
48 
49  float64_t* avec=
50  ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
51  float64_t* bvec=
52  ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
53  ASSERT(alen==blen)
54 
55  float64_t result = 0.0 ;
56  float64_t sum=0.0 ;
57  for (int32_t i=0; i<alen; i++)
58  sum+=(avec[i]-bvec[i])*(avec[i]-bvec[i]);
59  result += exp(-sum/width) ;
60 
61  for (int32_t shift = shift_step, s=1; shift<max_shift; shift+=shift_step, s++)
62  {
63  sum=0.0 ;
64  for (int32_t i=0; i<alen-shift; i++)
65  sum+=(avec[i+shift]-bvec[i])*(avec[i+shift]-bvec[i]);
66  result += exp(-sum/width)/(2*s) ;
67 
68  sum=0.0 ;
69  for (int32_t i=0; i<alen-shift; i++)
70  sum+=(avec[i]-bvec[i+shift])*(avec[i]-bvec[i+shift]);
71  result += exp(-sum/width)/(2*s) ;
72  }
73 
74  ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
75  ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
76 
77  return result;
78 }
79 
80 void CGaussianShiftKernel::init()
81 {
82  SG_ADD(&max_shift, "max_shift", "Maximum shift.", MS_AVAILABLE);
83  SG_ADD(&shift_step, "shift_step", "Shift stepsize.", MS_AVAILABLE);
84 }

SHOGUN Machine Learning Toolbox - Documentation