|
SHOGUN v0.9.3
|
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 "lib/common.h" 00012 #include "kernel/GaussianShiftKernel.h" 00013 #include "features/Features.h" 00014 #include "features/SimpleFeatures.h" 00015 #include "lib/io.h" 00016 00017 using namespace shogun; 00018 00019 CGaussianShiftKernel::CGaussianShiftKernel( 00020 int32_t size, float64_t w, int32_t ms, int32_t ss) 00021 : CGaussianKernel(size, w), max_shift(ms), shift_step(ss) 00022 { 00023 } 00024 00025 CGaussianShiftKernel::CGaussianShiftKernel( 00026 CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r, float64_t w, int32_t ms, int32_t ss, 00027 int32_t size) 00028 : CGaussianKernel(l, r, w, size), max_shift(ms), shift_step(ss) 00029 { 00030 init(l,r); 00031 } 00032 00033 CGaussianShiftKernel::~CGaussianShiftKernel() 00034 { 00035 } 00036 00037 float64_t CGaussianShiftKernel::compute(int32_t idx_a, int32_t idx_b) 00038 { 00039 int32_t alen, blen; 00040 bool afree, bfree; 00041 00042 float64_t* avec= 00043 ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree); 00044 float64_t* bvec= 00045 ((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree); 00046 ASSERT(alen==blen); 00047 00048 float64_t result = 0.0 ; 00049 float64_t sum=0.0 ; 00050 for (int32_t i=0; i<alen; i++) 00051 sum+=(avec[i]-bvec[i])*(avec[i]-bvec[i]); 00052 result += exp(-sum/width) ; 00053 00054 for (int32_t shift = shift_step, s=1; shift<max_shift; shift+=shift_step, s++) 00055 { 00056 sum=0.0 ; 00057 for (int32_t i=0; i<alen-shift; i++) 00058 sum+=(avec[i+shift]-bvec[i])*(avec[i+shift]-bvec[i]); 00059 result += exp(-sum/width)/(2*s) ; 00060 00061 sum=0.0 ; 00062 for (int32_t i=0; i<alen-shift; i++) 00063 sum+=(avec[i]-bvec[i+shift])*(avec[i]-bvec[i+shift]); 00064 result += exp(-sum/width)/(2*s) ; 00065 } 00066 00067 ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree); 00068 ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree); 00069 00070 return result; 00071 }