GaussianShiftKernel.cpp

Go to the documentation of this file.
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) 1999-2008 Soeren Sonnenburg
00008  * Copyright (C) 1999-2008 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/RealFeatures.h"
00015 #include "lib/io.h"
00016 
00017 CGaussianShiftKernel::CGaussianShiftKernel(INT size, double w, int ms, int ss)
00018 : CGaussianKernel(size, w), max_shift(ms), shift_step(ss)
00019 {
00020 }
00021 
00022 CGaussianShiftKernel::CGaussianShiftKernel(
00023     CRealFeatures* l, CRealFeatures* r, double w, int ms, int ss, INT size)
00024 : CGaussianKernel(l, r, w, size), max_shift(ms), shift_step(ss)
00025 {
00026     init(l,r);
00027 }
00028 
00029 CGaussianShiftKernel::~CGaussianShiftKernel()
00030 {
00031 }
00032 
00033 DREAL CGaussianShiftKernel::compute(INT idx_a, INT idx_b)
00034 {
00035     INT alen, blen;
00036     bool afree, bfree;
00037 
00038     double* avec=((CRealFeatures*) lhs)->get_feature_vector(idx_a, alen, afree);
00039     double* bvec=((CRealFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree);
00040     ASSERT(alen==blen);
00041 
00042     DREAL result = 0.0 ;
00043     DREAL sum=0.0 ;
00044     for (INT i=0; i<alen; i++)
00045         sum+=(avec[i]-bvec[i])*(avec[i]-bvec[i]);
00046     result += exp(-sum/width) ;
00047 
00048     for (INT shift = shift_step, s=1; shift<max_shift; shift+=shift_step, s++)
00049     {
00050         sum=0.0 ;
00051         for (INT i=0; i<alen-shift; i++)
00052             sum+=(avec[i+shift]-bvec[i])*(avec[i+shift]-bvec[i]);
00053         result += exp(-sum/width)/(2*s) ;
00054 
00055         sum=0.0 ;
00056         for (INT i=0; i<alen-shift; i++)
00057             sum+=(avec[i]-bvec[i+shift])*(avec[i]-bvec[i+shift]);
00058         result += exp(-sum/width)/(2*s) ;
00059     }
00060 
00061     ((CRealFeatures*) lhs)->free_feature_vector(avec, idx_a, afree);
00062     ((CRealFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00063 
00064     return result;
00065 }

SHOGUN Machine Learning Toolbox - Documentation