SHOGUN
v1.1.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2008 Gunnar Raetsch 00008 * Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #include <shogun/lib/common.h> 00012 #include <shogun/kernel/GaussianShiftKernel.h> 00013 #include <shogun/features/Features.h> 00014 #include <shogun/features/SimpleFeatures.h> 00015 #include <shogun/io/SGIO.h> 00016 00017 using namespace shogun; 00018 00019 CGaussianShiftKernel::CGaussianShiftKernel() 00020 : CGaussianKernel(), max_shift(0), shift_step(0) 00021 { 00022 init(); 00023 } 00024 00025 CGaussianShiftKernel::CGaussianShiftKernel( 00026 int32_t size, float64_t w, int32_t ms, int32_t ss) 00027 : CGaussianKernel(size, w), max_shift(ms), shift_step(ss) 00028 { 00029 init(); 00030 } 00031 00032 CGaussianShiftKernel::CGaussianShiftKernel( 00033 CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r, float64_t w, int32_t ms, int32_t ss, 00034 int32_t size) 00035 : CGaussianKernel(l, r, w, size), max_shift(ms), shift_step(ss) 00036 { 00037 init(); 00038 CGaussianKernel::init(l,r); 00039 } 00040 00041 CGaussianShiftKernel::~CGaussianShiftKernel() 00042 { 00043 } 00044 00045 float64_t CGaussianShiftKernel::compute(int32_t idx_a, int32_t idx_b) 00046 { 00047 int32_t alen, blen; 00048 bool afree, bfree; 00049 00050 float64_t* avec= 00051 ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree); 00052 float64_t* bvec= 00053 ((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree); 00054 ASSERT(alen==blen); 00055 00056 float64_t result = 0.0 ; 00057 float64_t sum=0.0 ; 00058 for (int32_t i=0; i<alen; i++) 00059 sum+=(avec[i]-bvec[i])*(avec[i]-bvec[i]); 00060 result += exp(-sum/width) ; 00061 00062 for (int32_t shift = shift_step, s=1; shift<max_shift; shift+=shift_step, s++) 00063 { 00064 sum=0.0 ; 00065 for (int32_t i=0; i<alen-shift; i++) 00066 sum+=(avec[i+shift]-bvec[i])*(avec[i+shift]-bvec[i]); 00067 result += exp(-sum/width)/(2*s) ; 00068 00069 sum=0.0 ; 00070 for (int32_t i=0; i<alen-shift; i++) 00071 sum+=(avec[i]-bvec[i+shift])*(avec[i]-bvec[i+shift]); 00072 result += exp(-sum/width)/(2*s) ; 00073 } 00074 00075 ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree); 00076 ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree); 00077 00078 return result; 00079 } 00080 00081 void CGaussianShiftKernel::init() 00082 { 00083 m_parameters->add(&max_shift, "max_shift", "Maximum shift."); 00084 m_parameters->add(&shift_step, "shift_step", "Shift stepsize."); 00085 } 00086