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-2009 Soeren Sonnenburg 00008 * Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #include <shogun/lib/common.h> 00012 #include <shogun/kernel/GaussianShortRealKernel.h> 00013 #include <shogun/features/Features.h> 00014 #include <shogun/io/SGIO.h> 00015 00016 using namespace shogun; 00017 00018 CGaussianShortRealKernel::CGaussianShortRealKernel() 00019 : CDotKernel(0), width(0.0) 00020 { 00021 register_params(); 00022 } 00023 00024 CGaussianShortRealKernel::CGaussianShortRealKernel(int32_t size, float64_t w) 00025 : CDotKernel(size), width(w) 00026 { 00027 register_params(); 00028 } 00029 00030 CGaussianShortRealKernel::CGaussianShortRealKernel( 00031 CDenseFeatures<float32_t>* l, CDenseFeatures<float32_t>* r, float64_t w, int32_t size) 00032 : CDotKernel(size), width(w) 00033 { 00034 init(l,r); 00035 register_params(); 00036 } 00037 00038 CGaussianShortRealKernel::~CGaussianShortRealKernel() 00039 { 00040 } 00041 00042 bool CGaussianShortRealKernel::init(CFeatures* l, CFeatures* r) 00043 { 00044 CDotKernel::init(l, r); 00045 return init_normalizer(); 00046 } 00047 00048 float64_t CGaussianShortRealKernel::compute(int32_t idx_a, int32_t idx_b) 00049 { 00050 int32_t alen, blen; 00051 bool afree, bfree; 00052 00053 float32_t* avec=((CDenseFeatures<float32_t>*) lhs)->get_feature_vector(idx_a, alen, afree); 00054 float32_t* bvec=((CDenseFeatures<float32_t>*) rhs)->get_feature_vector(idx_b, blen, bfree); 00055 ASSERT(alen==blen) 00056 00057 float64_t result=0; 00058 for (int32_t i=0; i<alen; i++) 00059 result+=CMath::sq(avec[i]-bvec[i]); 00060 00061 result=exp(-result/width); 00062 00063 ((CDenseFeatures<float32_t>*) lhs)->free_feature_vector(avec, idx_a, afree); 00064 ((CDenseFeatures<float32_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree); 00065 00066 return result; 00067 } 00068 00069 void CGaussianShortRealKernel::register_params() 00070 { 00071 SG_ADD(&width, "width", "kernel width", MS_AVAILABLE); 00072 }