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