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/kernel/JensenShannonKernel.h>
00013 #include <shogun/features/Features.h>
00014 #include <shogun/io/SGIO.h>
00015
00016 using namespace shogun;
00017
00018 CJensenShannonKernel::CJensenShannonKernel()
00019 : CDotKernel(0)
00020 {
00021 }
00022
00023 CJensenShannonKernel::CJensenShannonKernel(int32_t size)
00024 : CDotKernel(size)
00025 {
00026 }
00027
00028 CJensenShannonKernel::CJensenShannonKernel(
00029 CDenseFeatures<float64_t>* l, CDenseFeatures<float64_t>* r, int32_t size)
00030 : CDotKernel(size)
00031 {
00032 init(l,r);
00033 }
00034
00035 CJensenShannonKernel::~CJensenShannonKernel()
00036 {
00037 cleanup();
00038 }
00039
00040 bool CJensenShannonKernel::init(CFeatures* l, CFeatures* r)
00041 {
00042 bool result=CDotKernel::init(l,r);
00043 init_normalizer();
00044 return result;
00045 }
00046
00047 float64_t CJensenShannonKernel::compute(int32_t idx_a, int32_t idx_b)
00048 {
00049 int32_t alen, blen;
00050 bool afree, bfree;
00051
00052 float64_t* avec=
00053 ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
00054 float64_t* bvec=
00055 ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
00056 ASSERT(alen==blen);
00057
00058 float64_t result=0;
00059
00060
00061 for (int32_t i=0; i<alen; i++) {
00062 float64_t a_i = 0, b_i = 0;
00063 float64_t ab = avec[i]+bvec[i];
00064 if (avec[i] != 0)
00065 a_i = avec[i] * CMath::log2(ab/avec[i]);
00066 if (bvec[i] != 0)
00067 b_i = bvec[i] * CMath::log2(ab/bvec[i]);
00068
00069 result += 0.5*(a_i + b_i);
00070 }
00071
00072 ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00073 ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00074
00075 return result;
00076 }
00077