HistogramIntersectionKernel.cpp

Go to the documentation of this file.
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/io/SGIO.h>
00015 
00016 using namespace shogun;
00017 
00018 CHistogramIntersectionKernel::CHistogramIntersectionKernel()
00019 : CDotKernel(0), m_beta(1.0)
00020 {
00021     register_params();
00022 }
00023 
00024 CHistogramIntersectionKernel::CHistogramIntersectionKernel(int32_t size)
00025 : CDotKernel(size), m_beta(1.0)
00026 {
00027     register_params();
00028 }
00029 
00030 CHistogramIntersectionKernel::CHistogramIntersectionKernel(
00031     CDenseFeatures<float64_t>* l, CDenseFeatures<float64_t>* r,
00032     float64_t beta, int32_t size)
00033 : CDotKernel(size), m_beta(beta)
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         ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
00058     float64_t* bvec=
00059         ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
00060     ASSERT(alen==blen);
00061 
00062     float64_t result=0;
00063 
00064     // checking if beta is default or not
00065     if (m_beta == 1.0)
00066     {
00067         // compute standard histogram intersection kernel
00068         for (int32_t i=0; i<alen; i++)
00069             result += (avec[i] < bvec[i]) ? avec[i] : bvec[i];
00070     }
00071     else
00072     {
00073         //compute generalized histogram intersection kernel
00074         for (int32_t i=0; i<alen; i++)
00075             result += CMath::min(CMath::pow(avec[i],m_beta), CMath::pow(bvec[i],m_beta));
00076     }
00077     ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00078     ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00079 
00080     return result;
00081 }
00082 
00083 void CHistogramIntersectionKernel::register_params()
00084 {
00085     SG_ADD(&m_beta, "beta", "the beta parameter of the kernel", MS_AVAILABLE);
00086 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation