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