Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <shogun/lib/common.h>
00012 #include <shogun/io/SGIO.h>
00013 #include <shogun/kernel/GaussianMatchStringKernel.h>
00014 #include <shogun/kernel/SqrtDiagKernelNormalizer.h>
00015 #include <shogun/features/Features.h>
00016 #include <shogun/features/StringFeatures.h>
00017
00018 using namespace shogun;
00019
00020 CGaussianMatchStringKernel::CGaussianMatchStringKernel()
00021 : CStringKernel<char>(0), width(0.0)
00022 {
00023 set_normalizer(new CSqrtDiagKernelNormalizer());
00024 register_params();
00025 }
00026
00027 CGaussianMatchStringKernel::CGaussianMatchStringKernel(int32_t size, float64_t w)
00028 : CStringKernel<char>(size), width(w)
00029 {
00030 set_normalizer(new CSqrtDiagKernelNormalizer());
00031 register_params();
00032 }
00033
00034 CGaussianMatchStringKernel::CGaussianMatchStringKernel(
00035 CStringFeatures<char>* l, CStringFeatures<char>* r, float64_t w)
00036 : CStringKernel<char>(10), width(w)
00037 {
00038 set_normalizer(new CSqrtDiagKernelNormalizer());
00039 init(l, r);
00040 register_params();
00041 }
00042
00043 CGaussianMatchStringKernel::~CGaussianMatchStringKernel()
00044 {
00045 cleanup();
00046 }
00047
00048 bool CGaussianMatchStringKernel::init(CFeatures* l, CFeatures* r)
00049 {
00050 CStringKernel<char>::init(l, r);
00051 return init_normalizer();
00052 }
00053
00054 void CGaussianMatchStringKernel::cleanup()
00055 {
00056 CKernel::cleanup();
00057 }
00058
00059 float64_t CGaussianMatchStringKernel::compute(int32_t idx_a, int32_t idx_b)
00060 {
00061 int32_t i, alen, blen ;
00062 bool free_avec, free_bvec;
00063
00064 char* avec = ((CStringFeatures<char>*) lhs)->get_feature_vector(idx_a, alen, free_avec);
00065 char* bvec = ((CStringFeatures<char>*) rhs)->get_feature_vector(idx_b, blen, free_bvec);
00066
00067 float64_t result=0;
00068
00069 ASSERT(alen==blen);
00070
00071 for (i = 0; i<alen; i++)
00072 result+=(avec[i]==bvec[i]) ? 0:4;
00073
00074 result=exp(-result/width);
00075
00076
00077 ((CStringFeatures<char>*) lhs)->free_feature_vector(avec, idx_a, free_avec);
00078 ((CStringFeatures<char>*) rhs)->free_feature_vector(bvec, idx_b, free_bvec);
00079 return result;
00080 }
00081
00082 void CGaussianMatchStringKernel::register_params()
00083 {
00084 m_parameters->add(&width, "width", "kernel width");
00085 }