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