SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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>
15 #include <shogun/io/SGIO.h>
16 
17 using namespace shogun;
18 
20 : CGaussianKernel(), max_shift(0), shift_step(0)
21 {
22  init();
23 }
24 
26  int32_t size, float64_t w, int32_t ms, int32_t ss)
27 : CGaussianKernel(size, w), max_shift(ms), shift_step(ss)
28 {
29  init();
30 }
31 
33  CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r, float64_t w, int32_t ms, int32_t ss,
34  int32_t size)
35 : CGaussianKernel(l, r, w, size), max_shift(ms), shift_step(ss)
36 {
37  init();
38  CGaussianKernel::init(l,r);
39 }
40 
42 {
43 }
44 
45 float64_t CGaussianShiftKernel::compute(int32_t idx_a, int32_t idx_b)
46 {
47  int32_t alen, blen;
48  bool afree, bfree;
49 
50  float64_t* avec=
51  ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
52  float64_t* bvec=
53  ((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
54  ASSERT(alen==blen);
55 
56  float64_t result = 0.0 ;
57  float64_t sum=0.0 ;
58  for (int32_t i=0; i<alen; i++)
59  sum+=(avec[i]-bvec[i])*(avec[i]-bvec[i]);
60  result += exp(-sum/width) ;
61 
62  for (int32_t shift = shift_step, s=1; shift<max_shift; shift+=shift_step, s++)
63  {
64  sum=0.0 ;
65  for (int32_t i=0; i<alen-shift; i++)
66  sum+=(avec[i+shift]-bvec[i])*(avec[i+shift]-bvec[i]);
67  result += exp(-sum/width)/(2*s) ;
68 
69  sum=0.0 ;
70  for (int32_t i=0; i<alen-shift; i++)
71  sum+=(avec[i]-bvec[i+shift])*(avec[i]-bvec[i+shift]);
72  result += exp(-sum/width)/(2*s) ;
73  }
74 
75  ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
76  ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
77 
78  return result;
79 }
80 
81 void CGaussianShiftKernel::init()
82 {
83  m_parameters->add(&max_shift, "max_shift", "Maximum shift.");
84  m_parameters->add(&shift_step, "shift_step", "Shift stepsize.");
85 }
86 

SHOGUN Machine Learning Toolbox - Documentation