SHOGUN
v1.1.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2010 Koen van de Sande 00008 * Copyright (C) 2010 Koen van de Sande / University of Amsterdam 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 // checking if beta is default or not 00066 if (m_beta == 1.0) 00067 { 00068 // compute standard histogram intersection kernel 00069 for (int32_t i=0; i<alen; i++) 00070 result += (avec[i] < bvec[i]) ? avec[i] : bvec[i]; 00071 } 00072 else 00073 { 00074 //compute generalized histogram intersection kernel 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 }