SHOGUN
v3.2.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/io/SGIO.h> 00015 00016 using namespace shogun; 00017 00018 CGaussianShiftKernel::CGaussianShiftKernel() 00019 : CGaussianKernel(), max_shift(0), shift_step(0) 00020 { 00021 init(); 00022 } 00023 00024 CGaussianShiftKernel::CGaussianShiftKernel( 00025 int32_t size, float64_t w, int32_t ms, int32_t ss) 00026 : CGaussianKernel(size, w), max_shift(ms), shift_step(ss) 00027 { 00028 init(); 00029 } 00030 00031 CGaussianShiftKernel::CGaussianShiftKernel( 00032 CDenseFeatures<float64_t>* l, CDenseFeatures<float64_t>* r, float64_t w, int32_t ms, int32_t ss, 00033 int32_t size) 00034 : CGaussianKernel(l, r, w, size), max_shift(ms), shift_step(ss) 00035 { 00036 init(); 00037 init(l,r); 00038 } 00039 00040 CGaussianShiftKernel::~CGaussianShiftKernel() 00041 { 00042 } 00043 00044 float64_t CGaussianShiftKernel::compute(int32_t idx_a, int32_t idx_b) 00045 { 00046 int32_t alen, blen; 00047 bool afree, bfree; 00048 00049 float64_t* avec= 00050 ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree); 00051 float64_t* bvec= 00052 ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree); 00053 ASSERT(alen==blen) 00054 00055 float64_t result = 0.0 ; 00056 float64_t sum=0.0 ; 00057 for (int32_t i=0; i<alen; i++) 00058 sum+=(avec[i]-bvec[i])*(avec[i]-bvec[i]); 00059 result += exp(-sum/width) ; 00060 00061 for (int32_t shift = shift_step, s=1; shift<max_shift; shift+=shift_step, s++) 00062 { 00063 sum=0.0 ; 00064 for (int32_t i=0; i<alen-shift; i++) 00065 sum+=(avec[i+shift]-bvec[i])*(avec[i+shift]-bvec[i]); 00066 result += exp(-sum/width)/(2*s) ; 00067 00068 sum=0.0 ; 00069 for (int32_t i=0; i<alen-shift; i++) 00070 sum+=(avec[i]-bvec[i+shift])*(avec[i]-bvec[i+shift]); 00071 result += exp(-sum/width)/(2*s) ; 00072 } 00073 00074 ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree); 00075 ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree); 00076 00077 return result; 00078 } 00079 00080 void CGaussianShiftKernel::init() 00081 { 00082 SG_ADD(&max_shift, "max_shift", "Maximum shift.", MS_AVAILABLE); 00083 SG_ADD(&shift_step, "shift_step", "Shift stepsize.", MS_AVAILABLE); 00084 }