SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HistogramIntersectionKernel.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2010 Koen van de Sande
8  * Copyright (C) 2010 Koen van de Sande / University of Amsterdam
9  */
10 
11 #include <shogun/lib/common.h>
15 #include <shogun/io/SGIO.h>
16 
17 using namespace shogun;
18 
20 : CDotKernel(0), m_beta(1.0)
21 {
23 }
24 
26 : CDotKernel(size), m_beta(1.0)
27 {
29 }
30 
33  float64_t beta, int32_t size)
34 : CDotKernel(size), m_beta(1.0)
35 {
36  init(l,r);
38 }
39 
41 {
42  cleanup();
43 }
44 
45 bool CHistogramIntersectionKernel::init(CFeatures* l, CFeatures* r)
46 {
47  bool result=CDotKernel::init(l,r);
49  return result;
50 }
51 
52 float64_t CHistogramIntersectionKernel::compute(int32_t idx_a, int32_t idx_b)
53 {
54  int32_t alen, blen;
55  bool afree, bfree;
56 
57  float64_t* avec=
58  ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
59  float64_t* bvec=
60  ((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
61  ASSERT(alen==blen);
62 
63  float64_t result=0;
64 
65  // checking if beta is default or not
66  if (m_beta == 1.0)
67  {
68  // compute standard histogram intersection kernel
69  for (int32_t i=0; i<alen; i++)
70  result += (avec[i] < bvec[i]) ? avec[i] : bvec[i];
71  }
72  else
73  {
74  //compute generalized histogram intersection kernel
75  for (int32_t i=0; i<alen; i++)
76  result += CMath::min(CMath::pow(avec[i],m_beta), CMath::pow(bvec[i],m_beta));
77  }
78  ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
79  ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
80 
81  return result;
82 }
83 
85 {
86  m_parameters->add(&m_beta, "beta", "the beta parameter of the kernel");
87 }
virtual void cleanup()
Definition: Kernel.cpp:138
Parameter * m_parameters
Definition: SGObject.h:297
Template class DotKernel is the base class for kernels working on DotFeatures.
Definition: DotKernel.h:29
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:23
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
#define ASSERT(x)
Definition: SGIO.h:102
static int32_t pow(int32_t x, int32_t n)
Definition: Math.h:339
double float64_t
Definition: common.h:56
virtual bool init_normalizer()
Definition: Kernel.cpp:133
virtual bool init(CFeatures *l, CFeatures *r)
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:810
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:808
The class Features is the base class of all feature objects.
Definition: Features.h:56
static T min(T a, T b)
return the minimum of two integers
Definition: Math.h:155

SHOGUN Machine Learning Toolbox - Documentation